From 8aabf05e48b6950a4ccd09b171096124a7654d20 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Wed, 17 Jun 2020 17:44:27 -0400 Subject: [PATCH 1/4] fix default branch for .wiki --- dist/index.js | 21 ++++++++++++++++----- src/github-api-helper.ts | 23 ++++++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/dist/index.js b/dist/index.js index 4ade91c96..36c0105ce 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9583,14 +9583,25 @@ function getDefaultBranch(authToken, owner, repo) { return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { core.info('Retrieving the default branch name'); const octokit = new github.GitHub(authToken); - const response = yield octokit.repos.get({ owner, repo }); - if (response.status != 200) { - throw new Error(`Unexpected response from GitHub API. Status: ${response.status}, Data: ${response.data}`); + let result; + try { + // Get the default branch from the repo info + const response = yield octokit.repos.get({ owner, repo }); + result = response.data.default_branch; + assert.ok(result, 'default_branch cannot be empty'); + } + catch (err) { + // Handle .wiki repo + if (err['status'] === 404 && repo.toUpperCase().endsWith('.WIKI')) { + result = 'master'; + } + // Otherwise error + else { + throw err; + } } // Print the default branch - let result = response.data.default_branch; core.info(`Default branch '${result}'`); - assert.ok(result, 'default_branch cannot be empty'); // Prefix with 'refs/heads' if (!result.startsWith('refs/')) { result = `refs/heads/${result}`; diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 7a096388c..15ac77eab 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -8,6 +8,7 @@ import * as retryHelper from './retry-helper' import * as toolCache from '@actions/tool-cache' import {default as uuid} from 'uuid/v4' import {Octokit} from '@octokit/rest' +import {Console} from 'console' const IS_WINDOWS = process.platform === 'win32' @@ -78,17 +79,25 @@ export async function getDefaultBranch( return await retryHelper.execute(async () => { core.info('Retrieving the default branch name') const octokit = new github.GitHub(authToken) - const response = await octokit.repos.get({owner, repo}) - if (response.status != 200) { - throw new Error( - `Unexpected response from GitHub API. Status: ${response.status}, Data: ${response.data}` - ) + let result: string + try { + // Get the default branch from the repo info + const response = await octokit.repos.get({owner, repo}) + result = response.data.default_branch + assert.ok(result, 'default_branch cannot be empty') + } catch (err) { + // Handle .wiki repo + if (err['status'] === 404 && repo.toUpperCase().endsWith('.WIKI')) { + result = 'master' + } + // Otherwise error + else { + throw err + } } // Print the default branch - let result = response.data.default_branch core.info(`Default branch '${result}'`) - assert.ok(result, 'default_branch cannot be empty') // Prefix with 'refs/heads' if (!result.startsWith('refs/')) { From 6e3d2777afb996077a08b1ab2977c53bfe1fb020 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Thu, 18 Jun 2020 09:47:47 -0400 Subject: [PATCH 2/4] Fix getting default branch when using SSH --- dist/index.js | 49 +++++++++++++++++++++++++++++++++----- src/git-command-manager.ts | 29 ++++++++++++++++++++++ src/git-source-provider.ts | 26 +++++++++++--------- src/github-api-helper.ts | 7 +++++- 4 files changed, 93 insertions(+), 18 deletions(-) diff --git a/dist/index.js b/dist/index.js index 36c0105ce..e0d023855 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5827,6 +5827,33 @@ class GitCommandManager { })); }); } + getDefaultBranch(repositoryUrl) { + return __awaiter(this, void 0, void 0, function* () { + let output; + yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { + output = yield this.execGit([ + 'ls-remote', + '--quiet', + '--exit-code', + '--symref', + repositoryUrl, + 'HEAD' + ]); + })); + if (output) { + // Satisfy compiler, will always be set + for (let line of output.stdout.trim().split('\n')) { + line = line.trim(); + if (line.startsWith('ref:') || line.endsWith('HEAD')) { + return line + .substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length) + .trim(); + } + } + } + throw new Error('Unexpected output when retrieving default branch'); + }); + } getWorkingDirectory() { return this.workingDirectory; } @@ -6114,12 +6141,6 @@ function getSource(settings) { // Repository URL core.info(`Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}`); const repositoryUrl = urlHelper.getFetchUrl(settings); - // Determine the default branch - if (!settings.ref && !settings.commit) { - core.startGroup('Determining the default branch'); - settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName); - core.endGroup(); - } // Remove conflicting file path if (fsHelper.fileExistsSync(settings.repositoryPath)) { yield io.rmRF(settings.repositoryPath); @@ -6172,6 +6193,17 @@ function getSource(settings) { core.startGroup('Setting up auth'); yield authHelper.configureAuth(); core.endGroup(); + // Determine the default branch + if (!settings.ref && !settings.commit) { + core.startGroup('Determining the default branch'); + if (settings.sshKey) { + settings.ref = yield git.getDefaultBranch(repositoryUrl); + } + else { + settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName); + } + core.endGroup(); + } // LFS install if (settings.lfs) { yield git.lfsInstall(); @@ -9531,6 +9563,11 @@ const v4_1 = __importDefault(__webpack_require__(826)); const IS_WINDOWS = process.platform === 'win32'; function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) { return __awaiter(this, void 0, void 0, function* () { + // Determine the default branch + if (!ref && !commit) { + core.info('Determining the default branch'); + ref = yield getDefaultBranch(authToken, owner, repo); + } // Download the archive let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { core.info('Downloading the archive'); diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 9d2d45fdf..8bf3aa16b 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -25,6 +25,7 @@ export interface IGitCommandManager { ): Promise configExists(configKey: string, globalConfig?: boolean): Promise fetch(refSpec: string[], fetchDepth?: number): Promise + getDefaultBranch(repositoryUrl: string): Promise getWorkingDirectory(): string init(): Promise isDetached(): Promise @@ -195,6 +196,34 @@ class GitCommandManager { }) } + async getDefaultBranch(repositoryUrl: string): Promise { + let output: GitOutput | undefined + await retryHelper.execute(async () => { + output = await this.execGit([ + 'ls-remote', + '--quiet', + '--exit-code', + '--symref', + repositoryUrl, + 'HEAD' + ]) + }) + + if (output) { + // Satisfy compiler, will always be set + for (let line of output.stdout.trim().split('\n')) { + line = line.trim() + if (line.startsWith('ref:') || line.endsWith('HEAD')) { + return line + .substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length) + .trim() + } + } + } + + throw new Error('Unexpected output when retrieving default branch') + } + getWorkingDirectory(): string { return this.workingDirectory } diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index 25fba047a..366ff3378 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -19,17 +19,6 @@ export async function getSource(settings: IGitSourceSettings): Promise { ) const repositoryUrl = urlHelper.getFetchUrl(settings) - // Determine the default branch - if (!settings.ref && !settings.commit) { - core.startGroup('Determining the default branch') - settings.ref = await githubApiHelper.getDefaultBranch( - settings.authToken, - settings.repositoryOwner, - settings.repositoryName - ) - core.endGroup() - } - // Remove conflicting file path if (fsHelper.fileExistsSync(settings.repositoryPath)) { await io.rmRF(settings.repositoryPath) @@ -114,6 +103,21 @@ export async function getSource(settings: IGitSourceSettings): Promise { await authHelper.configureAuth() core.endGroup() + // Determine the default branch + if (!settings.ref && !settings.commit) { + core.startGroup('Determining the default branch') + if (settings.sshKey) { + settings.ref = await git.getDefaultBranch(repositoryUrl) + } else { + settings.ref = await githubApiHelper.getDefaultBranch( + settings.authToken, + settings.repositoryOwner, + settings.repositoryName + ) + } + core.endGroup() + } + // LFS install if (settings.lfs) { await git.lfsInstall() diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 15ac77eab..8bbcf2dea 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -8,7 +8,6 @@ import * as retryHelper from './retry-helper' import * as toolCache from '@actions/tool-cache' import {default as uuid} from 'uuid/v4' import {Octokit} from '@octokit/rest' -import {Console} from 'console' const IS_WINDOWS = process.platform === 'win32' @@ -20,6 +19,12 @@ export async function downloadRepository( commit: string, repositoryPath: string ): Promise { + // Determine the default branch + if (!ref && !commit) { + core.info('Determining the default branch') + ref = await getDefaultBranch(authToken, owner, repo) + } + // Download the archive let archiveData = await retryHelper.execute(async () => { core.info('Downloading the archive') From 5e36a5f9a2e9b7e781776b327ee4d2c10d2021c0 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Thu, 18 Jun 2020 09:51:35 -0400 Subject: [PATCH 3/4] fix test --- __test__/git-auth-helper.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index 92a462a2e..e4e640c62 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -714,6 +714,7 @@ async function setup(testName: string): Promise { ), env: {}, fetch: jest.fn(), + getDefaultBranch: jest.fn(), getWorkingDirectory: jest.fn(() => workspace), init: jest.fn(), isDetached: jest.fn(), From 3d0c2f195499671120c9d795b083ee1971517af0 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Thu, 18 Jun 2020 09:53:35 -0400 Subject: [PATCH 4/4] fix test --- __test__/git-directory-helper.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts index 7283102d3..70849b538 100644 --- a/__test__/git-directory-helper.test.ts +++ b/__test__/git-directory-helper.test.ts @@ -408,6 +408,7 @@ async function setup(testName: string): Promise { config: jest.fn(), configExists: jest.fn(), fetch: jest.fn(), + getDefaultBranch: jest.fn(), getWorkingDirectory: jest.fn(() => repositoryPath), init: jest.fn(), isDetached: jest.fn(),