@@ -5827,6 +5827,33 @@ class GitCommandManager {
58275827 }));
58285828 });
58295829 }
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+ }
58305857 getWorkingDirectory() {
58315858 return this.workingDirectory;
58325859 }
@@ -6114,12 +6141,6 @@ function getSource(settings) {
61146141 // Repository URL
61156142 core.info(`Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}`);
61166143 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- }
61236144 // Remove conflicting file path
61246145 if (fsHelper.fileExistsSync(settings.repositoryPath)) {
61256146 yield io.rmRF(settings.repositoryPath);
@@ -6172,6 +6193,17 @@ function getSource(settings) {
61726193 core.startGroup('Setting up auth');
61736194 yield authHelper.configureAuth();
61746195 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+ }
61756207 // LFS install
61766208 if (settings.lfs) {
61776209 yield git.lfsInstall();
@@ -9531,6 +9563,11 @@ const v4_1 = __importDefault(__webpack_require__(826));
95319563const IS_WINDOWS = process.platform === 'win32';
95329564function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) {
95339565 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+ }
95349571 // Download the archive
95359572 let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
95369573 core.info('Downloading the archive');
@@ -9583,14 +9620,25 @@ function getDefaultBranch(authToken, owner, repo) {
95839620 return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
95849621 core.info('Retrieving the default branch name');
95859622 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+ }
95899639 }
95909640 // Print the default branch
9591- let result = response.data.default_branch;
95929641 core.info(`Default branch '${result}'`);
9593- assert.ok(result, 'default_branch cannot be empty');
95949642 // Prefix with 'refs/heads'
95959643 if (!result.startsWith('refs/')) {
95969644 result = `refs/heads/${result}`;
0 commit comments