getLatestBranchCommitSHA fetches the latest commit SHA for a given branch.
(ctx context.Context, repo, branch string)
| 419 | |
| 420 | // getLatestBranchCommitSHA fetches the latest commit SHA for a given branch. |
| 421 | func getLatestBranchCommitSHA(ctx context.Context, repo, branch string) (string, error) { |
| 422 | // URL-encode the branch name since it may contain slashes (e.g. "feature/foo") |
| 423 | endpoint := fmt.Sprintf("/repos/%s/branches/%s", repo, url.PathEscape(branch)) |
| 424 | output, err := workflow.RunGHContext(ctx, "Fetching branch info...", "api", endpoint, "--jq", ".commit.sha") |
| 425 | if err != nil && gitutil.IsAuthError(err.Error()) { |
| 426 | updateLog.Printf("GitHub API auth failed for branch %s of %s, retrying without token", branch, repo) |
| 427 | body, fallbackErr := fetchPublicGitHubAPI(ctx, endpoint) |
| 428 | if fallbackErr != nil { |
| 429 | return "", fmt.Errorf("failed (with token: %w; without token: %w)", err, fallbackErr) |
| 430 | } |
| 431 | var result struct { |
| 432 | Commit struct { |
| 433 | SHA string `json:"sha"` |
| 434 | } `json:"commit"` |
| 435 | } |
| 436 | if fallbackErr = json.Unmarshal(body, &result); fallbackErr != nil { |
| 437 | return "", fmt.Errorf("failed to parse branch response: %w", fallbackErr) |
| 438 | } |
| 439 | if result.Commit.SHA == "" { |
| 440 | return "", fmt.Errorf("empty commit SHA returned for branch %s", branch) |
| 441 | } |
| 442 | return result.Commit.SHA, nil |
| 443 | } |
| 444 | if err != nil { |
| 445 | return "", err |
| 446 | } |
| 447 | |
| 448 | sha := strings.TrimSpace(string(output)) |
| 449 | if sha == "" { |
| 450 | return "", fmt.Errorf("empty commit SHA returned for branch %s", branch) |
| 451 | } |
| 452 | |
| 453 | return sha, nil |
| 454 | } |
| 455 | |
| 456 | type workflowUpdateDeps struct { |
| 457 | runReleasesAPI func(ctx context.Context, repo string) ([]byte, error) |