From 65865e15a14a3de9378a18a026ce6548b17a39ed Mon Sep 17 00:00:00 2001 From: Daniel Hwang Date: Sun, 31 May 2020 14:46:53 -0700 Subject: [PATCH 1/7] build because all is no more (#264) --- __test__/verify-no-unstaged-changes.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__test__/verify-no-unstaged-changes.sh b/__test__/verify-no-unstaged-changes.sh index 9fe6173..a7af194 100755 --- a/__test__/verify-no-unstaged-changes.sh +++ b/__test__/verify-no-unstaged-changes.sh @@ -12,6 +12,6 @@ if [[ "$(git status --porcelain)" != "" ]]; then echo ---------------------------------------- echo Troubleshooting echo ---------------------------------------- - echo "::error::Unstaged changes detected. Locally try running: git clean -ffdx && npm ci && npm run all" + echo "::error::Unstaged changes detected. Locally try running: git clean -ffdx && npm ci && npm run build" exit 1 fi From 453ee27fca95fa9e03a24c1969a92c82e1a9b15e Mon Sep 17 00:00:00 2001 From: eric sciple Date: Sun, 31 May 2020 17:48:32 -0400 Subject: [PATCH 2/7] update troubleshooting instructions to include 'npm run format' --- __test__/verify-no-unstaged-changes.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__test__/verify-no-unstaged-changes.sh b/__test__/verify-no-unstaged-changes.sh index a7af194..9b30471 100755 --- a/__test__/verify-no-unstaged-changes.sh +++ b/__test__/verify-no-unstaged-changes.sh @@ -12,6 +12,6 @@ if [[ "$(git status --porcelain)" != "" ]]; then echo ---------------------------------------- echo Troubleshooting echo ---------------------------------------- - echo "::error::Unstaged changes detected. Locally try running: git clean -ffdx && npm ci && npm run build" + echo "::error::Unstaged changes detected. Locally try running: git clean -ffdx && npm ci && npm run format && npm run build" exit 1 fi From 00a3be89340a3ce8d704f82f44a5e7f9e3a84dfe Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 16 Jun 2020 13:41:01 -0400 Subject: [PATCH 3/7] determine default branch (#278) --- README.md | 2 +- __test__/input-helper.test.ts | 7 ------- action.yml | 2 +- dist/index.js | 36 +++++++++++++++++++++++++++++++---- src/git-source-provider.ts | 11 +++++++++++ src/github-api-helper.ts | 32 +++++++++++++++++++++++++++++++ src/input-helper.ts | 6 +----- 7 files changed, 78 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9104f8b..f647b6e 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous # The branch, tag or SHA to checkout. When checking out the repository that # triggered a workflow, this defaults to the reference or SHA for that event. - # Otherwise, defaults to `master`. + # Otherwise, uses the default branch. ref: '' # Personal access token (PAT) used to fetch the repository. The PAT is configured diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 00732ef..920bc8e 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -110,13 +110,6 @@ describe('input-helper tests', () => { ) }) - it('sets correct default ref/sha for other repo', () => { - inputs.repository = 'some-owner/some-other-repo' - const settings: IGitSourceSettings = inputHelper.getInputs() - expect(settings.ref).toBe('refs/heads/master') - expect(settings.commit).toBeFalsy() - }) - it('sets ref to empty when explicit sha', () => { inputs.ref = '1111111111222222222233333333334444444444' const settings: IGitSourceSettings = inputHelper.getInputs() diff --git a/action.yml b/action.yml index 58e11b7..71655da 100644 --- a/action.yml +++ b/action.yml @@ -8,7 +8,7 @@ inputs: description: > The branch, tag or SHA to checkout. When checking out the repository that triggered a workflow, this defaults to the reference or SHA for that - event. Otherwise, defaults to `master`. + event. Otherwise, uses the default branch. token: description: > Personal access token (PAT) used to fetch the repository. The PAT is configured diff --git a/dist/index.js b/dist/index.js index 0c78d25..4ade91c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -6114,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); @@ -9569,6 +9575,31 @@ function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) }); } exports.downloadRepository = downloadRepository; +/** + * Looks up the default branch name + */ +function getDefaultBranch(authToken, owner, repo) { + return __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'); + 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}`); + } + // 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}`; + } + return result; + })); + }); +} +exports.getDefaultBranch = getDefaultBranch; function downloadArchive(authToken, owner, repo, ref, commit) { return __awaiter(this, void 0, void 0, function* () { const octokit = new github.GitHub(authToken); @@ -14471,9 +14502,6 @@ function getInputs() { result.ref = `refs/heads/${result.ref}`; } } - if (!result.ref && !result.commit) { - result.ref = 'refs/heads/master'; - } } // SHA? else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) { @@ -14508,7 +14536,7 @@ function getInputs() { core.debug(`submodules = ${result.submodules}`); core.debug(`recursive submodules = ${result.nestedSubmodules}`); // Auth token - result.authToken = core.getInput('token'); + result.authToken = core.getInput('token', { required: true }); // SSH result.sshKey = core.getInput('ssh-key'); result.sshKnownHosts = core.getInput('ssh-known-hosts'); diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index 89c16b5..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) diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index e559c45..7a09638 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -67,6 +67,38 @@ export async function downloadRepository( io.rmRF(extractPath) } +/** + * Looks up the default branch name + */ +export async function getDefaultBranch( + authToken: string, + owner: string, + repo: string +): Promise { + 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}` + ) + } + + // 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}` + } + + return result + }) +} + async function downloadArchive( authToken: string, owner: string, diff --git a/src/input-helper.ts b/src/input-helper.ts index 11a1ab6..eabb9e0 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -68,10 +68,6 @@ export function getInputs(): IGitSourceSettings { result.ref = `refs/heads/${result.ref}` } } - - if (!result.ref && !result.commit) { - result.ref = 'refs/heads/master' - } } // SHA? else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) { @@ -110,7 +106,7 @@ export function getInputs(): IGitSourceSettings { core.debug(`recursive submodules = ${result.nestedSubmodules}`) // Auth token - result.authToken = core.getInput('token') + result.authToken = core.getInput('token', {required: true}) // SSH result.sshKey = core.getInput('ssh-key') From b4483adec309c0d01a5435c5e24eb40de5773ad9 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 16 Jun 2020 13:48:53 -0400 Subject: [PATCH 4/7] changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3d2627..e9892cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,15 @@ # Changelog +## v2.3.0 + +- [Fallback to the default branch](https://github.com/actions/checkout/pull/278) + ## v2.2.0 + - [Fetch all history for all tags and branches when fetch-depth=0](https://github.com/actions/checkout/pull/258) ## v2.1.1 + - Changes to support GHES ([here](https://github.com/actions/checkout/pull/236) and [here](https://github.com/actions/checkout/pull/248)) ## v2.1.0 From fb6f360df236bd2026c7963cf88c8ddf20b4f0e2 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Thu, 18 Jun 2020 10:20:33 -0400 Subject: [PATCH 5/7] fix default branch for .wiki and when using ssh (#284) --- __test__/git-auth-helper.test.ts | 1 + __test__/git-directory-helper.test.ts | 1 + dist/index.js | 70 ++++++++++++++++++++++----- src/git-command-manager.ts | 29 +++++++++++ src/git-source-provider.ts | 26 +++++----- src/github-api-helper.ts | 28 ++++++++--- 6 files changed, 126 insertions(+), 29 deletions(-) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index 92a462a..e4e640c 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(), diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts index 7283102..70849b5 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(), diff --git a/dist/index.js b/dist/index.js index 4ade91c..e0d0238 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'); @@ -9583,14 +9620,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/git-command-manager.ts b/src/git-command-manager.ts index 9d2d45f..8bf3aa1 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 25fba04..366ff33 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 7a09638..8bbcf2d 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -19,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') @@ -78,17 +84,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 28c7f3d2b5162b5ddd3dfd9a45aa55eaf396478b Mon Sep 17 00:00:00 2001 From: eric sciple Date: Thu, 18 Jun 2020 10:27:39 -0400 Subject: [PATCH 6/7] changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9892cb..6f40def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # 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) From 61b9e3751b92087fd0b06925ba6dd6314e06f089 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Sun, 12 Jul 2020 21:02:24 -0400 Subject: [PATCH 7/7] improve description for fetch-depth (#301) --- README.md | 2 +- action.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f647b6e..c2bd069 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. + # Number of commits to fetch. 0 indicates all history for all branches and tags. # Default: 1 fetch-depth: '' diff --git a/action.yml b/action.yml index 71655da..91d3982 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.' + description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.' default: 1 lfs: description: 'Whether to download Git-LFS files'