resolveRefToSHAViaPublicAPI resolves a git ref to its commit SHA using an unauthenticated call to the public GitHub API. Used as a last-resort fallback when both authenticated API and git ls-remote fail.
(ctx context.Context, owner, repo, ref string)
| 565 | // unauthenticated call to the public GitHub API. Used as a last-resort fallback |
| 566 | // when both authenticated API and git ls-remote fail. |
| 567 | func resolveRefToSHAViaPublicAPI(ctx context.Context, owner, repo, ref string) (string, error) { |
| 568 | remoteLog.Printf("Attempting unauthenticated public API ref resolution for %s/%s@%s", owner, repo, ref) |
| 569 | apiURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits/%s", |
| 570 | owner, repo, url.PathEscape(ref)) |
| 571 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil) |
| 572 | if err != nil { |
| 573 | return "", err |
| 574 | } |
| 575 | req.Header.Set("Accept", "application/vnd.github+json") |
| 576 | |
| 577 | resp, err := publicAPIClient.Do(req) |
| 578 | if err != nil { |
| 579 | return "", err |
| 580 | } |
| 581 | defer resp.Body.Close() |
| 582 | |
| 583 | body, err := io.ReadAll(resp.Body) |
| 584 | if err != nil { |
| 585 | return "", err |
| 586 | } |
| 587 | if resp.StatusCode != http.StatusOK { |
| 588 | return "", fmt.Errorf("unauthenticated public API failed for %s/%s@%s: HTTP %d: %s", owner, repo, ref, resp.StatusCode, strings.TrimSpace(string(body))) |
| 589 | } |
| 590 | |
| 591 | var result struct { |
| 592 | SHA string `json:"sha"` |
| 593 | } |
| 594 | if err := json.Unmarshal(body, &result); err != nil { |
| 595 | return "", fmt.Errorf("failed to parse commit response: %w", err) |
| 596 | } |
| 597 | if result.SHA == "" || len(result.SHA) != 40 || !gitutil.IsHexString(result.SHA) { |
| 598 | return "", fmt.Errorf("invalid SHA returned from public API: %q", result.SHA) |
| 599 | } |
| 600 | return result.SHA, nil |
| 601 | } |
| 602 | |
| 603 | // downloadFileViaGit downloads a file from a Git repository using git commands |
| 604 | // This is a fallback for when GitHub API authentication fails |
no test coverage detected