fetchCommitSHA finds full commit SHA from a target ref in a repo
(httpClient *http.Client, baseRepo ghrepo.Interface, targetRef string)
| 167 | |
| 168 | // fetchCommitSHA finds full commit SHA from a target ref in a repo |
| 169 | func fetchCommitSHA(httpClient *http.Client, baseRepo ghrepo.Interface, targetRef string) (string, error) { |
| 170 | path, err := safeurl.JoinPath("repos", baseRepo.RepoOwner(), baseRepo.RepoName(), "commits", targetRef) |
| 171 | if err != nil { |
| 172 | return "", err |
| 173 | } |
| 174 | |
| 175 | // The response body is a bare SHA rather than JSON, so Request is used instead of REST. |
| 176 | // TODO(api-client-rollout) |
| 177 | // This line of code is part of a mechanical roll out of the api client. |
| 178 | // As a follow up, consider whether the api client can be injected to this call site, rather than constructed |
| 179 | resp, err := api.NewClientFromHTTP(httpClient).Request(baseRepo.RepoHost(), http.MethodGet, path.String(), nil, |
| 180 | api.WithHeader("Accept", "application/vnd.github.v3.sha")) |
| 181 | if err != nil { |
| 182 | if httpErr, ok := errors.AsType[api.HTTPError](err); ok && httpErr.StatusCode == http.StatusUnprocessableEntity { |
| 183 | return "", commitNotFoundErr |
| 184 | } |
| 185 | return "", err |
| 186 | } |
| 187 | defer resp.Body.Close() |
| 188 | |
| 189 | body, err := io.ReadAll(resp.Body) |
| 190 | if err != nil { |
| 191 | return "", err |
| 192 | } |
| 193 | |
| 194 | return string(body), nil |
| 195 | } |