MCPcopy Create free account
hub / github.com/github/gh-aw / getRepoDefaultBranch

Function getRepoDefaultBranch

pkg/cli/update_workflows.go:389–418  ·  view source on GitHub ↗

getRepoDefaultBranch fetches the default branch name for a repository.

(ctx context.Context, repo string)

Source from the content-addressed store, hash-verified

387
388// getRepoDefaultBranch fetches the default branch name for a repository.
389func getRepoDefaultBranch(ctx context.Context, repo string) (string, error) {
390 output, err := workflow.RunGHContext(ctx, "Fetching repo info...", "api", "/repos/"+repo, "--jq", ".default_branch")
391 if err != nil && gitutil.IsAuthError(err.Error()) {
392 updateLog.Printf("GitHub API auth failed for %s, retrying without token", repo)
393 body, fallbackErr := fetchPublicGitHubAPI(ctx, "/repos/"+repo)
394 if fallbackErr != nil {
395 return "", fmt.Errorf("failed (with token: %w; without token: %w)", err, fallbackErr)
396 }
397 var result struct {
398 DefaultBranch string `json:"default_branch"`
399 }
400 if fallbackErr = json.Unmarshal(body, &result); fallbackErr != nil {
401 return "", fmt.Errorf("failed to parse repo response: %w", fallbackErr)
402 }
403 if result.DefaultBranch == "" {
404 return "", fmt.Errorf("empty default branch returned for %s", repo)
405 }
406 return result.DefaultBranch, nil
407 }
408 if err != nil {
409 return "", err
410 }
411
412 branch := strings.TrimSpace(string(output))
413 if branch == "" {
414 return "", fmt.Errorf("empty default branch returned for %s", repo)
415 }
416
417 return branch, nil
418}
419
420// getLatestBranchCommitSHA fetches the latest commit SHA for a given branch.
421func getLatestBranchCommitSHA(ctx context.Context, repo, branch string) (string, error) {

Calls 6

RunGHContextFunction · 0.92
IsAuthErrorFunction · 0.92
fetchPublicGitHubAPIFunction · 0.85
ErrorMethod · 0.45
PrintfMethod · 0.45
ErrorfMethod · 0.45

Tested by 1