Skip to content

Commit fb6f360

Browse files
authoredJun 18, 2020
fix default branch for .wiki and when using ssh (#284)
1 parent b4483ad commit fb6f360

6 files changed

+126
-29
lines changed
 

‎__test__/git-auth-helper.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ async function setup(testName: string): Promise<void> {
714714
),
715715
env: {},
716716
fetch: jest.fn(),
717+
getDefaultBranch: jest.fn(),
717718
getWorkingDirectory: jest.fn(() => workspace),
718719
init: jest.fn(),
719720
isDetached: jest.fn(),

‎__test__/git-directory-helper.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ async function setup(testName: string): Promise<void> {
408408
config: jest.fn(),
409409
configExists: jest.fn(),
410410
fetch: jest.fn(),
411+
getDefaultBranch: jest.fn(),
411412
getWorkingDirectory: jest.fn(() => repositoryPath),
412413
init: jest.fn(),
413414
isDetached: jest.fn(),

‎dist/index.js

+59-11
Original file line numberDiff line numberDiff line change
@@ -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));
95319563
const IS_WINDOWS = process.platform === 'win32';
95329564
function 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}`;

‎src/git-command-manager.ts

+29
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface IGitCommandManager {
2525
): Promise<void>
2626
configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
2727
fetch(refSpec: string[], fetchDepth?: number): Promise<void>
28+
getDefaultBranch(repositoryUrl: string): Promise<string>
2829
getWorkingDirectory(): string
2930
init(): Promise<void>
3031
isDetached(): Promise<boolean>
@@ -195,6 +196,34 @@ class GitCommandManager {
195196
})
196197
}
197198

199+
async getDefaultBranch(repositoryUrl: string): Promise<string> {
200+
let output: GitOutput | undefined
201+
await retryHelper.execute(async () => {
202+
output = await this.execGit([
203+
'ls-remote',
204+
'--quiet',
205+
'--exit-code',
206+
'--symref',
207+
repositoryUrl,
208+
'HEAD'
209+
])
210+
})
211+
212+
if (output) {
213+
// Satisfy compiler, will always be set
214+
for (let line of output.stdout.trim().split('\n')) {
215+
line = line.trim()
216+
if (line.startsWith('ref:') || line.endsWith('HEAD')) {
217+
return line
218+
.substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length)
219+
.trim()
220+
}
221+
}
222+
}
223+
224+
throw new Error('Unexpected output when retrieving default branch')
225+
}
226+
198227
getWorkingDirectory(): string {
199228
return this.workingDirectory
200229
}

‎src/git-source-provider.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ 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-
3322
// Remove conflicting file path
3423
if (fsHelper.fileExistsSync(settings.repositoryPath)) {
3524
await io.rmRF(settings.repositoryPath)
@@ -114,6 +103,21 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
114103
await authHelper.configureAuth()
115104
core.endGroup()
116105

106+
// Determine the default branch
107+
if (!settings.ref && !settings.commit) {
108+
core.startGroup('Determining the default branch')
109+
if (settings.sshKey) {
110+
settings.ref = await git.getDefaultBranch(repositoryUrl)
111+
} else {
112+
settings.ref = await githubApiHelper.getDefaultBranch(
113+
settings.authToken,
114+
settings.repositoryOwner,
115+
settings.repositoryName
116+
)
117+
}
118+
core.endGroup()
119+
}
120+
117121
// LFS install
118122
if (settings.lfs) {
119123
await git.lfsInstall()

‎src/github-api-helper.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export async function downloadRepository(
1919
commit: string,
2020
repositoryPath: string
2121
): Promise<void> {
22+
// Determine the default branch
23+
if (!ref && !commit) {
24+
core.info('Determining the default branch')
25+
ref = await getDefaultBranch(authToken, owner, repo)
26+
}
27+
2228
// Download the archive
2329
let archiveData = await retryHelper.execute(async () => {
2430
core.info('Downloading the archive')
@@ -78,17 +84,25 @@ export async function getDefaultBranch(
7884
return await retryHelper.execute(async () => {
7985
core.info('Retrieving the default branch name')
8086
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-
)
87+
let result: string
88+
try {
89+
// Get the default branch from the repo info
90+
const response = await octokit.repos.get({owner, repo})
91+
result = response.data.default_branch
92+
assert.ok(result, 'default_branch cannot be empty')
93+
} catch (err) {
94+
// Handle .wiki repo
95+
if (err['status'] === 404 && repo.toUpperCase().endsWith('.WIKI')) {
96+
result = 'master'
97+
}
98+
// Otherwise error
99+
else {
100+
throw err
101+
}
86102
}
87103

88104
// Print the default branch
89-
let result = response.data.default_branch
90105
core.info(`Default branch '${result}'`)
91-
assert.ok(result, 'default_branch cannot be empty')
92106

93107
// Prefix with 'refs/heads'
94108
if (!result.startsWith('refs/')) {

0 commit comments

Comments
 (0)
Please sign in to comment.