FetchRelease finds a published repository release by its tagName, or a draft release by its pending tag name.
(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string)
| 190 | |
| 191 | // FetchRelease finds a published repository release by its tagName, or a draft release by its pending tag name. |
| 192 | func FetchRelease(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string) (*Release, error) { |
| 193 | publishedURL, err := safeurl.JoinPathWithHostPrefix(ghinstance.RESTPrefix(repo.RepoHost()), "repos", repo.RepoOwner(), repo.RepoName(), "releases", "tags", tagName) |
| 194 | if err != nil { |
| 195 | return nil, err |
| 196 | } |
| 197 | |
| 198 | cc, cancel := context.WithCancel(ctx) |
| 199 | results := make(chan fetchResult, 2) |
| 200 | |
| 201 | // published release lookup |
| 202 | go func() { |
| 203 | release, err := fetchReleasePath(cc, httpClient, publishedURL) |
| 204 | results <- fetchResult{release: release, error: err} |
| 205 | }() |
| 206 | |
| 207 | // draft release lookup |
| 208 | go func() { |
| 209 | release, err := fetchDraftRelease(cc, httpClient, repo, tagName) |
| 210 | results <- fetchResult{release: release, error: err} |
| 211 | }() |
| 212 | |
| 213 | // Prefer a release found by either lookup. A single failed lookup, such as |
| 214 | // the draft lookup when unauthenticated, must not mask a release found by |
| 215 | // the other; only report an error when both lookups fail. |
| 216 | first := <-results |
| 217 | if first.error == nil { |
| 218 | cancel() |
| 219 | <-results // drain the channel |
| 220 | return first.release, nil |
| 221 | } |
| 222 | |
| 223 | second := <-results |
| 224 | cancel() // satisfy the linter even though no goroutines are running anymore |
| 225 | if second.error == nil { |
| 226 | return second.release, nil |
| 227 | } |
| 228 | |
| 229 | // Both lookups failed; prefer reporting the release as not found. |
| 230 | if errors.Is(second.error, ErrReleaseNotFound) { |
| 231 | return nil, second.error |
| 232 | } |
| 233 | return nil, first.error |
| 234 | } |
| 235 | |
| 236 | // FetchLatestRelease finds the latest published release for a repository. |
| 237 | func FetchLatestRelease(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface) (*Release, error) { |
no test coverage detected