| 136 | } |
| 137 | |
| 138 | func FetchRefSHA(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string) (string, error) { |
| 139 | path := fmt.Sprintf("repos/%s/%s/git/ref/tags/%s", repo.RepoOwner(), repo.RepoName(), tagName) |
| 140 | req, err := http.NewRequestWithContext(ctx, "GET", ghinstance.RESTPrefix(repo.RepoHost())+path, nil) |
| 141 | if err != nil { |
| 142 | return "", err |
| 143 | } |
| 144 | |
| 145 | resp, err := httpClient.Do(req) |
| 146 | if err != nil { |
| 147 | return "", err |
| 148 | } |
| 149 | defer resp.Body.Close() |
| 150 | |
| 151 | if resp.StatusCode == http.StatusNotFound { |
| 152 | _, _ = io.Copy(io.Discard, resp.Body) |
| 153 | return "", ErrReleaseNotFound |
| 154 | } |
| 155 | |
| 156 | if resp.StatusCode > 299 { |
| 157 | return "", api.HandleHTTPError(resp) |
| 158 | } |
| 159 | |
| 160 | var ref struct { |
| 161 | Object struct { |
| 162 | SHA string `json:"sha"` |
| 163 | } `json:"object"` |
| 164 | } |
| 165 | |
| 166 | if err := json.NewDecoder(resp.Body).Decode(&ref); err != nil { |
| 167 | return "", fmt.Errorf("failed to parse ref response: %w", err) |
| 168 | } |
| 169 | |
| 170 | return ref.Object.SHA, nil |
| 171 | } |
| 172 | |
| 173 | // DigestAlgForRef returns the digest algorithm name corresponding to the given |
| 174 | // git ref SHA. SHA-1 git object IDs are 40 hex characters and SHA-256 git |