| 136 | } |
| 137 | |
| 138 | func FetchRefSHA(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string) (string, error) { |
| 139 | path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName(), "git", "ref", fmt.Sprintf("tags/%s", tagName)) |
| 140 | if err != nil { |
| 141 | return "", err |
| 142 | } |
| 143 | |
| 144 | // TODO(api-client-rollout) |
| 145 | // This line of code is part of a mechanical roll out of the api client. |
| 146 | // As a follow up, consider whether the api client can be injected to this call site, rather than constructed |
| 147 | resp, err := api.NewClientFromHTTP(httpClient).RequestWithContext(ctx, repo.RepoHost(), http.MethodGet, path.String(), nil) |
| 148 | if err != nil { |
| 149 | if isNotFound(err) { |
| 150 | return "", ErrReleaseNotFound |
| 151 | } |
| 152 | return "", err |
| 153 | } |
| 154 | defer resp.Body.Close() |
| 155 | |
| 156 | var ref struct { |
| 157 | Object struct { |
| 158 | SHA string `json:"sha"` |
| 159 | } `json:"object"` |
| 160 | } |
| 161 | |
| 162 | if err := json.NewDecoder(resp.Body).Decode(&ref); err != nil { |
| 163 | return "", fmt.Errorf("failed to parse ref response: %w", err) |
| 164 | } |
| 165 | |
| 166 | return ref.Object.SHA, nil |
| 167 | } |
| 168 | |
| 169 | // DigestAlgForRef returns the digest algorithm name corresponding to the given |
| 170 | // git ref SHA. SHA-1 git object IDs are 40 hex characters and SHA-256 git |