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 diff --git a/README.md b/README.md index 8982c3f..8d4af56 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,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 0a59969..22f0890 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 correct default ref/sha for gist', () => { inputs.gist = 'some-owner/some-gist' const settings: IGitSourceSettings = inputHelper.getInputs() diff --git a/__test__/verify-no-unstaged-changes.sh b/__test__/verify-no-unstaged-changes.sh index 9fe6173..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 all" + echo "::error::Unstaged changes detected. Locally try running: git clean -ffdx && npm ci && npm run format && npm run build" exit 1 fi diff --git a/action.yml b/action.yml index c066833..bc728ba 100644 --- a/action.yml +++ b/action.yml @@ -10,7 +10,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 3312562..0761a94 100644 --- a/dist/index.js +++ b/dist/index.js @@ -6123,6 +6123,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); @@ -9578,6 +9584,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); @@ -14487,9 +14518,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}$/)) { @@ -14524,7 +14552,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 a692cf3..e3e823b 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -76,10 +76,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}$/)) { @@ -118,7 +114,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')