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 01/11] `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 } From df4b58c7899c5e7a9a371c4092d97c3a15c0a2cb Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:29:39 +0000 Subject: [PATCH 02/11] second attempt --- __test__/url-helper.test.ts | 23 +++++++++++++++++++ src/url-helper.ts | 45 ++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 __test__/url-helper.test.ts diff --git a/__test__/url-helper.test.ts b/__test__/url-helper.test.ts new file mode 100644 index 0000000..e3e810b --- /dev/null +++ b/__test__/url-helper.test.ts @@ -0,0 +1,23 @@ +import * as urlHelper from '../src/url-helper' + +describe('isGhes tests', () => { + it('basics', async () => { + expect(urlHelper.isGhes()).toBeFalsy() + expect(urlHelper.isGhes('https://github.com')).toBeFalsy() + //expect(urlHelper.isGhes('https://api.github.com')).toBeFalsy() + expect(urlHelper.isGhes('https://europe.ghe.com')).toBeFalsy() + expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy() + expect(urlHelper.isGhes('https://src.onpremise.customer.com')).toBeTruthy() + }) +}) + +describe('getServerApiUrl tests', () => { + it('basics', async () => { + expect(urlHelper.getServerApiUrl()).toBe('https://api.github.com') + expect(urlHelper.getServerApiUrl('https://github.com')).toBe('https://api.github.com') + expect(urlHelper.getServerApiUrl('https://GitHub.com')).toBe('https://api.github.com') + expect(urlHelper.getServerApiUrl('https://europe.ghe.com')).toBe('https://api.europe.ghe.com') + expect(urlHelper.getServerApiUrl('https://australia.GHE.COM')).toBe('https://api.australia.ghe.com') + expect(urlHelper.getServerApiUrl('https://src.onpremise.customer.com')).toBe('https://src.onpremise.customer.com/api/v3') + }) +}) diff --git a/src/url-helper.ts b/src/url-helper.ts index 5ae9ebe..597021d 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -21,20 +21,32 @@ export function getFetchUrl(settings: IGitSourceSettings): string { } export function getServerUrl(url?: string): URL { - let urlValue = - url && url.trim().length > 0 - ? url - : process.env['GITHUB_SERVER_URL'] || 'https://github.com' - return new URL(urlValue) + let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com' + if (hasContent(url, false)) { + resolvedUrl = url! + } + + return new URL(resolvedUrl) } -export function getServerApiUrl(): string { +export function getServerApiUrl(url?: string): string { + if (hasContent(url, false)) { + let serverUrl = getServerUrl(url) + if (isGhes(url)) { + serverUrl.pathname = "api/v3" + } else { + serverUrl.hostname = "api." + serverUrl.hostname + } + + return pruneSuffix(serverUrl.toString(), '/') + } + return process.env['GITHUB_API_URL'] || 'https://api.github.com' } -export function isGhes(): boolean { +export function isGhes(url?: string): boolean { const ghUrl = new URL( - process.env['GITHUB_SERVER_URL'] || 'https://github.com' + url || process.env['GITHUB_SERVER_URL'] || 'https://github.com' ) const hostname = ghUrl.hostname.trimEnd().toUpperCase() @@ -44,3 +56,20 @@ export function isGhes(): boolean { return !isGitHubHost && !isGheHost && !isLocalHost } + + +function pruneSuffix(text: string, suffix: string) { + if (hasContent(suffix, true) && text?.endsWith(suffix)) { + return text.substring(0, text.length - suffix.length) + } + return text +} + +function hasContent(text: string | undefined, allowPureWhitespace: boolean): boolean { + let refinedText = text ?? "" + if (!allowPureWhitespace) { + refinedText = refinedText.trim() + } + return refinedText.length > 0 +} + From 2f42443ddaaf0277464417de30a81183e5b91981 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:35:40 +0000 Subject: [PATCH 03/11] restore passing `baseUrl` where appropriate --- dist/index.js | 46 ++++++++++++++++++++++++++++++---------- src/github-api-helper.ts | 4 ++-- src/ref-helper.ts | 4 ++-- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/dist/index.js b/dist/index.js index 1791daa..8cfb1d5 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: (0, url_helper_1.getServerApiUrl)(baseUrl) }); 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: (0, url_helper_1.getServerApiUrl)(baseUrl) }); 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)()) { + if ((0, url_helper_1.isGhes)(baseUrl)) { 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: (0, url_helper_1.getServerApiUrl)(baseUrl), 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({ @@ -2454,22 +2454,46 @@ function getFetchUrl(settings) { return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`; } function getServerUrl(url) { - let urlValue = url && url.trim().length > 0 - ? url - : process.env['GITHUB_SERVER_URL'] || 'https://github.com'; - return new url_1.URL(urlValue); + let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; + if (hasContent(url, false)) { + resolvedUrl = url; + } + return new url_1.URL(resolvedUrl); } -function getServerApiUrl() { +function getServerApiUrl(url) { + if (hasContent(url, false)) { + let serverUrl = getServerUrl(url); + if (isGhes(url)) { + serverUrl.pathname = "api/v3"; + } + else { + serverUrl.hostname = "api." + serverUrl.hostname; + } + return pruneSuffix(serverUrl.toString(), '/'); + } return process.env['GITHUB_API_URL'] || 'https://api.github.com'; } -function isGhes() { - const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); +function isGhes(url) { + const ghUrl = new url_1.URL(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; } +function pruneSuffix(text, suffix) { + if (hasContent(suffix, true) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) { + return text.substring(0, text.length - suffix.length); + } + return text; +} +function hasContent(text, allowPureWhitespace) { + let refinedText = text !== null && text !== void 0 ? text : ""; + if (!allowPureWhitespace) { + refinedText = refinedText.trim(); + } + return refinedText.length > 0; +} /***/ }), diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 5df59f3..1ff27c2 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: getServerApiUrl(baseUrl) }) let result: string try { @@ -131,7 +131,7 @@ async function downloadArchive( baseUrl?: string ): Promise { const octokit = github.getOctokit(authToken, { - baseUrl: getServerApiUrl() + baseUrl: getServerApiUrl(baseUrl) }) const download = IS_WINDOWS ? octokit.rest.repos.downloadZipballArchive diff --git a/src/ref-helper.ts b/src/ref-helper.ts index 0de96ae..58f9290 100644 --- a/src/ref-helper.ts +++ b/src/ref-helper.ts @@ -192,7 +192,7 @@ export async function checkCommitInfo( ): Promise { try { // GHES? - if (isGhes()) { + if (isGhes(baseUrl)) { 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: getServerApiUrl(baseUrl), userAgent: `actions-checkout-tracepoint/1.0 (code=STALE_MERGE;owner=${repositoryOwner};repo=${repositoryName};pr=${fromPayload( 'number' )};run_id=${ From 7fcbcdce7d7b3a50a59e1b8a70636605230bfbf5 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:15:26 +0000 Subject: [PATCH 04/11] ran `npm run format` --- __test__/url-helper.test.ts | 20 +++++++++++++++----- src/url-helper.ts | 13 +++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/__test__/url-helper.test.ts b/__test__/url-helper.test.ts index e3e810b..65773b9 100644 --- a/__test__/url-helper.test.ts +++ b/__test__/url-helper.test.ts @@ -14,10 +14,20 @@ describe('isGhes tests', () => { describe('getServerApiUrl tests', () => { it('basics', async () => { expect(urlHelper.getServerApiUrl()).toBe('https://api.github.com') - expect(urlHelper.getServerApiUrl('https://github.com')).toBe('https://api.github.com') - expect(urlHelper.getServerApiUrl('https://GitHub.com')).toBe('https://api.github.com') - expect(urlHelper.getServerApiUrl('https://europe.ghe.com')).toBe('https://api.europe.ghe.com') - expect(urlHelper.getServerApiUrl('https://australia.GHE.COM')).toBe('https://api.australia.ghe.com') - expect(urlHelper.getServerApiUrl('https://src.onpremise.customer.com')).toBe('https://src.onpremise.customer.com/api/v3') + expect(urlHelper.getServerApiUrl('https://github.com')).toBe( + 'https://api.github.com' + ) + expect(urlHelper.getServerApiUrl('https://GitHub.com')).toBe( + 'https://api.github.com' + ) + expect(urlHelper.getServerApiUrl('https://europe.ghe.com')).toBe( + 'https://api.europe.ghe.com' + ) + expect(urlHelper.getServerApiUrl('https://australia.GHE.COM')).toBe( + 'https://api.australia.ghe.com' + ) + expect( + urlHelper.getServerApiUrl('https://src.onpremise.customer.com') + ).toBe('https://src.onpremise.customer.com/api/v3') }) }) diff --git a/src/url-helper.ts b/src/url-helper.ts index 597021d..b12765c 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -33,9 +33,9 @@ export function getServerApiUrl(url?: string): string { if (hasContent(url, false)) { let serverUrl = getServerUrl(url) if (isGhes(url)) { - serverUrl.pathname = "api/v3" + serverUrl.pathname = 'api/v3' } else { - serverUrl.hostname = "api." + serverUrl.hostname + serverUrl.hostname = 'api.' + serverUrl.hostname } return pruneSuffix(serverUrl.toString(), '/') @@ -57,7 +57,6 @@ export function isGhes(url?: string): boolean { return !isGitHubHost && !isGheHost && !isLocalHost } - function pruneSuffix(text: string, suffix: string) { if (hasContent(suffix, true) && text?.endsWith(suffix)) { return text.substring(0, text.length - suffix.length) @@ -65,11 +64,13 @@ function pruneSuffix(text: string, suffix: string) { return text } -function hasContent(text: string | undefined, allowPureWhitespace: boolean): boolean { - let refinedText = text ?? "" +function hasContent( + text: string | undefined, + allowPureWhitespace: boolean +): boolean { + let refinedText = text ?? '' if (!allowPureWhitespace) { refinedText = refinedText.trim() } return refinedText.length > 0 } - From 19060a122d0c2f1ca421f732a370ce68d2fc33de Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:18:09 +0000 Subject: [PATCH 05/11] ran `npm run build` --- dist/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8cfb1d5..652ff1d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2464,10 +2464,10 @@ function getServerApiUrl(url) { if (hasContent(url, false)) { let serverUrl = getServerUrl(url); if (isGhes(url)) { - serverUrl.pathname = "api/v3"; + serverUrl.pathname = 'api/v3'; } else { - serverUrl.hostname = "api." + serverUrl.hostname; + serverUrl.hostname = 'api.' + serverUrl.hostname; } return pruneSuffix(serverUrl.toString(), '/'); } @@ -2488,7 +2488,7 @@ function pruneSuffix(text, suffix) { return text; } function hasContent(text, allowPureWhitespace) { - let refinedText = text !== null && text !== void 0 ? text : ""; + let refinedText = text !== null && text !== void 0 ? text : ''; if (!allowPureWhitespace) { refinedText = refinedText.trim(); } From 813c3f2d8248599f6bf278179244986d567ba4e6 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:49:31 +0200 Subject: [PATCH 06/11] Apply suggestions from @easyt Co-authored-by: Erez Testiler --- __test__/url-helper.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/__test__/url-helper.test.ts b/__test__/url-helper.test.ts index 65773b9..53b6671 100644 --- a/__test__/url-helper.test.ts +++ b/__test__/url-helper.test.ts @@ -5,7 +5,7 @@ describe('isGhes tests', () => { expect(urlHelper.isGhes()).toBeFalsy() expect(urlHelper.isGhes('https://github.com')).toBeFalsy() //expect(urlHelper.isGhes('https://api.github.com')).toBeFalsy() - expect(urlHelper.isGhes('https://europe.ghe.com')).toBeFalsy() + expect(urlHelper.isGhes('https://contoso.ghe.com')).toBeFalsy() expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy() expect(urlHelper.isGhes('https://src.onpremise.customer.com')).toBeTruthy() }) @@ -20,11 +20,11 @@ describe('getServerApiUrl tests', () => { expect(urlHelper.getServerApiUrl('https://GitHub.com')).toBe( 'https://api.github.com' ) - expect(urlHelper.getServerApiUrl('https://europe.ghe.com')).toBe( - 'https://api.europe.ghe.com' + expect(urlHelper.getServerApiUrl('https://contoso.ghe.com')).toBe( + 'https://api.contoso.ghe.com' ) - expect(urlHelper.getServerApiUrl('https://australia.GHE.COM')).toBe( - 'https://api.australia.ghe.com' + expect(urlHelper.getServerApiUrl('https://fabrikam.GHE.COM')).toBe( + 'https://api.fabrikam.ghe.com' ) expect( urlHelper.getServerApiUrl('https://src.onpremise.customer.com') From 041723abcc261fc3c9a738f026fe7e5fde461962 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:56:31 +0000 Subject: [PATCH 07/11] updated unit tests --- __test__/url-helper.test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/__test__/url-helper.test.ts b/__test__/url-helper.test.ts index 53b6671..0c2c8d1 100644 --- a/__test__/url-helper.test.ts +++ b/__test__/url-helper.test.ts @@ -4,10 +4,9 @@ describe('isGhes tests', () => { it('basics', async () => { expect(urlHelper.isGhes()).toBeFalsy() expect(urlHelper.isGhes('https://github.com')).toBeFalsy() - //expect(urlHelper.isGhes('https://api.github.com')).toBeFalsy() expect(urlHelper.isGhes('https://contoso.ghe.com')).toBeFalsy() expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy() - expect(urlHelper.isGhes('https://src.onpremise.customer.com')).toBeTruthy() + expect(urlHelper.isGhes('https://src.onpremise.fabrikam.com')).toBeTruthy() }) }) @@ -27,7 +26,7 @@ describe('getServerApiUrl tests', () => { 'https://api.fabrikam.ghe.com' ) expect( - urlHelper.getServerApiUrl('https://src.onpremise.customer.com') - ).toBe('https://src.onpremise.customer.com/api/v3') + urlHelper.getServerApiUrl('https://src.onpremise.fabrikam.com') + ).toBe('https://src.onpremise.fabrikam.com/api/v3') }) }) From 914445f9e7f1b9aadf46eb6c3a478d36e1477925 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:46:35 +0000 Subject: [PATCH 08/11] Responded to PR feedback. --- src/url-helper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/url-helper.ts b/src/url-helper.ts index b12765c..5c4fcbd 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -51,10 +51,10 @@ export function isGhes(url?: string): boolean { const hostname = ghUrl.hostname.trimEnd().toUpperCase() const isGitHubHost = hostname === 'GITHUB.COM' - const isGheHost = hostname.endsWith('.GHE.COM') + const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM') const isLocalHost = hostname.endsWith('.LOCALHOST') - return !isGitHubHost && !isGheHost && !isLocalHost + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost } function pruneSuffix(text: string, suffix: string) { From 7695871fe06159c11edae34db6ecea209300fc62 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:50:52 +0000 Subject: [PATCH 09/11] ran `npm run build` --- dist/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 652ff1d..2b418c3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2477,9 +2477,9 @@ function isGhes(url) { const ghUrl = new url_1.URL(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 isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM'); const isLocalHost = hostname.endsWith('.LOCALHOST'); - return !isGitHubHost && !isGheHost && !isLocalHost; + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } function pruneSuffix(text, suffix) { if (hasContent(suffix, true) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) { From 7fd13ec4182002d42f22aba877b1cef3c920be02 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:54:12 +0000 Subject: [PATCH 10/11] Address `hasContent` readability --- __test__/url-helper.test.ts | 23 +++++++++++++++++++++++ dist/index.js | 16 +++++++++++----- src/url-helper.ts | 18 +++++++++++++----- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/__test__/url-helper.test.ts b/__test__/url-helper.test.ts index 0c2c8d1..27f6606 100644 --- a/__test__/url-helper.test.ts +++ b/__test__/url-helper.test.ts @@ -1,5 +1,28 @@ import * as urlHelper from '../src/url-helper' +describe('getServerUrl tests', () => { + it('basics', async () => { + // Note that URL::toString will append a trailing / when passed just a domain name ... + expect(urlHelper.getServerUrl().toString()).toBe('https://github.com/') + expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/') + expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/') + expect(urlHelper.getServerUrl('http://contoso.com').toString()).toBe( + 'http://contoso.com/' + ) + expect(urlHelper.getServerUrl('https://contoso.com').toString()).toBe( + 'https://contoso.com/' + ) + expect(urlHelper.getServerUrl('https://contoso.com/').toString()).toBe( + 'https://contoso.com/' + ) + + // ... but can't make that same assumption when passed an URL that includes some deeper path. + expect(urlHelper.getServerUrl('https://contoso.com/a/b').toString()).toBe( + 'https://contoso.com/a/b' + ) + }) +}) + describe('isGhes tests', () => { it('basics', async () => { expect(urlHelper.isGhes()).toBeFalsy() diff --git a/dist/index.js b/dist/index.js index 2b418c3..55ee37a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2455,13 +2455,13 @@ function getFetchUrl(settings) { } function getServerUrl(url) { let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; - if (hasContent(url, false)) { + if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { resolvedUrl = url; } return new url_1.URL(resolvedUrl); } function getServerApiUrl(url) { - if (hasContent(url, false)) { + if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { let serverUrl = getServerUrl(url); if (isGhes(url)) { serverUrl.pathname = 'api/v3'; @@ -2482,14 +2482,20 @@ function isGhes(url) { return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } function pruneSuffix(text, suffix) { - if (hasContent(suffix, true) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) { + if (hasContent(suffix, WhitespaceMode.AllowPureWhitespace) && + (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) { return text.substring(0, text.length - suffix.length); } return text; } -function hasContent(text, allowPureWhitespace) { +var WhitespaceMode; +(function (WhitespaceMode) { + WhitespaceMode[WhitespaceMode["IgnorePureWhitespace"] = 0] = "IgnorePureWhitespace"; + WhitespaceMode[WhitespaceMode["AllowPureWhitespace"] = 1] = "AllowPureWhitespace"; +})(WhitespaceMode || (WhitespaceMode = {})); +function hasContent(text, whitespaceMode) { let refinedText = text !== null && text !== void 0 ? text : ''; - if (!allowPureWhitespace) { + if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) { refinedText = refinedText.trim(); } return refinedText.length > 0; diff --git a/src/url-helper.ts b/src/url-helper.ts index 5c4fcbd..47721da 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -22,7 +22,7 @@ export function getFetchUrl(settings: IGitSourceSettings): string { export function getServerUrl(url?: string): URL { let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com' - if (hasContent(url, false)) { + if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { resolvedUrl = url! } @@ -30,7 +30,7 @@ export function getServerUrl(url?: string): URL { } export function getServerApiUrl(url?: string): string { - if (hasContent(url, false)) { + if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { let serverUrl = getServerUrl(url) if (isGhes(url)) { serverUrl.pathname = 'api/v3' @@ -58,18 +58,26 @@ export function isGhes(url?: string): boolean { } function pruneSuffix(text: string, suffix: string) { - if (hasContent(suffix, true) && text?.endsWith(suffix)) { + if ( + hasContent(suffix, WhitespaceMode.AllowPureWhitespace) && + text?.endsWith(suffix) + ) { return text.substring(0, text.length - suffix.length) } return text } +enum WhitespaceMode { + IgnorePureWhitespace, + AllowPureWhitespace +} + function hasContent( text: string | undefined, - allowPureWhitespace: boolean + whitespaceMode: WhitespaceMode ): boolean { let refinedText = text ?? '' - if (!allowPureWhitespace) { + if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) { refinedText = refinedText.trim() } return refinedText.length > 0 From 55f79d95dfa5db93171a58a2aef766066e6e65c2 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Thu, 17 Oct 2024 23:01:47 +0000 Subject: [PATCH 11/11] Use concise `WhitespaceMode` names --- dist/index.js | 13 ++++++------- src/url-helper.ts | 15 ++++++--------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/dist/index.js b/dist/index.js index 55ee37a..b0db713 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2455,13 +2455,13 @@ function getFetchUrl(settings) { } function getServerUrl(url) { let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; - if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { + if (hasContent(url, WhitespaceMode.Trim)) { resolvedUrl = url; } return new url_1.URL(resolvedUrl); } function getServerApiUrl(url) { - if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { + if (hasContent(url, WhitespaceMode.Trim)) { let serverUrl = getServerUrl(url); if (isGhes(url)) { serverUrl.pathname = 'api/v3'; @@ -2482,20 +2482,19 @@ function isGhes(url) { return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } function pruneSuffix(text, suffix) { - if (hasContent(suffix, WhitespaceMode.AllowPureWhitespace) && - (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) { + if (hasContent(suffix, WhitespaceMode.Preserve) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) { return text.substring(0, text.length - suffix.length); } return text; } var WhitespaceMode; (function (WhitespaceMode) { - WhitespaceMode[WhitespaceMode["IgnorePureWhitespace"] = 0] = "IgnorePureWhitespace"; - WhitespaceMode[WhitespaceMode["AllowPureWhitespace"] = 1] = "AllowPureWhitespace"; + WhitespaceMode[WhitespaceMode["Trim"] = 0] = "Trim"; + WhitespaceMode[WhitespaceMode["Preserve"] = 1] = "Preserve"; })(WhitespaceMode || (WhitespaceMode = {})); function hasContent(text, whitespaceMode) { let refinedText = text !== null && text !== void 0 ? text : ''; - if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) { + if (whitespaceMode == WhitespaceMode.Trim) { refinedText = refinedText.trim(); } return refinedText.length > 0; diff --git a/src/url-helper.ts b/src/url-helper.ts index 47721da..17a0842 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -22,7 +22,7 @@ export function getFetchUrl(settings: IGitSourceSettings): string { export function getServerUrl(url?: string): URL { let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com' - if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { + if (hasContent(url, WhitespaceMode.Trim)) { resolvedUrl = url! } @@ -30,7 +30,7 @@ export function getServerUrl(url?: string): URL { } export function getServerApiUrl(url?: string): string { - if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) { + if (hasContent(url, WhitespaceMode.Trim)) { let serverUrl = getServerUrl(url) if (isGhes(url)) { serverUrl.pathname = 'api/v3' @@ -58,18 +58,15 @@ export function isGhes(url?: string): boolean { } function pruneSuffix(text: string, suffix: string) { - if ( - hasContent(suffix, WhitespaceMode.AllowPureWhitespace) && - text?.endsWith(suffix) - ) { + if (hasContent(suffix, WhitespaceMode.Preserve) && text?.endsWith(suffix)) { return text.substring(0, text.length - suffix.length) } return text } enum WhitespaceMode { - IgnorePureWhitespace, - AllowPureWhitespace + Trim, + Preserve } function hasContent( @@ -77,7 +74,7 @@ function hasContent( whitespaceMode: WhitespaceMode ): boolean { let refinedText = text ?? '' - if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) { + if (whitespaceMode == WhitespaceMode.Trim) { refinedText = refinedText.trim() } return refinedText.length > 0