Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feature: add support for commit input (fixes #1634) #1858

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# Otherwise, uses the default branch.
ref: ''

# The commit SHA to checkout. Used when ref is not specified or is ambiguous. This
# can be used as a replacement for ref, or alongside it to checkout a specific
# commit of the ref.
commit: ''

# Personal access token (PAT) used to fetch the repository. The PAT is configured
# with the local git config, which enables your scripts to run authenticated git
# commands. The post-job step removes the PAT.
Expand Down
26 changes: 26 additions & 0 deletions __test__/input-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,30 @@ describe('input-helper tests', () => {
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.workflowOrganizationId).toBe(123456)
})

it('accepts ref and commit', async () => {
inputs.ref = 'refs/pull/123/merge'
inputs.commit = '0123456789012345678901234567890123456789'
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings).toBeTruthy()
expect(settings.ref).toBeTruthy()
expect(settings.ref).toStrictEqual('refs/pull/123/merge')
expect(settings.commit).toBeTruthy()
expect(settings.commit).toStrictEqual(
'0123456789012345678901234567890123456789'
)
})

it('ref fallbacks to commit if ref is empty but commit is specified', async () => {
inputs.ref = ''
inputs.commit = '0123456789012345678901234567890123456789'
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings).toBeTruthy()
expect(settings.ref).toBeFalsy()
expect(settings.ref).toStrictEqual('')
expect(settings.commit).toBeTruthy()
expect(settings.commit).toStrictEqual(
'0123456789012345678901234567890123456789'
)
})
})
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ inputs:
The branch, tag or SHA to checkout. When checking out the repository that
triggered a workflow, this defaults to the reference or SHA for that
event. Otherwise, uses the default branch.
commit:
description: >
The commit SHA to checkout. Used when ref is not specified or is ambiguous.
This can be used as a replacement for ref, or alongside it to checkout a
specific commit of the ref.
token:
description: >
Personal access token (PAT) used to fetch the repository. The PAT is configured
Expand Down
6 changes: 5 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,11 @@ function getInputs() {
const isWorkflowRepository = qualifiedRepository.toUpperCase() ===
`${github.context.repo.owner}/${github.context.repo.repo}`.toUpperCase();
// Source branch, source version
result.ref = core.getInput('ref');
result.commit = core.getInput('commit');
if (result.commit && !result.commit.match(/^[0-9a-fA-F]{40}$/)) {
throw new Error(`The commit SHA '${result.commit}' is not a valid SHA.`);
}
result.ref = core.getInput('ref') || result.commit;
if (!result.ref) {
if (isWorkflowRepository) {
result.ref = github.context.ref;
Expand Down
7 changes: 6 additions & 1 deletion src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ export async function getInputs(): Promise<IGitSourceSettings> {
`${github.context.repo.owner}/${github.context.repo.repo}`.toUpperCase()

// Source branch, source version
result.ref = core.getInput('ref')
result.commit = core.getInput('commit')
if (result.commit && !result.commit.match(/^[0-9a-fA-F]{40}$/)) {
throw new Error(`The commit SHA '${result.commit}' is not a valid SHA.`)
}

result.ref = core.getInput('ref') || result.commit
if (!result.ref) {
if (isWorkflowRepository) {
result.ref = github.context.ref
Expand Down