From e8e821983d056fcaf39cfc95ceafaa097da37e6f Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:31:54 +0000 Subject: [PATCH] `utl-helper.ts` now leverages well-known environment variables. --- dist/index.js | 27 +++++++++++++-------------- src/github-api-helper.ts | 4 ++-- src/ref-helper.ts | 4 ++-- src/url-helper.ts | 24 ++++++++++++------------ 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/dist/index.js b/dist/index.js index d86415e..1791daa 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1617,7 +1617,7 @@ function getDefaultBranch(authToken, owner, repo, baseUrl) { return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { core.info('Retrieving the default branch name'); const octokit = github.getOctokit(authToken, { - baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl) + baseUrl: (0, url_helper_1.getServerApiUrl)() }); let result; try { @@ -1650,7 +1650,7 @@ function getDefaultBranch(authToken, owner, repo, baseUrl) { function downloadArchive(authToken, owner, repo, ref, commit, baseUrl) { return __awaiter(this, void 0, void 0, function* () { const octokit = github.getOctokit(authToken, { - baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl) + baseUrl: (0, url_helper_1.getServerApiUrl)() }); const download = IS_WINDOWS ? octokit.rest.repos.downloadZipballArchive @@ -2128,7 +2128,7 @@ function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref var _a; try { // GHES? - if ((0, url_helper_1.isGhes)(baseUrl)) { + if ((0, url_helper_1.isGhes)()) { return; } // Auth token? @@ -2174,7 +2174,7 @@ function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref if (actualHeadSha !== expectedHeadSha) { core.debug(`Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}`); const octokit = github.getOctokit(token, { - baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl), + baseUrl: (0, url_helper_1.getServerApiUrl)(), 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})` }); yield octokit.rest.repos.get({ @@ -2459,17 +2459,16 @@ function getServerUrl(url) { : process.env['GITHUB_SERVER_URL'] || 'https://github.com'; return new url_1.URL(urlValue); } -function getServerApiUrl(url) { - let apiUrl = 'https://api.github.com'; - if (isGhes(url)) { - const serverUrl = getServerUrl(url); - apiUrl = new url_1.URL(`${serverUrl.origin}/api/v3`).toString(); - } - return apiUrl; +function getServerApiUrl() { + return process.env['GITHUB_API_URL'] || 'https://api.github.com'; } -function isGhes(url) { - const ghUrl = getServerUrl(url); - return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; +function isGhes() { + const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); + const hostname = ghUrl.hostname.trimEnd().toUpperCase(); + const isGitHubHost = hostname === 'GITHUB.COM'; + const isGheHost = hostname.endsWith('.GHE.COM'); + const isLocalHost = hostname.endsWith('.LOCALHOST'); + return !isGitHubHost && !isGheHost && !isLocalHost; } diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 1ff27c2..5df59f3 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -88,7 +88,7 @@ export async function getDefaultBranch( return await retryHelper.execute(async () => { core.info('Retrieving the default branch name') const octokit = github.getOctokit(authToken, { - baseUrl: getServerApiUrl(baseUrl) + baseUrl: getServerApiUrl() }) let result: string try { @@ -131,7 +131,7 @@ async function downloadArchive( baseUrl?: string ): Promise { const octokit = github.getOctokit(authToken, { - baseUrl: getServerApiUrl(baseUrl) + baseUrl: getServerApiUrl() }) const download = IS_WINDOWS ? octokit.rest.repos.downloadZipballArchive diff --git a/src/ref-helper.ts b/src/ref-helper.ts index 58f9290..0de96ae 100644 --- a/src/ref-helper.ts +++ b/src/ref-helper.ts @@ -192,7 +192,7 @@ export async function checkCommitInfo( ): Promise { try { // GHES? - if (isGhes(baseUrl)) { + if (isGhes()) { return } @@ -249,7 +249,7 @@ export async function checkCommitInfo( `Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}` ) const octokit = github.getOctokit(token, { - baseUrl: getServerApiUrl(baseUrl), + baseUrl: getServerApiUrl(), userAgent: `actions-checkout-tracepoint/1.0 (code=STALE_MERGE;owner=${repositoryOwner};repo=${repositoryName};pr=${fromPayload( 'number' )};run_id=${ diff --git a/src/url-helper.ts b/src/url-helper.ts index 64ecbf3..5ae9ebe 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -28,19 +28,19 @@ export function getServerUrl(url?: string): URL { return new URL(urlValue) } -export function getServerApiUrl(url?: string): string { - let apiUrl = 'https://api.github.com' - - if (isGhes(url)) { - const serverUrl = getServerUrl(url) - apiUrl = new URL(`${serverUrl.origin}/api/v3`).toString() - } - - return apiUrl +export function getServerApiUrl(): string { + return process.env['GITHUB_API_URL'] || 'https://api.github.com' } -export function isGhes(url?: string): boolean { - const ghUrl = getServerUrl(url) +export function isGhes(): boolean { + const ghUrl = new URL( + process.env['GITHUB_SERVER_URL'] || 'https://github.com' + ) - return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM' + const hostname = ghUrl.hostname.trimEnd().toUpperCase() + const isGitHubHost = hostname === 'GITHUB.COM' + const isGheHost = hostname.endsWith('.GHE.COM') + const isLocalHost = hostname.endsWith('.LOCALHOST') + + return !isGitHubHost && !isGheHost && !isLocalHost }