fetchCommitSHA finds full commit SHA from a target ref in a repo
(httpClient *http.Client, baseRepo ghrepo.Interface, targetRef string)
| 173 | |
| 174 | // fetchCommitSHA finds full commit SHA from a target ref in a repo |
| 175 | func fetchCommitSHA(httpClient *http.Client, baseRepo ghrepo.Interface, targetRef string) (string, error) { |
| 176 | url, err := safeurl.JoinPathWithHostPrefix(ghinstance.RESTPrefix(baseRepo.RepoHost()), "repos", baseRepo.RepoOwner(), baseRepo.RepoName(), "commits", targetRef) |
| 177 | if err != nil { |
| 178 | return "", err |
| 179 | } |
| 180 | req, err := http.NewRequest("GET", url.String(), nil) |
| 181 | if err != nil { |
| 182 | return "", err |
| 183 | } |
| 184 | |
| 185 | req.Header.Set("Accept", "application/vnd.github.v3.sha") |
| 186 | // TODO(api-client-rollout) |
| 187 | // This has been deferred from moving to api.Client due to its custom Accept header and bare SHA response body. |
| 188 | resp, err := httpClient.Do(req) |
| 189 | if err != nil { |
| 190 | return "", err |
| 191 | } |
| 192 | |
| 193 | defer resp.Body.Close() |
| 194 | if resp.StatusCode == 422 { |
| 195 | return "", commitNotFoundErr |
| 196 | } |
| 197 | if resp.StatusCode > 299 { |
| 198 | return "", api.HandleHTTPError(resp) |
| 199 | } |
| 200 | |
| 201 | body, err := io.ReadAll(resp.Body) |
| 202 | if err != nil { |
| 203 | return "", err |
| 204 | } |
| 205 | |
| 206 | return string(body), nil |
| 207 | } |
no test coverage detected