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