From 47fbe2df0ad0e27efb67a70beac3555f192b062f Mon Sep 17 00:00:00 2001 From: SKi Date: Fri, 14 Apr 2023 03:26:47 -0700 Subject: [PATCH] Fix: Checkout fail in self-hosted runners when faulty submodule are checked-in (#1196) * Fix Self hosted runner issue wrt bad submodules - solution cleanup working space. * Fix format with npm run format output * Add mock implementation for new function submoduleStatus * Add 2 test cases for submodule status. * Codeql-Action Analyse revert v1 to v2 --------- Co-authored-by: Bassem Dghaidi <568794+Link-@users.noreply.github.com> Co-authored-by: sminnie --- __test__/git-auth-helper.test.ts | 3 ++ __test__/git-directory-helper.test.ts | 62 +++++++++++++++++++++++++++ dist/index.js | 12 ++++++ src/git-command-manager.ts | 7 +++ src/git-directory-helper.ts | 6 +++ 5 files changed, 90 insertions(+) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index 2acec38..b58010b 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -770,6 +770,9 @@ async function setup(testName: string): Promise { return '' }), submoduleSync: jest.fn(), + submoduleStatus: jest.fn(async () => { + return true + }), submoduleUpdate: jest.fn(), tagExists: jest.fn(), tryClean: jest.fn(), diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts index 70849b5..02118ae 100644 --- a/__test__/git-directory-helper.test.ts +++ b/__test__/git-directory-helper.test.ts @@ -281,6 +281,65 @@ describe('git-directory-helper tests', () => { expect(git.branchDelete).toHaveBeenCalledWith(false, 'local-branch-2') }) + const cleanWhenSubmoduleStatusIsFalse = + 'cleans when submodule status is false' + + it(cleanWhenSubmoduleStatusIsFalse, async () => { + // Arrange + await setup(cleanWhenSubmoduleStatusIsFalse) + await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '') + + //mock bad submodule + + const submoduleStatus = git.submoduleStatus as jest.Mock + submoduleStatus.mockImplementation(async (remote: boolean) => { + return false + }) + + // Act + await gitDirectoryHelper.prepareExistingDirectory( + git, + repositoryPath, + repositoryUrl, + clean, + ref + ) + + // Assert + const files = await fs.promises.readdir(repositoryPath) + expect(files).toHaveLength(0) + expect(git.tryClean).toHaveBeenCalled() + }) + + const doesNotCleanWhenSubmoduleStatusIsTrue = + 'does not clean when submodule status is true' + + it(doesNotCleanWhenSubmoduleStatusIsTrue, async () => { + // Arrange + await setup(doesNotCleanWhenSubmoduleStatusIsTrue) + await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '') + + const submoduleStatus = git.submoduleStatus as jest.Mock + submoduleStatus.mockImplementation(async (remote: boolean) => { + return true + }) + + // Act + await gitDirectoryHelper.prepareExistingDirectory( + git, + repositoryPath, + repositoryUrl, + clean, + ref + ) + + // Assert + + const files = await fs.promises.readdir(repositoryPath) + expect(files.sort()).toEqual(['.git', 'my-file']) + expect(git.tryClean).toHaveBeenCalled() + }) + const removesLockFiles = 'removes lock files' it(removesLockFiles, async () => { // Arrange @@ -423,6 +482,9 @@ async function setup(testName: string): Promise { submoduleForeach: jest.fn(), submoduleSync: jest.fn(), submoduleUpdate: jest.fn(), + submoduleStatus: jest.fn(async () => { + return true + }), tagExists: jest.fn(), tryClean: jest.fn(async () => { return true diff --git a/dist/index.js b/dist/index.js index 1b418d7..13a291a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -765,6 +765,13 @@ class GitCommandManager { yield this.execGit(args); }); } + submoduleStatus() { + return __awaiter(this, void 0, void 0, function* () { + const output = yield this.execGit(['submodule', 'status'], true); + core.debug(output.stdout); + return output.exitCode === 0; + }); + } tagExists(pattern) { return __awaiter(this, void 0, void 0, function* () { const output = yield this.execGit(['tag', '--list', pattern]); @@ -1023,6 +1030,11 @@ function prepareExistingDirectory(git, repositoryPath, repositoryUrl, clean, ref } } core.endGroup(); + // Check for submodules and delete any existing files if submodules are present + if (!(yield git.submoduleStatus())) { + remove = true; + core.info('Bad Submodules found, removing existing files'); + } // Clean if (clean) { core.startGroup('Cleaning the repository'); diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 01aedfe..ab07524 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -41,6 +41,7 @@ export interface IGitCommandManager { submoduleForeach(command: string, recursive: boolean): Promise submoduleSync(recursive: boolean): Promise submoduleUpdate(fetchDepth: number, recursive: boolean): Promise + submoduleStatus(): Promise tagExists(pattern: string): Promise tryClean(): Promise tryConfigUnset(configKey: string, globalConfig?: boolean): Promise @@ -357,6 +358,12 @@ class GitCommandManager { await this.execGit(args) } + async submoduleStatus(): Promise { + const output = await this.execGit(['submodule', 'status'], true) + core.debug(output.stdout) + return output.exitCode === 0 + } + async tagExists(pattern: string): Promise { const output = await this.execGit(['tag', '--list', pattern]) return !!output.stdout.trim() diff --git a/src/git-directory-helper.ts b/src/git-directory-helper.ts index 2979e97..fcd4b60 100644 --- a/src/git-directory-helper.ts +++ b/src/git-directory-helper.ts @@ -81,6 +81,12 @@ export async function prepareExistingDirectory( } core.endGroup() + // Check for submodules and delete any existing files if submodules are present + if (!(await git.submoduleStatus())) { + remove = true + core.info('Bad Submodules found, removing existing files') + } + // Clean if (clean) { core.startGroup('Cleaning the repository')