diff --git a/__tests__/fs-helper.test.ts b/__tests__/fs-helper.test.ts index 4456db8..d400934 100644 --- a/__tests__/fs-helper.test.ts +++ b/__tests__/fs-helper.test.ts @@ -230,6 +230,10 @@ describe('ensureCorrectShaCheckedOut', () => { execSync('git config user.email monalisa@github.com', { cwd: dir }) execSync('git config user.name Mona', { cwd: dir }) + // Add a file to the repo + fs.writeFileSync(`${dir}/file1.txt`, fileContent) + execSync('git add .', { cwd: dir }) + // Add two commits execSync('git commit --allow-empty -m "test"', { cwd: dir }) execSync('git commit --allow-empty -m "test"', { cwd: dir }) @@ -284,4 +288,27 @@ describe('ensureCorrectShaCheckedOut', () => { fsHelper.ensureTagAndRefCheckedOut(`refs/heads/main`, commit2, dir) ).rejects.toThrow('Tag ref provided is not in expected format.') }) + + it('throws if there are untracked files in the working directory', async () => { + // Add an untracked file + fs.writeFileSync(`${dir}/untracked-file.txt`, fileContent) + + await expect(async () => + fsHelper.ensureTagAndRefCheckedOut(`refs/tags/${tag2}`, commit2, dir) + ).rejects.toThrow( + 'The working directory has uncommitted changes. Uploading modified code from the checked out repository is not supported by this action.' + ) + }) + + it('throws if there are uncommitted changes in the working directory', async () => { + // Add an untracked file + fs.writeFileSync(`${dir}/file1.txt`, fileContent + fileContent) + execSync('git add .', { cwd: dir }) + + await expect(async () => + fsHelper.ensureTagAndRefCheckedOut(`refs/tags/${tag2}`, commit2, dir) + ).rejects.toThrow( + 'The working directory has uncommitted changes. Uploading modified code from the checked out repository is not supported by this action.' + ) + }) }) diff --git a/badges/coverage.svg b/badges/coverage.svg index 9c6aee7..367f1bd 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 97.31%Coverage97.31% \ No newline at end of file +Coverage: 97.08%Coverage97.08% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 30bfc2a..ae9fc2a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -106642,6 +106642,19 @@ async function ensureTagAndRefCheckedOut(tagRef, expectedSha, gitDir) { if (currentlyCheckedOutSha.trim() !== expectedSha) { throw new Error(`The expected commit associated with the tag ${tagRef} is not checked out.`); } + // Call git status to check for any changes in the working directory + // This version of this action only supports uploading actions packages + // which contain the same content as the repository at the appropriate source commit. + let status; + try { + status = await git.status(); + } + catch (err) { + throw new Error(`Error checking git status: ${err}`); + } + if (!status.isClean()) { + throw new Error(`The working directory has uncommitted changes. Uploading modified code from the checked out repository is not supported by this action.`); + } } // Converts a file path to a filemetadata object by querying the fs for relevant metadata. async function fileMetadata(filePath) { diff --git a/src/fs-helper.ts b/src/fs-helper.ts index 2d4965b..a9b5997 100644 --- a/src/fs-helper.ts +++ b/src/fs-helper.ts @@ -153,6 +153,21 @@ export async function ensureTagAndRefCheckedOut( `The expected commit associated with the tag ${tagRef} is not checked out.` ) } + + // Call git status to check for any changes in the working directory + // This version of this action only supports uploading actions packages + // which contain the same content as the repository at the appropriate source commit. + let status: simpleGit.StatusResult + try { + status = await git.status() + } catch (err) { + throw new Error(`Error checking git status: ${err}`) + } + if (!status.isClean()) { + throw new Error( + `The working directory has uncommitted changes. Uploading modified code from the checked out repository is not supported by this action.` + ) + } } // Converts a file path to a filemetadata object by querying the fs for relevant metadata.