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

Function FetchRefSHA

pkg/cmd/release/shared/fetch.go:140–176  ·  view source on GitHub ↗
(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string)

Source from the content-addressed store, hash-verified

138}
139
140func 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

Callers 3

verifyRunFunction · 0.92
verifyAssetRunFunction · 0.92
TestFetchRefSHAFunction · 0.85

Calls 11

JoinPathWithHostPrefixFunction · 0.92
RESTPrefixFunction · 0.92
HandleHTTPErrorFunction · 0.92
RepoHostMethod · 0.65
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
StringMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.65
CopyMethod · 0.45

Tested by 1

TestFetchRefSHAFunction · 0.68