From 7f14f1d82430c1da28a7ba63cddce9b7e44b8584 Mon Sep 17 00:00:00 2001 From: SKi Date: Wed, 1 Mar 2023 19:23:12 -0800 Subject: [PATCH 1/5] Fix Self hosted runner issue wrt bad submodules - solution cleanup working space. --- dist/index.js | 10 ++++++++++ src/git-command-manager.ts | 6 ++++++ src/git-directory-helper.ts | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/dist/index.js b/dist/index.js index 0a33ea19d..e83c1b0f5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7683,6 +7683,12 @@ class GitCommandManager { yield this.execGit(args); }); } + submoduleStatus() { + return __awaiter(this, void 0, void 0, function* () { + const output = yield this.execGit(['submodule', 'status'], true); + return output.exitCode === 0; + }); + } tagExists(pattern) { return __awaiter(this, void 0, void 0, function* () { const output = yield this.execGit(['tag', '--list', pattern]); @@ -9436,6 +9442,10 @@ 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; + } // Clean if (clean) { core.startGroup('Cleaning the repository'); diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 01aedfe55..731fbb203 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,11 @@ class GitCommandManager { await this.execGit(args) } + async submoduleStatus(): Promise { + const output = await this.execGit(['submodule', 'status'], true) + 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 2979e9781..ac6145489 100644 --- a/src/git-directory-helper.ts +++ b/src/git-directory-helper.ts @@ -81,6 +81,11 @@ export async function prepareExistingDirectory( } core.endGroup() + // Check for submodules and delete any existing files if submodules are present + if (!(await git.submoduleStatus())){ + remove = true + } + // Clean if (clean) { core.startGroup('Cleaning the repository') From 39cfb844ff2bd153a403cd469f4e22353dfa324a Mon Sep 17 00:00:00 2001 From: SKi Date: Wed, 1 Mar 2023 19:31:47 -0800 Subject: [PATCH 2/5] Fix format with npm run format output --- src/git-directory-helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git-directory-helper.ts b/src/git-directory-helper.ts index ac6145489..848c80433 100644 --- a/src/git-directory-helper.ts +++ b/src/git-directory-helper.ts @@ -82,7 +82,7 @@ export async function prepareExistingDirectory( core.endGroup() // Check for submodules and delete any existing files if submodules are present - if (!(await git.submoduleStatus())){ + if (!(await git.submoduleStatus())) { remove = true } From d12863bffd0b89f66bb35c44a231bb2fdc5e3dad Mon Sep 17 00:00:00 2001 From: SKi Date: Wed, 1 Mar 2023 19:42:00 -0800 Subject: [PATCH 3/5] Add mock implementation for new function submoduleStatus --- __test__/git-auth-helper.test.ts | 3 +++ __test__/git-directory-helper.test.ts | 3 +++ dist/index.js | 2 ++ src/git-command-manager.ts | 1 + src/git-directory-helper.ts | 1 + 5 files changed, 10 insertions(+) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index 2acec38f8..b58010b7c 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 70849b538..a783177d0 100644 --- a/__test__/git-directory-helper.test.ts +++ b/__test__/git-directory-helper.test.ts @@ -423,6 +423,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 e83c1b0f5..e9f579473 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7686,6 +7686,7 @@ class GitCommandManager { 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; }); } @@ -9445,6 +9446,7 @@ function prepareExistingDirectory(git, repositoryPath, repositoryUrl, clean, ref // 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) { diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 731fbb203..ab07524e1 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -360,6 +360,7 @@ class GitCommandManager { async submoduleStatus(): Promise { const output = await this.execGit(['submodule', 'status'], true) + core.debug(output.stdout) return output.exitCode === 0 } diff --git a/src/git-directory-helper.ts b/src/git-directory-helper.ts index 848c80433..fcd4b60b2 100644 --- a/src/git-directory-helper.ts +++ b/src/git-directory-helper.ts @@ -84,6 +84,7 @@ export async function prepareExistingDirectory( // 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 From bb965fa266499cc8df6b104e7e448f6f6744cc61 Mon Sep 17 00:00:00 2001 From: sminnie Date: Sun, 19 Mar 2023 01:08:37 +0000 Subject: [PATCH 4/5] Add 2 test cases for submodule status. This commit adds tests to verify the behavior of the gitDirectoryHelper.prepareExistingDirectory() function when the submodule status is either true or false. The test cleanWhenSubmoduleStatusIsFalse verifies that the function will clean the directory when the submodule status is false. The test sets up a mock implementation of git.submoduleStatus to always return false, writes a file to the repository, and then calls gitDirectoryHelper.prepareExistingDirectory(). The test verifies that the directory is cleaned and that git.tryClean() is called. The test doesNotCleanWhenSubmoduleStatusIsTrue verifies that the function will not clean the directory when the submodule status is true. The test sets up a mock implementation of git.submoduleStatus to always return true, writes a file to the repository, and then calls gitDirectoryHelper.prepareExistingDirectory(). The test verifies that the directory is not cleaned, that the file and .git folder are present, and that git.tryClean() is called. These tests ensure that the function behaves as expected based on the submodule status. --- __test__/git-directory-helper.test.ts | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts index a783177d0..02118ae48 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 From 6b842657bc27d31deeae7977f25f4f1ced77a653 Mon Sep 17 00:00:00 2001 From: SKi Date: Tue, 11 Apr 2023 20:26:34 -0700 Subject: [PATCH 5/5] Codeql-Action Analyse revert v1 to v2 --- .github/workflows/codeql-analysis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index a771bc05a..d936b9c7c 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -42,7 +42,7 @@ jobs: uses: actions/checkout@v3 - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -55,4 +55,4 @@ jobs: - run: rm -rf dist # We want code scanning to analyze lib instead (individual .js files) - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2