Merge pull request #182 from actions/conorsloan/check-for-uncommitted-changes

Fail if local changes made to the checked out action content
This commit is contained in:
Conor Sloan
2024-08-28 13:56:41 +01:00
committed by GitHub
4 changed files with 56 additions and 1 deletions
+27
View File
@@ -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.'
)
})
})
+1 -1
View File
@@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="116" height="20" role="img" aria-label="Coverage: 97.31%"><title>Coverage: 97.31%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="116" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="63" height="20" fill="#555"/><rect x="63" width="53" height="20" fill="#4c1"/><rect width="116" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="325" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">Coverage</text><text x="325" y="140" transform="scale(.1)" fill="#fff" textLength="530">Coverage</text><text aria-hidden="true" x="885" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">97.31%</text><text x="885" y="140" transform="scale(.1)" fill="#fff" textLength="430">97.31%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="116" height="20" role="img" aria-label="Coverage: 97.08%"><title>Coverage: 97.08%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="116" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="63" height="20" fill="#555"/><rect x="63" width="53" height="20" fill="#4c1"/><rect width="116" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="325" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">Coverage</text><text x="325" y="140" transform="scale(.1)" fill="#fff" textLength="530">Coverage</text><text aria-hidden="true" x="885" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">97.08%</text><text x="885" y="140" transform="scale(.1)" fill="#fff" textLength="430">97.08%</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Generated Vendored
+13
View File
@@ -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) {
+15
View File
@@ -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.