determine default branch (#278)
This commit is contained in:
		@@ -19,6 +19,17 @@ 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)
 | 
			
		||||
 
 | 
			
		||||
@@ -67,6 +67,38 @@ export async function downloadRepository(
 | 
			
		||||
  io.rmRF(extractPath)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Looks up the default branch name
 | 
			
		||||
 */
 | 
			
		||||
export async function getDefaultBranch(
 | 
			
		||||
  authToken: string,
 | 
			
		||||
  owner: string,
 | 
			
		||||
  repo: string
 | 
			
		||||
): Promise<string> {
 | 
			
		||||
  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}`
 | 
			
		||||
      )
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 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}`
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return result
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function downloadArchive(
 | 
			
		||||
  authToken: string,
 | 
			
		||||
  owner: string,
 | 
			
		||||
 
 | 
			
		||||
@@ -68,10 +68,6 @@ export function getInputs(): IGitSourceSettings {
 | 
			
		||||
        result.ref = `refs/heads/${result.ref}`
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!result.ref && !result.commit) {
 | 
			
		||||
      result.ref = 'refs/heads/master'
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  // SHA?
 | 
			
		||||
  else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
 | 
			
		||||
@@ -110,7 +106,7 @@ export function getInputs(): IGitSourceSettings {
 | 
			
		||||
  core.debug(`recursive submodules = ${result.nestedSubmodules}`)
 | 
			
		||||
 | 
			
		||||
  // Auth token
 | 
			
		||||
  result.authToken = core.getInput('token')
 | 
			
		||||
  result.authToken = core.getInput('token', {required: true})
 | 
			
		||||
 | 
			
		||||
  // SSH
 | 
			
		||||
  result.sshKey = core.getInput('ssh-key')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user