(ctx context.Context, client *http.Client, repo string)
| 114 | } |
| 115 | |
| 116 | func getLatestReleaseInfo(ctx context.Context, client *http.Client, repo string) (*ReleaseInfo, error) { |
| 117 | owner, name, err := safeurl.RepoPartsFromNWO(repo) |
| 118 | if err != nil { |
| 119 | return nil, err |
| 120 | } |
| 121 | u, err := safeurl.JoinPathWithHostPrefix("https://api.github.com", "repos", owner, name, "releases", "latest") |
| 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | // The URL stays absolute on purpose: CLI releases live on github.com regardless of the |
| 126 | // host the user has configured, so this must not be rewritten to their API host. |
| 127 | // TODO(api-client-rollout) |
| 128 | // This line of code is part of a mechanical roll out of the api client. |
| 129 | // As a follow up, consider whether the api client can be injected to this call site, rather than constructed |
| 130 | res, err := api.NewClientFromHTTP(client).RequestWithContext(ctx, "github.com", http.MethodGet, u.String(), nil) |
| 131 | if err != nil { |
| 132 | return nil, err |
| 133 | } |
| 134 | defer func() { |
| 135 | _, _ = io.Copy(io.Discard, res.Body) |
| 136 | res.Body.Close() |
| 137 | }() |
| 138 | if res.StatusCode != 200 { |
| 139 | return nil, api.UnexpectedStatusError(res) |
| 140 | } |
| 141 | dec := json.NewDecoder(res.Body) |
| 142 | var latestRelease ReleaseInfo |
| 143 | if err := dec.Decode(&latestRelease); err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | return &latestRelease, nil |
| 147 | } |
| 148 | |
| 149 | func getStateEntry(stateFilePath string) (*StateEntry, error) { |
| 150 | content, err := os.ReadFile(stateFilePath) |
no test coverage detected