Compare commits

...

11 Commits

Author SHA1 Message Date
James Bradlee
1546f88972
Merge 0865c4bfce into b684943689 2024-09-05 11:11:04 -07:00
Luca Comellini
b684943689
Add Ref and Commit outputs (#1180)
Signed-off-by: Luca Comellini <luca.com@gmail.com>
2024-09-05 11:57:13 -04:00
James Bradlee
0865c4bfce
must use || and not ?? when falling back to commit when ref is not provided.
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 17:48:27 +02:00
James Bradlee
1be0f9404c
builds updates dist
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:59:28 +02:00
James Bradlee
a52fa92dc9
build updates docs
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:59:18 +02:00
James Bradlee
491fae084d
format input-helper
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:58:15 +02:00
James Bradlee
3a6c8fb5e6
added tests
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:54:17 +02:00
James Bradlee
267ca9cee1
in input-helper, add validation to commit input
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:49:44 +02:00
James Bradlee
8a241b5b4d
in input-helper, make ref fallback to commit if the commit was provided but not ref
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:46:49 +02:00
James Bradlee
67b5caa109
in input-helper, set commit = core.getInput('commit')
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:46:15 +02:00
James Bradlee
650ceb06a8
added commit input in action.yaml
Signed-off-by: James Bradlee <james.bradlee@telenor.no>
2024-08-20 10:43:19 +02:00
8 changed files with 92 additions and 4 deletions

View File

@ -295,3 +295,37 @@ jobs:
uses: actions/checkout@v4.1.6
with:
path: localClone
test-output:
runs-on: ubuntu-latest
steps:
# Clone this repo
- name: Checkout
uses: actions/checkout@v4.1.6
# Basic checkout using git
- name: Checkout basic
id: checkout
uses: ./
with:
ref: test-data/v2/basic
# Verify output
- name: Verify output
run: |
echo "Commit: ${{ steps.checkout.outputs.commit }}"
echo "Ref: ${{ steps.checkout.outputs.ref }}"
if [ "${{ steps.checkout.outputs.ref }}" != "test-data/v2/basic" ]; then
echo "Expected ref to be test-data/v2/basic"
exit 1
fi
if [ "${{ steps.checkout.outputs.commit }}" != "82f71901cf8c021332310dcc8cdba84c4193ff5d" ]; then
echo "Expected commit to be 82f71901cf8c021332310dcc8cdba84c4193ff5d"
exit 1
fi
# needed to make checkout post cleanup succeed
- name: Fix Checkout
uses: actions/checkout@v4.1.6

View File

@ -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.

View File

@ -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'
)
})
})

View File

@ -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
@ -98,6 +103,11 @@ inputs:
github-server-url:
description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com
required: false
outputs:
ref:
description: 'The branch, tag or SHA that was checked out'
commit:
description: 'The commit SHA that was checked out'
runs:
using: node20
main: dist/index.js

10
dist/index.js vendored
View File

@ -1355,7 +1355,8 @@ function getSource(settings) {
// Get commit information
const commitInfo = yield git.log1();
// Log commit sha
yield git.log1("--format='%H'");
const commitSHA = yield git.log1('--format=%H');
core.setOutput('commit', commitSHA.trim());
// Check for incorrect pull request merge commit
yield refHelper.checkCommitInfo(settings.authToken, commitInfo, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.githubServerUrl);
}
@ -1748,7 +1749,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;
@ -1897,6 +1902,7 @@ function run() {
coreCommand.issueCommand('add-matcher', {}, path.join(__dirname, 'problem-matcher.json'));
// Get sources
yield gitSourceProvider.getSource(sourceSettings);
core.setOutput('ref', sourceSettings.ref);
}
finally {
// Unregister problem matcher

View File

@ -261,7 +261,8 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
const commitInfo = await git.log1()
// Log commit sha
await git.log1("--format='%H'")
const commitSHA = await git.log1('--format=%H')
core.setOutput('commit', commitSHA.trim())
// Check for incorrect pull request merge commit
await refHelper.checkCommitInfo(

View File

@ -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

View File

@ -19,6 +19,7 @@ async function run(): Promise<void> {
// Get sources
await gitSourceProvider.getSource(sourceSettings)
core.setOutput('ref', sourceSettings.ref)
} finally {
// Unregister problem matcher
coreCommand.issueCommand('remove-matcher', {owner: 'checkout-git'}, '')