Pass correct baseUrl to octokit

The PR #1246 replaced the `getOctokit` method from the
`octokit-provider.ts` file with the `getOctokit` method from the
`@actions/github` package.
The octokit-provider was previously responsible for creating an Octokit
instance and setting the `baseUrl` via the `getServerApiUrl` helper
function. This function calls `getServerUrl` which reads the server url
from the `GITHUB_SERVER_URL` environment variable, which on GHES is set
to the enterprise instance.
This commit restores the previous behaviour by calling `getServerApiUrl`
in all places where an octokit instance is created.

Co-authored-by: Markus Wolf <mail@markus-wolf.de>
This commit is contained in:
ZauberNerd 2023-04-13 14:10:01 +02:00
parent 83b7061638
commit 590916fc5e
No known key found for this signature in database
GPG Key ID: 9B617FBFF79E4F60
4 changed files with 54 additions and 7 deletions

View File

@ -0,0 +1,37 @@
import * as github from '@actions/github'
import * as githubApiHelper from '../lib/github-api-helper'
jest.mock('@actions/github')
describe('github-api-helper tests', () => {
describe('github enterprise compatibility', () => {
beforeEach(() => {
process.env.GITHUB_SERVER_URL = 'https://enterprise.git.com'
})
afterEach(() => {
delete process.env.GITHUB_SERVER_URL
})
it('getDefaultBranch should use GITHUB_SERVER_URL to set the baseUrl', async () => {
;(github.getOctokit as jest.Mock).mockImplementation(() => {
return {
rest: {
repos: {
get: jest.fn(() => ({data: {default_branch: 'default-branch'}}))
}
}
}
})
await githubApiHelper.getDefaultBranch('token', 'owner', 'repo')
expect(github.getOctokit).toHaveBeenCalledWith(
'token',
expect.objectContaining({
baseUrl: 'https://enterprise.git.com/api/v3'
})
)
})
})
})

11
dist/index.js vendored
View File

@ -1452,6 +1452,7 @@ const io = __importStar(__nccwpck_require__(7436));
const path = __importStar(__nccwpck_require__(1017)); const path = __importStar(__nccwpck_require__(1017));
const retryHelper = __importStar(__nccwpck_require__(2155)); const retryHelper = __importStar(__nccwpck_require__(2155));
const toolCache = __importStar(__nccwpck_require__(7784)); const toolCache = __importStar(__nccwpck_require__(7784));
const url_helper_1 = __nccwpck_require__(9437);
const v4_1 = __importDefault(__nccwpck_require__(824)); const v4_1 = __importDefault(__nccwpck_require__(824));
const IS_WINDOWS = process.platform === 'win32'; const IS_WINDOWS = process.platform === 'win32';
function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath, baseUrl) { function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath, baseUrl) {
@ -1513,7 +1514,9 @@ 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* () {
var _a; var _a;
core.info('Retrieving the default branch name'); core.info('Retrieving the default branch name');
const octokit = github.getOctokit(authToken, { baseUrl: baseUrl }); const octokit = github.getOctokit(authToken, {
baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl)
});
let result; let result;
try { try {
// Get the default branch from the repo info // Get the default branch from the repo info
@ -1545,7 +1548,9 @@ function getDefaultBranch(authToken, owner, repo, baseUrl) {
exports.getDefaultBranch = getDefaultBranch; exports.getDefaultBranch = getDefaultBranch;
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, { baseUrl: baseUrl }); const octokit = github.getOctokit(authToken, {
baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl)
});
const download = IS_WINDOWS const download = IS_WINDOWS
? octokit.rest.repos.downloadZipballArchive ? octokit.rest.repos.downloadZipballArchive
: octokit.rest.repos.downloadTarballArchive; : octokit.rest.repos.downloadTarballArchive;
@ -2026,7 +2031,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: baseUrl, 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({

View File

@ -6,6 +6,7 @@ import * as io from '@actions/io'
import * as path from 'path' import * as path from 'path'
import * as retryHelper from './retry-helper' import * as retryHelper from './retry-helper'
import * as toolCache from '@actions/tool-cache' import * as toolCache from '@actions/tool-cache'
import {getServerApiUrl} from './url-helper'
import {default as uuid} from 'uuid/v4' import {default as uuid} from 'uuid/v4'
const IS_WINDOWS = process.platform === 'win32' const IS_WINDOWS = process.platform === 'win32'
@ -84,7 +85,9 @@ export async function getDefaultBranch(
): Promise<string> { ): Promise<string> {
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, {baseUrl: baseUrl}) const octokit = github.getOctokit(authToken, {
baseUrl: getServerApiUrl(baseUrl)
})
let result: string let result: string
try { try {
// Get the default branch from the repo info // Get the default branch from the repo info
@ -125,7 +128,9 @@ async function downloadArchive(
commit: string, commit: string,
baseUrl?: string baseUrl?: string
): Promise<Buffer> { ): Promise<Buffer> {
const octokit = github.getOctokit(authToken, {baseUrl: baseUrl}) const octokit = github.getOctokit(authToken, {
baseUrl: getServerApiUrl(baseUrl)
})
const download = IS_WINDOWS const download = IS_WINDOWS
? octokit.rest.repos.downloadZipballArchive ? octokit.rest.repos.downloadZipballArchive
: octokit.rest.repos.downloadTarballArchive : octokit.rest.repos.downloadTarballArchive

View File

@ -1,7 +1,7 @@
import {IGitCommandManager} from './git-command-manager' import {IGitCommandManager} from './git-command-manager'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as github from '@actions/github' import * as github from '@actions/github'
import {isGhes} from './url-helper' import {getServerApiUrl, isGhes} from './url-helper'
export const tagsRefSpec = '+refs/tags/*:refs/tags/*' export const tagsRefSpec = '+refs/tags/*:refs/tags/*'
@ -245,7 +245,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: baseUrl, 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=${