Read-only mirror — the public repositories of the Forgejo instance, refreshed every 10 minutes. The live instance (fg.dieschoenewolke.de) is reachable only over the VPN; clone here with https://git.dieschoenewolke.de/git/<owner>/<repo>.git.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreric sciple <ericsciple@users.noreply.github.com>2020-06-18 10:20:33 -0400
committerGitHub <noreply@github.com>2020-06-18 10:20:33 -0400
commitb8cb77a290d6bf1500962ef3fdb92249d45b0ac6 (patch)
tree456f207b7d487d1414c5d954dac17ce0d33789ca /src
parent33f740f1eb34a02546c9a2eb2d6a725d7c67ca92 (diff)
fix default branch for .wiki and when using ssh (#284)
Diffstat (limited to 'src')
-rw-r--r--src/git-command-manager.ts29
-rw-r--r--src/git-source-provider.ts26
-rw-r--r--src/github-api-helper.ts28
3 files changed, 65 insertions, 18 deletions
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts
index 9d2d45f..8bf3aa1 100644
--- a/src/git-command-manager.ts
+++ b/src/git-command-manager.ts
@@ -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>
@@ -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
}
diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts
index 25fba04..366ff33 100644
--- a/src/git-source-provider.ts
+++ b/src/git-source-provider.ts
@@ -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)
@@ -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()
diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts
index 7a09638..8bbcf2d 100644
--- a/src/github-api-helper.ts
+++ b/src/github-api-helper.ts
@@ -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')
@@ -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/')) {