MCPcopy Create free account
hub / github.com/cli/cli / fetchCommitSHA

Function fetchCommitSHA

pkg/cmd/extension/http.go:169–195  ·  view source on GitHub ↗

fetchCommitSHA finds full commit SHA from a target ref in a repo

(httpClient *http.Client, baseRepo ghrepo.Interface, targetRef string)

Source from the content-addressed store, hash-verified

167
168// fetchCommitSHA finds full commit SHA from a target ref in a repo
169func 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}

Callers 2

installGitMethod · 0.85
TestFetchCommitSHAFunction · 0.85

Calls 9

JoinPathFunction · 0.92
NewClientFromHTTPFunction · 0.92
WithHeaderFunction · 0.92
RequestMethod · 0.80
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RepoHostMethod · 0.65
StringMethod · 0.65
CloseMethod · 0.65

Tested by 1

TestFetchCommitSHAFunction · 0.68