Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix default branch for .wiki and when using ssh #284

Merged
merged 4 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __test__/git-auth-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ async function setup(testName: string): Promise<void> {
),
env: {},
fetch: jest.fn(),
getDefaultBranch: jest.fn(),
getWorkingDirectory: jest.fn(() => workspace),
init: jest.fn(),
isDetached: jest.fn(),
Expand Down
1 change: 1 addition & 0 deletions __test__/git-directory-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ async function setup(testName: string): Promise<void> {
config: jest.fn(),
configExists: jest.fn(),
fetch: jest.fn(),
getDefaultBranch: jest.fn(),
getWorkingDirectory: jest.fn(() => repositoryPath),
init: jest.fn(),
isDetached: jest.fn(),
Expand Down
70 changes: 59 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5827,6 +5827,33 @@ class GitCommandManager {
}));
});
}
getDefaultBranch(repositoryUrl) {
return __awaiter(this, void 0, void 0, function* () {
let output;
yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
output = yield this.execGit([
'ls-remote',
'--quiet',
'--exit-code',
'--symref',
repositoryUrl,
'HEAD'
]);
}));
if (output) {
// Satisfy compiler, will always be set
for (let line of output.stdout.trim().split('\n')) {
line = line.trim();
if (line.startsWith('ref:') || line.endsWith('HEAD')) {
return line
.substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length)
.trim();
}
}
}
throw new Error('Unexpected output when retrieving default branch');
});
}
getWorkingDirectory() {
return this.workingDirectory;
}
Expand Down Expand Up @@ -6114,12 +6141,6 @@ 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);
Expand Down Expand Up @@ -6172,6 +6193,17 @@ function getSource(settings) {
core.startGroup('Setting up auth');
yield authHelper.configureAuth();
core.endGroup();
// Determine the default branch
if (!settings.ref && !settings.commit) {
core.startGroup('Determining the default branch');
if (settings.sshKey) {
settings.ref = yield git.getDefaultBranch(repositoryUrl);
}
else {
settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName);
}
core.endGroup();
}
// LFS install
if (settings.lfs) {
yield git.lfsInstall();
Expand Down Expand Up @@ -9531,6 +9563,11 @@ const v4_1 = __importDefault(__webpack_require__(826));
const IS_WINDOWS = process.platform === 'win32';
function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) {
return __awaiter(this, void 0, void 0, function* () {
// Determine the default branch
if (!ref && !commit) {
core.info('Determining the default branch');
ref = yield getDefaultBranch(authToken, owner, repo);
}
// Download the archive
let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
core.info('Downloading the archive');
Expand Down Expand Up @@ -9583,14 +9620,25 @@ function getDefaultBranch(authToken, owner, repo) {
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}`);
let result;
try {
// Get the default branch from the repo info
const response = yield octokit.repos.get({ owner, repo });
result = response.data.default_branch;
assert.ok(result, 'default_branch cannot be empty');
}
catch (err) {
// Handle .wiki repo
if (err['status'] === 404 && repo.toUpperCase().endsWith('.WIKI')) {
result = 'master';
}
// Otherwise error
else {
throw err;
}
}
// 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}`;
Expand Down
29 changes: 29 additions & 0 deletions src/git-command-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface IGitCommandManager {
): Promise<void>
configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
fetch(refSpec: string[], fetchDepth?: number): Promise<void>
getDefaultBranch(repositoryUrl: string): Promise<string>
getWorkingDirectory(): string
init(): Promise<void>
isDetached(): Promise<boolean>
Expand Down Expand Up @@ -195,6 +196,34 @@ class GitCommandManager {
})
}

async getDefaultBranch(repositoryUrl: string): Promise<string> {
let output: GitOutput | undefined
await retryHelper.execute(async () => {
output = await this.execGit([
'ls-remote',
'--quiet',
'--exit-code',
'--symref',
repositoryUrl,
'HEAD'
])
})

if (output) {
// Satisfy compiler, will always be set
for (let line of output.stdout.trim().split('\n')) {
line = line.trim()
if (line.startsWith('ref:') || line.endsWith('HEAD')) {
return line
.substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length)
.trim()
}
}
}

throw new Error('Unexpected output when retrieving default branch')
}

getWorkingDirectory(): string {
return this.workingDirectory
}
Expand Down
26 changes: 15 additions & 11 deletions src/git-source-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
)
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)
Expand Down Expand Up @@ -114,6 +103,21 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
await authHelper.configureAuth()
core.endGroup()

// Determine the default branch
if (!settings.ref && !settings.commit) {
core.startGroup('Determining the default branch')
if (settings.sshKey) {
settings.ref = await git.getDefaultBranch(repositoryUrl)
} else {
settings.ref = await githubApiHelper.getDefaultBranch(
settings.authToken,
settings.repositoryOwner,
settings.repositoryName
)
}
core.endGroup()
}

// LFS install
if (settings.lfs) {
await git.lfsInstall()
Expand Down
28 changes: 21 additions & 7 deletions src/github-api-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export async function downloadRepository(
commit: string,
repositoryPath: string
): Promise<void> {
// Determine the default branch
if (!ref && !commit) {
core.info('Determining the default branch')
ref = await getDefaultBranch(authToken, owner, repo)
}

// Download the archive
let archiveData = await retryHelper.execute(async () => {
core.info('Downloading the archive')
Expand Down Expand Up @@ -78,17 +84,25 @@ export async function getDefaultBranch(
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}`
)
let result: string
try {
// Get the default branch from the repo info
const response = await octokit.repos.get({owner, repo})
result = response.data.default_branch
assert.ok(result, 'default_branch cannot be empty')
} catch (err) {
// Handle .wiki repo
if (err['status'] === 404 && repo.toUpperCase().endsWith('.WIKI')) {
result = 'master'
}
// Otherwise error
else {
throw err
}
}

// 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/')) {
Expand Down