Skip to content

Commit e3f16e2

Browse files
authored
Merge pull request #182 from actions/conorsloan/check-for-uncommitted-changes
Fail if local changes made to the checked out action content
2 parents 4aeb3f6 + 1255bb0 commit e3f16e2

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

__tests__/fs-helper.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ describe('ensureCorrectShaCheckedOut', () => {
230230
execSync('git config user.email [email protected]', { cwd: dir })
231231
execSync('git config user.name Mona', { cwd: dir })
232232

233+
// Add a file to the repo
234+
fs.writeFileSync(`${dir}/file1.txt`, fileContent)
235+
execSync('git add .', { cwd: dir })
236+
233237
// Add two commits
234238
execSync('git commit --allow-empty -m "test"', { cwd: dir })
235239
execSync('git commit --allow-empty -m "test"', { cwd: dir })
@@ -284,4 +288,27 @@ describe('ensureCorrectShaCheckedOut', () => {
284288
fsHelper.ensureTagAndRefCheckedOut(`refs/heads/main`, commit2, dir)
285289
).rejects.toThrow('Tag ref provided is not in expected format.')
286290
})
291+
292+
it('throws if there are untracked files in the working directory', async () => {
293+
// Add an untracked file
294+
fs.writeFileSync(`${dir}/untracked-file.txt`, fileContent)
295+
296+
await expect(async () =>
297+
fsHelper.ensureTagAndRefCheckedOut(`refs/tags/${tag2}`, commit2, dir)
298+
).rejects.toThrow(
299+
'The working directory has uncommitted changes. Uploading modified code from the checked out repository is not supported by this action.'
300+
)
301+
})
302+
303+
it('throws if there are uncommitted changes in the working directory', async () => {
304+
// Add an untracked file
305+
fs.writeFileSync(`${dir}/file1.txt`, fileContent + fileContent)
306+
execSync('git add .', { cwd: dir })
307+
308+
await expect(async () =>
309+
fsHelper.ensureTagAndRefCheckedOut(`refs/tags/${tag2}`, commit2, dir)
310+
).rejects.toThrow(
311+
'The working directory has uncommitted changes. Uploading modified code from the checked out repository is not supported by this action.'
312+
)
313+
})
287314
})

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fs-helper.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ export async function ensureTagAndRefCheckedOut(
153153
`The expected commit associated with the tag ${tagRef} is not checked out.`
154154
)
155155
}
156+
157+
// Call git status to check for any changes in the working directory
158+
// This version of this action only supports uploading actions packages
159+
// which contain the same content as the repository at the appropriate source commit.
160+
let status: simpleGit.StatusResult
161+
try {
162+
status = await git.status()
163+
} catch (err) {
164+
throw new Error(`Error checking git status: ${err}`)
165+
}
166+
if (!status.isClean()) {
167+
throw new Error(
168+
`The working directory has uncommitted changes. Uploading modified code from the checked out repository is not supported by this action.`
169+
)
170+
}
156171
}
157172

158173
// Converts a file path to a filemetadata object by querying the fs for relevant metadata.

0 commit comments

Comments
 (0)