report the resolved commit when a ref is downloaded without a SHA

On the REST API fallback, a ref given without a SHA (a branch or tag of
another repository, or the default branch) left the commit output empty: the
archive was downloaded by ref and nothing recorded which commit that was.

downloadRepository now resolves the ref to its commit first
(GET /repos/{owner}/{repo}/commits/{ref} with the sha media type), downloads
that commit, and returns its SHA, which getSource sets as the output. The
downloaded content and the reported SHA are the same commit even if the ref
moves meanwhile. If the ref cannot be resolved, the download falls back to
the ref as before, a warning is logged, and the output stays empty.
This commit is contained in:
askalf 2026-09-28 15:38:49 -04:00
parent 56a41f36e5
commit dbd3a6024b
5 changed files with 170 additions and 13 deletions

36
dist/index.js vendored
View file

@ -41324,6 +41324,11 @@ async function downloadRepository(authToken, owner, repo, ref, commit, repositor
info('Determining the default branch');
ref = await getDefaultBranch(authToken, owner, repo, baseUrl);
}
// Without a commit, resolve the ref to one and download that commit, so the
// content and the returned SHA agree even if the ref moves in the meantime.
if (!commit) {
commit = await tryGetCommitSha(authToken, owner, repo, ref, baseUrl);
}
// Download the archive
let archiveData = await execute(async () => {
info('Downloading the archive');
@ -41367,6 +41372,30 @@ async function downloadRepository(authToken, owner, repo, ref, commit, repositor
}
}
await rmRF(extractPath);
return commit;
}
/**
* Resolves a ref to the SHA of the commit it points at. Returns an empty string
* when the ref cannot be resolved, so the download falls back to the ref itself.
*/
async function tryGetCommitSha(authToken, owner, repo, ref, baseUrl) {
try {
return await execute(async () => {
info(`Resolving the commit for '${ref}'`);
const octokit = getOctokit(authToken, {
baseUrl: getServerApiUrl(baseUrl)
});
const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{ref}', { owner, repo, ref, headers: { accept: 'application/vnd.github.sha' } });
const sha = String(response.data).trim();
external_assert_.ok(/^([0-9a-f]{40}|[0-9a-f]{64})$/.test(sha), `Unexpected commit SHA '${sha}'`);
info(`Resolved '${ref}' to ${sha}`);
return sha;
});
}
catch (err) {
warning(`Unable to resolve the commit for '${ref}': ${err?.message ?? err}`);
return '';
}
}
/**
* Looks up the default branch name
@ -41754,10 +41783,11 @@ async function getSource(settings) {
else if (settings.sshKey) {
throw new Error(`Input 'ssh-key' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${MinimumGitVersion} or higher to the PATH.`);
}
await downloadRepository(settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.repositoryPath, settings.githubServerUrl);
const downloadedCommit = await downloadRepository(settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.repositoryPath, settings.githubServerUrl);
// Set the commit output. The REST API fallback does not create a local
// Git repository, so the SHA can only come from the resolved settings.
setOutput('commit', settings.commit);
// Git repository, so the SHA is the commit that was downloaded: the one
// requested, or the one the ref resolved to.
setOutput('commit', downloadedCommit);
return;
}
// Save state for POST action