From eb9dcad7e007cb96364b4e612799ce0ff736cb09 Mon Sep 17 00:00:00 2001 From: Yashwanth Anantharaju Date: Mon, 1 Jun 2026 11:35:58 -0400 Subject: Fix checkout init for SHA-256 repositories (#2439) * Fix checkout init for SHA-256 repositories * Remove unused object format result field --- src/github-api-helper.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/github-api-helper.ts') diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 1ff27c2..bef31e6 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -11,6 +11,11 @@ import {getServerApiUrl} from './url-helper' const IS_WINDOWS = process.platform === 'win32' +export interface RepositoryObjectFormatResult { + format: string + succeeded: boolean +} + export async function downloadRepository( authToken: string, owner: string, @@ -122,6 +127,53 @@ export async function getDefaultBranch( }) } +export async function tryGetRepositoryObjectFormat( + authToken: string, + owner: string, + repo: string, + baseUrl?: string, + commit?: string +): Promise { + const commitFormat = getObjectFormat(commit) + if (commitFormat) { + return {format: commitFormat, succeeded: true} + } + + try { + const octokit = github.getOctokit(authToken, { + baseUrl: getServerApiUrl(baseUrl) + }) + const response = await octokit.request( + 'GET /repos/{owner}/{repo}/hash-algorithm', + {owner, repo} + ) + const hashAlgorithm = response.data.hash_algorithm + if (hashAlgorithm === 'sha256' || hashAlgorithm === 'sha1') { + return {format: hashAlgorithm, succeeded: true} + } + + core.debug( + 'Unable to determine repository object format from hash-algorithm endpoint' + ) + return {format: '', succeeded: false} + } catch (err) { + core.debug( + `Unable to determine repository object format from hash-algorithm endpoint: ${(err as any)?.message ?? err}` + ) + return {format: '', succeeded: false} + } +} + +function getObjectFormat(sha?: string): string { + if (/^[0-9a-fA-F]{64}$/.test(sha || '')) { + return 'sha256' + } + if (/^[0-9a-fA-F]{40}$/.test(sha || '')) { + return 'sha1' + } + return '' +} + async function downloadArchive( authToken: string, owner: string, -- cgit v1.2.3