Compare commits

...

11 Commits

Author SHA1 Message Date
James Bradlee
05a4ba51fe
Merge 0865c4bfce into 900f2210b1 2026-05-04 19:22:27 -05:00
Yashwanth Anantharaju
900f2210b1
fix: expand merge commit SHA regex and add SHA-256 test cases (#2414)
Some checks failed
Check dist / check-dist (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Licensed / Check licenses (push) Has been cancelled
Build and Test / build (push) Has been cancelled
Build and Test / test (macos-latest) (push) Has been cancelled
Build and Test / test (ubuntu-latest) (push) Has been cancelled
Build and Test / test (windows-latest) (push) Has been cancelled
Build and Test / test-proxy (push) Has been cancelled
Build and Test / test-bypass-proxy (push) Has been cancelled
Build and Test / test-git-container (push) Has been cancelled
Build and Test / test-output (push) Has been cancelled
* fix: expand merge commit SHA regex and add SHA-256 test cases

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* test: add checkCommitInfo SHA coverage

Add checkCommitInfo tests for SHA-1 and SHA-256 merge messages and reject invalid 50-character hex merge heads.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* style: fix Prettier formatting in test and source files

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-04 13:30:55 -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
7 changed files with 211 additions and 6 deletions

View File

@ -63,6 +63,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

@ -133,6 +133,16 @@ describe('input-helper tests', () => {
expect(settings.commit).toBe('1111111111222222222233333333334444444444')
})
it('sets ref to empty when explicit sha-256', async () => {
inputs.ref =
'1111111111222222222233333333334444444444555555555566666666667777'
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.ref).toBeFalsy()
expect(settings.commit).toBe(
'1111111111222222222233333333334444444444555555555566666666667777'
)
})
it('sets sha to empty when explicit ref', async () => {
inputs.ref = 'refs/heads/some-other-ref'
const settings: IGitSourceSettings = await inputHelper.getInputs()
@ -144,4 +154,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

@ -1,8 +1,12 @@
import * as assert from 'assert'
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as refHelper from '../lib/ref-helper'
import {IGitCommandManager} from '../lib/git-command-manager'
const commit = '1234567890123456789012345678901234567890'
const sha256Commit =
'1234567890123456789012345678901234567890123456789012345678901234'
let git: IGitCommandManager
describe('ref-helper tests', () => {
@ -37,6 +41,12 @@ describe('ref-helper tests', () => {
expect(checkoutInfo.startPoint).toBeFalsy()
})
it('getCheckoutInfo sha-256 only', async () => {
const checkoutInfo = await refHelper.getCheckoutInfo(git, '', sha256Commit)
expect(checkoutInfo.ref).toBe(sha256Commit)
expect(checkoutInfo.startPoint).toBeFalsy()
})
it('getCheckoutInfo refs/heads/', async () => {
const checkoutInfo = await refHelper.getCheckoutInfo(
git,
@ -227,4 +237,142 @@ describe('ref-helper tests', () => {
'+refs/heads/my/branch:refs/remotes/origin/my/branch'
)
})
describe('checkCommitInfo', () => {
const repositoryOwner = 'some-owner'
const repositoryName = 'some-repo'
const ref = 'refs/pull/123/merge'
const sha1Head = '1111111111222222222233333333334444444444'
const sha1Base = 'aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd'
const sha256Head =
'1111111111222222222233333333334444444444555555555566666666667777'
const sha256Base =
'aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffff0000'
let debugSpy: jest.SpyInstance
let getOctokitSpy: jest.SpyInstance
let repoGetSpy: jest.Mock
let originalEventName: string
let originalPayload: unknown
let originalRef: string
let originalSha: string
function setPullRequestContext(
expectedHeadSha: string,
expectedBaseSha: string,
mergeCommit: string
): void {
;(github.context as any).eventName = 'pull_request'
github.context.ref = ref
github.context.sha = mergeCommit
;(github.context as any).payload = {
action: 'synchronize',
after: expectedHeadSha,
number: 123,
pull_request: {
base: {
sha: expectedBaseSha
}
},
repository: {
private: false
}
}
}
beforeEach(() => {
originalEventName = github.context.eventName
originalPayload = github.context.payload
originalRef = github.context.ref
originalSha = github.context.sha
jest.spyOn(github.context, 'repo', 'get').mockReturnValue({
owner: repositoryOwner,
repo: repositoryName
})
debugSpy = jest.spyOn(core, 'debug').mockImplementation(jest.fn())
repoGetSpy = jest.fn(async () => ({}))
getOctokitSpy = jest.spyOn(github, 'getOctokit').mockReturnValue({
rest: {
repos: {
get: repoGetSpy
}
}
} as any)
})
afterEach(() => {
;(github.context as any).eventName = originalEventName
;(github.context as any).payload = originalPayload
github.context.ref = originalRef
github.context.sha = originalSha
jest.restoreAllMocks()
})
it('returns early for SHA-1 merge commit', async () => {
setPullRequestContext(sha1Head, sha1Base, commit)
await refHelper.checkCommitInfo(
'token',
`Merge ${sha1Head} into ${sha1Base}`,
repositoryOwner,
repositoryName,
ref,
commit
)
expect(getOctokitSpy).not.toHaveBeenCalled()
expect(repoGetSpy).not.toHaveBeenCalled()
})
it('matches SHA-256 merge commit info', async () => {
const actualHeadSha =
'9999999999888888888877777777776666666666555555555544444444443333'
setPullRequestContext(sha256Head, sha256Base, sha256Commit)
await refHelper.checkCommitInfo(
'token',
`Merge ${actualHeadSha} into ${sha256Base}`,
repositoryOwner,
repositoryName,
ref,
sha256Commit
)
expect(getOctokitSpy).toHaveBeenCalledWith(
'token',
expect.objectContaining({
userAgent: expect.stringContaining(
`expected_head_sha=${sha256Head};actual_head_sha=${actualHeadSha}`
)
})
)
expect(repoGetSpy).toHaveBeenCalledWith({
owner: repositoryOwner,
repo: repositoryName
})
expect(debugSpy).toHaveBeenCalledWith(
`Expected head sha ${sha256Head}; actual head sha ${actualHeadSha}`
)
expect(debugSpy).not.toHaveBeenCalledWith('Unexpected message format')
})
it('does not match 50-char hex as a valid merge', async () => {
const invalidHeadSha =
'99999999998888888888777777777766666666665555555555'
setPullRequestContext(sha1Head, sha1Base, commit)
await refHelper.checkCommitInfo(
'token',
`Merge ${invalidHeadSha} into ${sha1Base}`,
repositoryOwner,
repositoryName,
ref,
commit
)
expect(getOctokitSpy).not.toHaveBeenCalled()
expect(repoGetSpy).not.toHaveBeenCalled()
expect(debugSpy).toHaveBeenCalledWith('Unexpected message format')
})
})
})

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

10
dist/index.js vendored
View File

@ -2008,7 +2008,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;
@ -2021,7 +2025,7 @@ function getInputs() {
}
}
// SHA?
else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
else if (result.ref.match(/^(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/)) {
result.commit = result.ref;
result.ref = '';
}
@ -2444,7 +2448,7 @@ function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref
return;
}
// Extract details from message
const match = commitInfo.match(/Merge ([0-9a-f]{40}) into ([0-9a-f]{40})/);
const match = commitInfo.match(/Merge ([0-9a-f]{40}|[0-9a-f]{64}) into ([0-9a-f]{40}|[0-9a-f]{64})/);
if (!match) {
core.debug('Unexpected message format');
return;

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
@ -71,7 +76,7 @@ export async function getInputs(): Promise<IGitSourceSettings> {
}
}
// SHA?
else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
else if (result.ref.match(/^(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/)) {
result.commit = result.ref
result.ref = ''
}

View File

@ -258,7 +258,9 @@ export async function checkCommitInfo(
}
// Extract details from message
const match = commitInfo.match(/Merge ([0-9a-f]{40}) into ([0-9a-f]{40})/)
const match = commitInfo.match(
/Merge ([0-9a-f]{40}|[0-9a-f]{64}) into ([0-9a-f]{40}|[0-9a-f]{64})/
)
if (!match) {
core.debug('Unexpected message format')
return