@@ -5827,6 +5827,33 @@ class GitCommandManager {
5827
5827
}));
5828
5828
});
5829
5829
}
5830
+ getDefaultBranch(repositoryUrl) {
5831
+ return __awaiter(this, void 0, void 0, function* () {
5832
+ let output;
5833
+ yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
5834
+ output = yield this.execGit([
5835
+ 'ls-remote',
5836
+ '--quiet',
5837
+ '--exit-code',
5838
+ '--symref',
5839
+ repositoryUrl,
5840
+ 'HEAD'
5841
+ ]);
5842
+ }));
5843
+ if (output) {
5844
+ // Satisfy compiler, will always be set
5845
+ for (let line of output.stdout.trim().split('\n')) {
5846
+ line = line.trim();
5847
+ if (line.startsWith('ref:') || line.endsWith('HEAD')) {
5848
+ return line
5849
+ .substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length)
5850
+ .trim();
5851
+ }
5852
+ }
5853
+ }
5854
+ throw new Error('Unexpected output when retrieving default branch');
5855
+ });
5856
+ }
5830
5857
getWorkingDirectory() {
5831
5858
return this.workingDirectory;
5832
5859
}
@@ -6114,12 +6141,6 @@ function getSource(settings) {
6114
6141
// Repository URL
6115
6142
core.info(`Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}`);
6116
6143
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
- }
6123
6144
// Remove conflicting file path
6124
6145
if (fsHelper.fileExistsSync(settings.repositoryPath)) {
6125
6146
yield io.rmRF(settings.repositoryPath);
@@ -6172,6 +6193,17 @@ function getSource(settings) {
6172
6193
core.startGroup('Setting up auth');
6173
6194
yield authHelper.configureAuth();
6174
6195
core.endGroup();
6196
+ // Determine the default branch
6197
+ if (!settings.ref && !settings.commit) {
6198
+ core.startGroup('Determining the default branch');
6199
+ if (settings.sshKey) {
6200
+ settings.ref = yield git.getDefaultBranch(repositoryUrl);
6201
+ }
6202
+ else {
6203
+ settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName);
6204
+ }
6205
+ core.endGroup();
6206
+ }
6175
6207
// LFS install
6176
6208
if (settings.lfs) {
6177
6209
yield git.lfsInstall();
@@ -9531,6 +9563,11 @@ const v4_1 = __importDefault(__webpack_require__(826));
9531
9563
const IS_WINDOWS = process.platform === 'win32';
9532
9564
function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) {
9533
9565
return __awaiter(this, void 0, void 0, function* () {
9566
+ // Determine the default branch
9567
+ if (!ref && !commit) {
9568
+ core.info('Determining the default branch');
9569
+ ref = yield getDefaultBranch(authToken, owner, repo);
9570
+ }
9534
9571
// Download the archive
9535
9572
let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
9536
9573
core.info('Downloading the archive');
@@ -9583,14 +9620,25 @@ function getDefaultBranch(authToken, owner, repo) {
9583
9620
return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
9584
9621
core.info('Retrieving the default branch name');
9585
9622
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}`);
9623
+ let result;
9624
+ try {
9625
+ // Get the default branch from the repo info
9626
+ const response = yield octokit.repos.get({ owner, repo });
9627
+ result = response.data.default_branch;
9628
+ assert.ok(result, 'default_branch cannot be empty');
9629
+ }
9630
+ catch (err) {
9631
+ // Handle .wiki repo
9632
+ if (err['status'] === 404 && repo.toUpperCase().endsWith('.WIKI')) {
9633
+ result = 'master';
9634
+ }
9635
+ // Otherwise error
9636
+ else {
9637
+ throw err;
9638
+ }
9589
9639
}
9590
9640
// Print the default branch
9591
- let result = response.data.default_branch;
9592
9641
core.info(`Default branch '${result}'`);
9593
- assert.ok(result, 'default_branch cannot be empty');
9594
9642
// Prefix with 'refs/heads'
9595
9643
if (!result.startsWith('refs/')) {
9596
9644
result = `refs/heads/${result}`;
0 commit comments