getRepoDefaultBranch fetches the default branch name for a repository.
(ctx context.Context, repo string)
| 387 | |
| 388 | // getRepoDefaultBranch fetches the default branch name for a repository. |
| 389 | func 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. |
| 421 | func getLatestBranchCommitSHA(ctx context.Context, repo, branch string) (string, error) { |