Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass correct baseUrl to octokit #1288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions __test__/github-api-helper.test.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the ; necessary for this typecast?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically no, but the prettier configuration in this repository seems to require it. At least the format-check fails without it and format adds it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, my mistake, it is correct. The ; is to prevent the parentheses of (github.getOctokit as jest.Mock) to be interpreted as function invocation in case later statement is added above.

console.log('foo')
(github.getOctokit as jest.Mock)

would be read as console.log('foo')(github.getOctokit as jest.Mock), the ; prevents it.

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 changes: 8 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,7 @@ const io = __importStar(__nccwpck_require__(7436));
const path = __importStar(__nccwpck_require__(1017));
const retryHelper = __importStar(__nccwpck_require__(2155));
const toolCache = __importStar(__nccwpck_require__(7784));
const url_helper_1 = __nccwpck_require__(9437);
const v4_1 = __importDefault(__nccwpck_require__(824));
const IS_WINDOWS = process.platform === 'win32';
function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath, baseUrl) {
Expand Down Expand Up @@ -1513,7 +1514,9 @@ function getDefaultBranch(authToken, owner, repo, baseUrl) {
return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
var _a;
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;
try {
// Get the default branch from the repo info
Expand Down Expand Up @@ -1545,7 +1548,9 @@ function getDefaultBranch(authToken, owner, repo, baseUrl) {
exports.getDefaultBranch = getDefaultBranch;
function downloadArchive(authToken, owner, repo, ref, commit, baseUrl) {
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
? octokit.rest.repos.downloadZipballArchive
: octokit.rest.repos.downloadTarballArchive;
Expand Down Expand Up @@ -2026,7 +2031,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: 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})`
});
yield octokit.rest.repos.get({
Expand Down
9 changes: 7 additions & 2 deletions src/github-api-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as io from '@actions/io'
import * as path from 'path'
import * as retryHelper from './retry-helper'
import * as toolCache from '@actions/tool-cache'
import {getServerApiUrl} from './url-helper'
import {default as uuid} from 'uuid/v4'

const IS_WINDOWS = process.platform === 'win32'
Expand Down Expand Up @@ -84,7 +85,9 @@ export async function getDefaultBranch(
): Promise<string> {
return await retryHelper.execute(async () => {
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
try {
// Get the default branch from the repo info
Expand Down Expand Up @@ -125,7 +128,9 @@ async function downloadArchive(
commit: string,
baseUrl?: string
): Promise<Buffer> {
const octokit = github.getOctokit(authToken, {baseUrl: baseUrl})
const octokit = github.getOctokit(authToken, {
baseUrl: getServerApiUrl(baseUrl)
})
const download = IS_WINDOWS
? octokit.rest.repos.downloadZipballArchive
: octokit.rest.repos.downloadTarballArchive
Expand Down
4 changes: 2 additions & 2 deletions src/ref-helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IGitCommandManager} from './git-command-manager'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {isGhes} from './url-helper'
import {getServerApiUrl, isGhes} from './url-helper'

export const tagsRefSpec = '+refs/tags/*:refs/tags/*'

Expand Down Expand Up @@ -245,7 +245,7 @@ export async function checkCommitInfo(
`Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}`
)
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(
'number'
)};run_id=${
Expand Down