fetchDraftRelease returns the first draft release that has tagName as its pending tag.
(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string)
| 244 | |
| 245 | // fetchDraftRelease returns the first draft release that has tagName as its pending tag. |
| 246 | func fetchDraftRelease(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string) (*Release, error) { |
| 247 | // First use GraphQL to find a draft release by pending tag name, since REST doesn't have this ability. |
| 248 | var query struct { |
| 249 | Repository struct { |
| 250 | Release *struct { |
| 251 | DatabaseID int64 |
| 252 | IsDraft bool |
| 253 | } `graphql:"release(tagName: $tagName)"` |
| 254 | } `graphql:"repository(owner: $owner, name: $name)"` |
| 255 | } |
| 256 | |
| 257 | variables := map[string]interface{}{ |
| 258 | "owner": githubv4.String(repo.RepoOwner()), |
| 259 | "name": githubv4.String(repo.RepoName()), |
| 260 | "tagName": githubv4.String(tagName), |
| 261 | } |
| 262 | |
| 263 | gql := api.NewClientFromHTTP(httpClient) |
| 264 | if err := gql.QueryWithContext(ctx, repo.RepoHost(), "RepositoryReleaseByTag", &query, variables); err != nil { |
| 265 | return nil, err |
| 266 | } |
| 267 | |
| 268 | if query.Repository.Release == nil || !query.Repository.Release.IsDraft { |
| 269 | return nil, ErrReleaseNotFound |
| 270 | } |
| 271 | |
| 272 | // Then, use REST to get information about the draft release. In theory, we could have fetched |
| 273 | // all the necessary information via GraphQL, but REST is safer for backwards compatibility. |
| 274 | path, err := safeurl.JoinPathWithHostPrefix(ghinstance.RESTPrefix(repo.RepoHost()), "repos", repo.RepoOwner(), repo.RepoName(), "releases", strconv.FormatInt(query.Repository.Release.DatabaseID, 10)) |
| 275 | if err != nil { |
| 276 | return nil, err |
| 277 | } |
| 278 | return fetchReleasePath(ctx, httpClient, path) |
| 279 | } |
| 280 | |
| 281 | func fetchReleasePath(ctx context.Context, httpClient *http.Client, url safeurl.SafeURL) (*Release, error) { |
| 282 | req, err := http.NewRequestWithContext(ctx, "GET", url.String(), nil) |
no test coverage detected