Compare commits

...

6 Commits

Author SHA1 Message Date
John Wesley Walker III 041723abcc updated unit tests 2024-10-17 11:56:31 +00:00
John Wesley Walker III 813c3f2d82
Apply suggestions from @easyt
Co-authored-by: Erez Testiler <easyt@github.com>
2024-10-17 13:49:31 +02:00
John Wesley Walker III 19060a122d ran `npm run build` 2024-10-16 18:18:09 +00:00
John Wesley Walker III 7fcbcdce7d ran `npm run format` 2024-10-16 18:15:26 +00:00
John Wesley Walker III 2f42443dda restore passing `baseUrl` where appropriate 2024-10-16 17:35:40 +00:00
John Wesley Walker III df4b58c789 second attempt 2024-10-16 17:29:39 +00:00
5 changed files with 109 additions and 23 deletions

View File

@ -0,0 +1,32 @@
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://contoso.ghe.com')).toBeFalsy()
expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy()
expect(urlHelper.isGhes('https://src.onpremise.fabrikam.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://contoso.ghe.com')).toBe(
'https://api.contoso.ghe.com'
)
expect(urlHelper.getServerApiUrl('https://fabrikam.GHE.COM')).toBe(
'https://api.fabrikam.ghe.com'
)
expect(
urlHelper.getServerApiUrl('https://src.onpremise.fabrikam.com')
).toBe('https://src.onpremise.fabrikam.com/api/v3')
})
})

46
dist/index.js vendored
View File

@ -1617,7 +1617,7 @@ function getDefaultBranch(authToken, owner, repo, baseUrl) {
return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
core.info('Retrieving the default branch name'); core.info('Retrieving the default branch name');
const octokit = github.getOctokit(authToken, { const octokit = github.getOctokit(authToken, {
baseUrl: (0, url_helper_1.getServerApiUrl)() baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl)
}); });
let result; let result;
try { try {
@ -1650,7 +1650,7 @@ function getDefaultBranch(authToken, owner, repo, baseUrl) {
function downloadArchive(authToken, owner, repo, ref, commit, baseUrl) { function downloadArchive(authToken, owner, repo, ref, commit, baseUrl) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const octokit = github.getOctokit(authToken, { const octokit = github.getOctokit(authToken, {
baseUrl: (0, url_helper_1.getServerApiUrl)() baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl)
}); });
const download = IS_WINDOWS const download = IS_WINDOWS
? octokit.rest.repos.downloadZipballArchive ? octokit.rest.repos.downloadZipballArchive
@ -2128,7 +2128,7 @@ function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref
var _a; var _a;
try { try {
// GHES? // GHES?
if ((0, url_helper_1.isGhes)()) { if ((0, url_helper_1.isGhes)(baseUrl)) {
return; return;
} }
// Auth token? // Auth token?
@ -2174,7 +2174,7 @@ function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref
if (actualHeadSha !== expectedHeadSha) { if (actualHeadSha !== expectedHeadSha) {
core.debug(`Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}`); core.debug(`Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}`);
const octokit = github.getOctokit(token, { 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})` 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({ yield octokit.rest.repos.get({
@ -2454,22 +2454,46 @@ function getFetchUrl(settings) {
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`; return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`;
} }
function getServerUrl(url) { function getServerUrl(url) {
let urlValue = url && url.trim().length > 0 let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
? url if (hasContent(url, false)) {
: process.env['GITHUB_SERVER_URL'] || 'https://github.com'; resolvedUrl = url;
return new url_1.URL(urlValue); }
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'; return process.env['GITHUB_API_URL'] || 'https://api.github.com';
} }
function isGhes() { function isGhes(url) {
const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); const ghUrl = new url_1.URL(url || process.env['GITHUB_SERVER_URL'] || 'https://github.com');
const hostname = ghUrl.hostname.trimEnd().toUpperCase(); const hostname = ghUrl.hostname.trimEnd().toUpperCase();
const isGitHubHost = hostname === 'GITHUB.COM'; const isGitHubHost = hostname === 'GITHUB.COM';
const isGheHost = hostname.endsWith('.GHE.COM'); const isGheHost = hostname.endsWith('.GHE.COM');
const isLocalHost = hostname.endsWith('.LOCALHOST'); const isLocalHost = hostname.endsWith('.LOCALHOST');
return !isGitHubHost && !isGheHost && !isLocalHost; 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;
}
/***/ }), /***/ }),

View File

@ -88,7 +88,7 @@ export async function getDefaultBranch(
return await retryHelper.execute(async () => { return await retryHelper.execute(async () => {
core.info('Retrieving the default branch name') core.info('Retrieving the default branch name')
const octokit = github.getOctokit(authToken, { const octokit = github.getOctokit(authToken, {
baseUrl: getServerApiUrl() baseUrl: getServerApiUrl(baseUrl)
}) })
let result: string let result: string
try { try {
@ -131,7 +131,7 @@ async function downloadArchive(
baseUrl?: string baseUrl?: string
): Promise<Buffer> { ): Promise<Buffer> {
const octokit = github.getOctokit(authToken, { const octokit = github.getOctokit(authToken, {
baseUrl: getServerApiUrl() baseUrl: getServerApiUrl(baseUrl)
}) })
const download = IS_WINDOWS const download = IS_WINDOWS
? octokit.rest.repos.downloadZipballArchive ? octokit.rest.repos.downloadZipballArchive

View File

@ -192,7 +192,7 @@ export async function checkCommitInfo(
): Promise<void> { ): Promise<void> {
try { try {
// GHES? // GHES?
if (isGhes()) { if (isGhes(baseUrl)) {
return return
} }
@ -249,7 +249,7 @@ export async function checkCommitInfo(
`Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}` `Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}`
) )
const octokit = github.getOctokit(token, { 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( userAgent: `actions-checkout-tracepoint/1.0 (code=STALE_MERGE;owner=${repositoryOwner};repo=${repositoryName};pr=${fromPayload(
'number' 'number'
)};run_id=${ )};run_id=${

View File

@ -21,20 +21,32 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
} }
export function getServerUrl(url?: string): URL { export function getServerUrl(url?: string): URL {
let urlValue = let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'
url && url.trim().length > 0 if (hasContent(url, false)) {
? url resolvedUrl = url!
: process.env['GITHUB_SERVER_URL'] || 'https://github.com' }
return new URL(urlValue)
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' return process.env['GITHUB_API_URL'] || 'https://api.github.com'
} }
export function isGhes(): boolean { export function isGhes(url?: string): boolean {
const ghUrl = new URL( 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() const hostname = ghUrl.hostname.trimEnd().toUpperCase()
@ -44,3 +56,21 @@ export function isGhes(): boolean {
return !isGitHubHost && !isGheHost && !isLocalHost 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
}