diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f40def..e9892cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,5 @@ # Changelog -## v2.3.1 - -- [Fix default branch resolution for .wiki and when using SSH](https://github.com/actions/checkout/pull/284) - - ## v2.3.0 - [Fallback to the default branch](https://github.com/actions/checkout/pull/278) diff --git a/README.md b/README.md index c2bd069..f647b6e 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous # Default: true clean: '' - # Number of commits to fetch. 0 indicates all history for all branches and tags. + # Number of commits to fetch. 0 indicates all history. # Default: 1 fetch-depth: '' diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index e4e640c..92a462a 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -714,7 +714,6 @@ async function setup(testName: string): Promise { ), env: {}, fetch: jest.fn(), - getDefaultBranch: jest.fn(), getWorkingDirectory: jest.fn(() => workspace), init: jest.fn(), isDetached: jest.fn(), diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts index 70849b5..7283102 100644 --- a/__test__/git-directory-helper.test.ts +++ b/__test__/git-directory-helper.test.ts @@ -408,7 +408,6 @@ 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(), diff --git a/action.yml b/action.yml index 91d3982..71655da 100644 --- a/action.yml +++ b/action.yml @@ -54,7 +54,7 @@ inputs: description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching' default: true fetch-depth: - description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.' + description: 'Number of commits to fetch. 0 indicates all history.' default: 1 lfs: description: 'Whether to download Git-LFS files' diff --git a/dist/index.js b/dist/index.js index e0d0238..4ade91c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5827,33 +5827,6 @@ 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; } @@ -6141,6 +6114,12 @@ 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); @@ -6193,17 +6172,6 @@ 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(); @@ -9563,11 +9531,6 @@ 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'); @@ -9620,25 +9583,14 @@ 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); - 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; - } + 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}`); } // 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/git-command-manager.ts b/src/git-command-manager.ts index 8bf3aa1..9d2d45f 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -25,7 +25,6 @@ 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 @@ -196,34 +195,6 @@ 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 366ff33..25fba04 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -19,6 +19,17 @@ 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) @@ -103,21 +114,6 @@ 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 8bbcf2d..7a09638 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -19,12 +19,6 @@ 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') @@ -84,25 +78,17 @@ export async function getDefaultBranch( return await retryHelper.execute(async () => { core.info('Retrieving the default branch name') const octokit = new github.GitHub(authToken) - 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 - } + 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}` + ) } // 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/')) {