2019-12-03 23:28:59 +08:00
|
|
|
import {IGitCommandManager} from './git-command-manager'
|
2020-05-21 23:09:16 +08:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
|
import * as github from '@actions/github'
|
2023-04-13 20:25:50 +08:00
|
|
|
import {getServerApiUrl, isGhes} from './url-helper'
|
2019-12-03 23:28:59 +08:00
|
|
|
|
2020-05-27 21:54:28 +08:00
|
|
|
export const tagsRefSpec = '+refs/tags/*:refs/tags/*'
|
|
|
|
|
|
2019-12-03 23:28:59 +08:00
|
|
|
export interface ICheckoutInfo {
|
|
|
|
|
ref: string
|
|
|
|
|
startPoint: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getCheckoutInfo(
|
|
|
|
|
git: IGitCommandManager,
|
|
|
|
|
ref: string,
|
|
|
|
|
commit: string
|
|
|
|
|
): Promise<ICheckoutInfo> {
|
|
|
|
|
if (!git) {
|
|
|
|
|
throw new Error('Arg git cannot be empty')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ref && !commit) {
|
|
|
|
|
throw new Error('Args ref and commit cannot both be empty')
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 00:04:10 +08:00
|
|
|
const result = {} as unknown as ICheckoutInfo
|
2019-12-03 23:28:59 +08:00
|
|
|
const upperRef = (ref || '').toUpperCase()
|
|
|
|
|
|
|
|
|
|
// SHA only
|
|
|
|
|
if (!ref) {
|
|
|
|
|
result.ref = commit
|
|
|
|
|
}
|
|
|
|
|
// refs/heads/
|
|
|
|
|
else if (upperRef.startsWith('REFS/HEADS/')) {
|
|
|
|
|
const branch = ref.substring('refs/heads/'.length)
|
|
|
|
|
result.ref = branch
|
|
|
|
|
result.startPoint = `refs/remotes/origin/${branch}`
|
|
|
|
|
}
|
|
|
|
|
// refs/pull/
|
|
|
|
|
else if (upperRef.startsWith('REFS/PULL/')) {
|
|
|
|
|
const branch = ref.substring('refs/pull/'.length)
|
|
|
|
|
result.ref = `refs/remotes/pull/${branch}`
|
|
|
|
|
}
|
|
|
|
|
// refs/tags/
|
|
|
|
|
else if (upperRef.startsWith('REFS/')) {
|
|
|
|
|
result.ref = ref
|
|
|
|
|
}
|
|
|
|
|
// Unqualified ref, check for a matching branch or tag
|
|
|
|
|
else {
|
|
|
|
|
if (await git.branchExists(true, `origin/${ref}`)) {
|
|
|
|
|
result.ref = ref
|
|
|
|
|
result.startPoint = `refs/remotes/origin/${ref}`
|
|
|
|
|
} else if (await git.tagExists(`${ref}`)) {
|
|
|
|
|
result.ref = `refs/tags/${ref}`
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`A branch or tag with the name '${ref}' could not be found`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 21:54:28 +08:00
|
|
|
export function getRefSpecForAllHistory(ref: string, commit: string): string[] {
|
|
|
|
|
const result = ['+refs/heads/*:refs/remotes/origin/*', tagsRefSpec]
|
|
|
|
|
if (ref && ref.toUpperCase().startsWith('REFS/PULL/')) {
|
|
|
|
|
const branch = ref.substring('refs/pull/'.length)
|
|
|
|
|
result.push(`+${commit || ref}:refs/remotes/pull/${branch}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 23:28:59 +08:00
|
|
|
export function getRefSpec(ref: string, commit: string): string[] {
|
|
|
|
|
if (!ref && !commit) {
|
|
|
|
|
throw new Error('Args ref and commit cannot both be empty')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const upperRef = (ref || '').toUpperCase()
|
|
|
|
|
|
|
|
|
|
// SHA
|
|
|
|
|
if (commit) {
|
|
|
|
|
// refs/heads
|
|
|
|
|
if (upperRef.startsWith('REFS/HEADS/')) {
|
|
|
|
|
const branch = ref.substring('refs/heads/'.length)
|
|
|
|
|
return [`+${commit}:refs/remotes/origin/${branch}`]
|
|
|
|
|
}
|
|
|
|
|
// refs/pull/
|
|
|
|
|
else if (upperRef.startsWith('REFS/PULL/')) {
|
|
|
|
|
const branch = ref.substring('refs/pull/'.length)
|
|
|
|
|
return [`+${commit}:refs/remotes/pull/${branch}`]
|
|
|
|
|
}
|
|
|
|
|
// refs/tags/
|
|
|
|
|
else if (upperRef.startsWith('REFS/TAGS/')) {
|
|
|
|
|
return [`+${commit}:${ref}`]
|
|
|
|
|
}
|
|
|
|
|
// Otherwise no destination ref
|
|
|
|
|
else {
|
|
|
|
|
return [commit]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Unqualified ref, check for a matching branch or tag
|
|
|
|
|
else if (!upperRef.startsWith('REFS/')) {
|
|
|
|
|
return [
|
|
|
|
|
`+refs/heads/${ref}*:refs/remotes/origin/${ref}*`,
|
|
|
|
|
`+refs/tags/${ref}*:refs/tags/${ref}*`
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
// refs/heads/
|
|
|
|
|
else if (upperRef.startsWith('REFS/HEADS/')) {
|
|
|
|
|
const branch = ref.substring('refs/heads/'.length)
|
|
|
|
|
return [`+${ref}:refs/remotes/origin/${branch}`]
|
|
|
|
|
}
|
|
|
|
|
// refs/pull/
|
|
|
|
|
else if (upperRef.startsWith('REFS/PULL/')) {
|
|
|
|
|
const branch = ref.substring('refs/pull/'.length)
|
|
|
|
|
return [`+${ref}:refs/remotes/pull/${branch}`]
|
|
|
|
|
}
|
|
|
|
|
// refs/tags/
|
|
|
|
|
else {
|
|
|
|
|
return [`+${ref}:${ref}`]
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-21 23:09:16 +08:00
|
|
|
|
2020-05-27 21:54:28 +08:00
|
|
|
/**
|
|
|
|
|
* Tests whether the initial fetch created the ref at the expected commit
|
|
|
|
|
*/
|
|
|
|
|
export async function testRef(
|
|
|
|
|
git: IGitCommandManager,
|
|
|
|
|
ref: string,
|
|
|
|
|
commit: string
|
|
|
|
|
): Promise<boolean> {
|
|
|
|
|
if (!git) {
|
|
|
|
|
throw new Error('Arg git cannot be empty')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ref && !commit) {
|
|
|
|
|
throw new Error('Args ref and commit cannot both be empty')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No SHA? Nothing to test
|
|
|
|
|
if (!commit) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
// SHA only?
|
|
|
|
|
else if (!ref) {
|
|
|
|
|
return await git.shaExists(commit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const upperRef = ref.toUpperCase()
|
|
|
|
|
|
|
|
|
|
// refs/heads/
|
|
|
|
|
if (upperRef.startsWith('REFS/HEADS/')) {
|
|
|
|
|
const branch = ref.substring('refs/heads/'.length)
|
|
|
|
|
return (
|
|
|
|
|
(await git.branchExists(true, `origin/${branch}`)) &&
|
|
|
|
|
commit === (await git.revParse(`refs/remotes/origin/${branch}`))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
// refs/pull/
|
|
|
|
|
else if (upperRef.startsWith('REFS/PULL/')) {
|
|
|
|
|
// Assume matches because fetched using the commit
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
// refs/tags/
|
|
|
|
|
else if (upperRef.startsWith('REFS/TAGS/')) {
|
|
|
|
|
const tagName = ref.substring('refs/tags/'.length)
|
|
|
|
|
return (
|
Fix checkout of annotated tag loosing annotation
Currently, a check is done after fetch to ensure that the repo state has
not changed since the workflow was triggered. This check will reset the
checkout to the commit that triggered the workflow, even if the branch
or tag has moved since.
The issue is that the check currently sees what "object" the ref points
to. For an annotated tag, that is the annotation, not the commit. This
means the check always fails for annotated tags, and they are reset to
the commit, losing the annotation. Losing the annotation can be fatal,
as `git describe` will only match annotated tags.
The fix is simple: check if the tag points at the right commit, ignoring
any other type of object. This is done with the <rev>^{commit} syntax.
From the git-rev-parse docs:
> <rev>^{<type>}, e.g. v0.99.8^{commit}
> A suffix ^ followed by an object type name enclosed in brace pair
> means dereference the object at <rev> recursively until an object of
> type <type> is found or the object cannot be dereferenced anymore (in
> which case, barf). For example, if <rev> is a commit-ish,
> <rev>^{commit} describes the corresponding commit object. Similarly,
> if <rev> is a tree-ish, <rev>^{tree} describes the corresponding tree
> object. <rev>^0 is a short-hand for <rev>^{commit}.
If the check still fails, we will still reset the tag to the commit,
losing the annotation. However, there is no way to truly recover in this
situtation, as GitHub does not capture the annotation on workflow start,
and since the history has changed, we can not trust the new tag to
contain the same data as it did before.
Fixes #290
Closes #697
2023-10-07 00:40:07 +08:00
|
|
|
(await git.tagExists(tagName)) &&
|
|
|
|
|
commit === (await git.revParse(`${ref}^{commit}`))
|
2020-05-27 21:54:28 +08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
// Unexpected
|
|
|
|
|
else {
|
|
|
|
|
core.debug(`Unexpected ref format '${ref}' when testing ref info`)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 23:09:16 +08:00
|
|
|
export async function checkCommitInfo(
|
|
|
|
|
token: string,
|
|
|
|
|
commitInfo: string,
|
|
|
|
|
repositoryOwner: string,
|
|
|
|
|
repositoryName: string,
|
|
|
|
|
ref: string,
|
2022-09-27 00:34:52 +08:00
|
|
|
commit: string,
|
|
|
|
|
baseUrl?: string
|
2020-05-21 23:09:16 +08:00
|
|
|
): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
// GHES?
|
2022-09-27 00:34:52 +08:00
|
|
|
if (isGhes(baseUrl)) {
|
2020-05-21 23:09:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Auth token?
|
|
|
|
|
if (!token) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Public PR synchronize, for workflow repo?
|
|
|
|
|
if (
|
|
|
|
|
fromPayload('repository.private') !== false ||
|
|
|
|
|
github.context.eventName !== 'pull_request' ||
|
|
|
|
|
fromPayload('action') !== 'synchronize' ||
|
|
|
|
|
repositoryOwner !== github.context.repo.owner ||
|
|
|
|
|
repositoryName !== github.context.repo.repo ||
|
|
|
|
|
ref !== github.context.ref ||
|
|
|
|
|
!ref.startsWith('refs/pull/') ||
|
|
|
|
|
commit !== github.context.sha
|
|
|
|
|
) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Head SHA
|
|
|
|
|
const expectedHeadSha = fromPayload('after')
|
|
|
|
|
if (!expectedHeadSha) {
|
|
|
|
|
core.debug('Unable to determine head sha')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Base SHA
|
|
|
|
|
const expectedBaseSha = fromPayload('pull_request.base.sha')
|
|
|
|
|
if (!expectedBaseSha) {
|
|
|
|
|
core.debug('Unable to determine base sha')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expected message?
|
|
|
|
|
const expectedMessage = `Merge ${expectedHeadSha} into ${expectedBaseSha}`
|
|
|
|
|
if (commitInfo.indexOf(expectedMessage) >= 0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract details from message
|
|
|
|
|
const match = commitInfo.match(/Merge ([0-9a-f]{40}) into ([0-9a-f]{40})/)
|
|
|
|
|
if (!match) {
|
|
|
|
|
core.debug('Unexpected message format')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Post telemetry
|
|
|
|
|
const actualHeadSha = match[1]
|
|
|
|
|
if (actualHeadSha !== expectedHeadSha) {
|
|
|
|
|
core.debug(
|
|
|
|
|
`Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}`
|
|
|
|
|
)
|
2023-04-12 19:55:27 +08:00
|
|
|
const octokit = github.getOctokit(token, {
|
2023-04-13 20:25:50 +08:00
|
|
|
baseUrl: getServerApiUrl(baseUrl),
|
2020-05-21 23:09:16 +08:00
|
|
|
userAgent: `actions-checkout-tracepoint/1.0 (code=STALE_MERGE;owner=${repositoryOwner};repo=${repositoryName};pr=${fromPayload(
|
|
|
|
|
'number'
|
|
|
|
|
)};run_id=${
|
|
|
|
|
process.env['GITHUB_RUN_ID']
|
|
|
|
|
};expected_head_sha=${expectedHeadSha};actual_head_sha=${actualHeadSha})`
|
|
|
|
|
})
|
2023-04-12 19:55:27 +08:00
|
|
|
await octokit.rest.repos.get({
|
|
|
|
|
owner: repositoryOwner,
|
|
|
|
|
repo: repositoryName
|
|
|
|
|
})
|
2020-05-21 23:09:16 +08:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
2021-10-19 22:52:57 +08:00
|
|
|
core.debug(
|
|
|
|
|
`Error when validating commit info: ${(err as any)?.stack ?? err}`
|
|
|
|
|
)
|
2020-05-21 23:09:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fromPayload(path: string): any {
|
|
|
|
|
return select(github.context.payload, path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function select(obj: any, path: string): any {
|
|
|
|
|
if (!obj) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const i = path.indexOf('.')
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
return obj[path]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const key = path.substr(0, i)
|
|
|
|
|
return select(obj[key], path.substr(i + 1))
|
|
|
|
|
}
|