Skip to content

Commit 00a3be8

Browse files
authored
determine default branch (#278)
1 parent 453ee27 commit 00a3be8

7 files changed

+78
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous
4242

4343
# The branch, tag or SHA to checkout. When checking out the repository that
4444
# triggered a workflow, this defaults to the reference or SHA for that event.
45-
# Otherwise, defaults to `master`.
45+
# Otherwise, uses the default branch.
4646
ref: ''
4747

4848
# Personal access token (PAT) used to fetch the repository. The PAT is configured

__test__/input-helper.test.ts

-7
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,6 @@ describe('input-helper tests', () => {
110110
)
111111
})
112112

113-
it('sets correct default ref/sha for other repo', () => {
114-
inputs.repository = 'some-owner/some-other-repo'
115-
const settings: IGitSourceSettings = inputHelper.getInputs()
116-
expect(settings.ref).toBe('refs/heads/master')
117-
expect(settings.commit).toBeFalsy()
118-
})
119-
120113
it('sets ref to empty when explicit sha', () => {
121114
inputs.ref = '1111111111222222222233333333334444444444'
122115
const settings: IGitSourceSettings = inputHelper.getInputs()

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ inputs:
88
description: >
99
The branch, tag or SHA to checkout. When checking out the repository that
1010
triggered a workflow, this defaults to the reference or SHA for that
11-
event. Otherwise, defaults to `master`.
11+
event. Otherwise, uses the default branch.
1212
token:
1313
description: >
1414
Personal access token (PAT) used to fetch the repository. The PAT is configured

dist/index.js

+32-4
Original file line numberDiff line numberDiff line change
@@ -6114,6 +6114,12 @@ function getSource(settings) {
61146114
// Repository URL
61156115
core.info(`Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}`);
61166116
const repositoryUrl = urlHelper.getFetchUrl(settings);
6117+
// Determine the default branch
6118+
if (!settings.ref && !settings.commit) {
6119+
core.startGroup('Determining the default branch');
6120+
settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName);
6121+
core.endGroup();
6122+
}
61176123
// Remove conflicting file path
61186124
if (fsHelper.fileExistsSync(settings.repositoryPath)) {
61196125
yield io.rmRF(settings.repositoryPath);
@@ -9569,6 +9575,31 @@ function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath)
95699575
});
95709576
}
95719577
exports.downloadRepository = downloadRepository;
9578+
/**
9579+
* Looks up the default branch name
9580+
*/
9581+
function getDefaultBranch(authToken, owner, repo) {
9582+
return __awaiter(this, void 0, void 0, function* () {
9583+
return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
9584+
core.info('Retrieving the default branch name');
9585+
const octokit = new github.GitHub(authToken);
9586+
const response = yield octokit.repos.get({ owner, repo });
9587+
if (response.status != 200) {
9588+
throw new Error(`Unexpected response from GitHub API. Status: ${response.status}, Data: ${response.data}`);
9589+
}
9590+
// Print the default branch
9591+
let result = response.data.default_branch;
9592+
core.info(`Default branch '${result}'`);
9593+
assert.ok(result, 'default_branch cannot be empty');
9594+
// Prefix with 'refs/heads'
9595+
if (!result.startsWith('refs/')) {
9596+
result = `refs/heads/${result}`;
9597+
}
9598+
return result;
9599+
}));
9600+
});
9601+
}
9602+
exports.getDefaultBranch = getDefaultBranch;
95729603
function downloadArchive(authToken, owner, repo, ref, commit) {
95739604
return __awaiter(this, void 0, void 0, function* () {
95749605
const octokit = new github.GitHub(authToken);
@@ -14471,9 +14502,6 @@ function getInputs() {
1447114502
result.ref = `refs/heads/${result.ref}`;
1447214503
}
1447314504
}
14474-
if (!result.ref && !result.commit) {
14475-
result.ref = 'refs/heads/master';
14476-
}
1447714505
}
1447814506
// SHA?
1447914507
else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
@@ -14508,7 +14536,7 @@ function getInputs() {
1450814536
core.debug(`submodules = ${result.submodules}`);
1450914537
core.debug(`recursive submodules = ${result.nestedSubmodules}`);
1451014538
// Auth token
14511-
result.authToken = core.getInput('token');
14539+
result.authToken = core.getInput('token', { required: true });
1451214540
// SSH
1451314541
result.sshKey = core.getInput('ssh-key');
1451414542
result.sshKnownHosts = core.getInput('ssh-known-hosts');

src/git-source-provider.ts

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
1919
)
2020
const repositoryUrl = urlHelper.getFetchUrl(settings)
2121

22+
// Determine the default branch
23+
if (!settings.ref && !settings.commit) {
24+
core.startGroup('Determining the default branch')
25+
settings.ref = await githubApiHelper.getDefaultBranch(
26+
settings.authToken,
27+
settings.repositoryOwner,
28+
settings.repositoryName
29+
)
30+
core.endGroup()
31+
}
32+
2233
// Remove conflicting file path
2334
if (fsHelper.fileExistsSync(settings.repositoryPath)) {
2435
await io.rmRF(settings.repositoryPath)

src/github-api-helper.ts

+32
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,38 @@ export async function downloadRepository(
6767
io.rmRF(extractPath)
6868
}
6969

70+
/**
71+
* Looks up the default branch name
72+
*/
73+
export async function getDefaultBranch(
74+
authToken: string,
75+
owner: string,
76+
repo: string
77+
): Promise<string> {
78+
return await retryHelper.execute(async () => {
79+
core.info('Retrieving the default branch name')
80+
const octokit = new github.GitHub(authToken)
81+
const response = await octokit.repos.get({owner, repo})
82+
if (response.status != 200) {
83+
throw new Error(
84+
`Unexpected response from GitHub API. Status: ${response.status}, Data: ${response.data}`
85+
)
86+
}
87+
88+
// Print the default branch
89+
let result = response.data.default_branch
90+
core.info(`Default branch '${result}'`)
91+
assert.ok(result, 'default_branch cannot be empty')
92+
93+
// Prefix with 'refs/heads'
94+
if (!result.startsWith('refs/')) {
95+
result = `refs/heads/${result}`
96+
}
97+
98+
return result
99+
})
100+
}
101+
70102
async function downloadArchive(
71103
authToken: string,
72104
owner: string,

src/input-helper.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ export function getInputs(): IGitSourceSettings {
6868
result.ref = `refs/heads/${result.ref}`
6969
}
7070
}
71-
72-
if (!result.ref && !result.commit) {
73-
result.ref = 'refs/heads/master'
74-
}
7571
}
7672
// SHA?
7773
else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
@@ -110,7 +106,7 @@ export function getInputs(): IGitSourceSettings {
110106
core.debug(`recursive submodules = ${result.nestedSubmodules}`)
111107

112108
// Auth token
113-
result.authToken = core.getInput('token')
109+
result.authToken = core.getInput('token', {required: true})
114110

115111
// SSH
116112
result.sshKey = core.getInput('ssh-key')

0 commit comments

Comments
 (0)