GetPullRequestDatabaseID retrieves the database ID and URL of a pull request given its number in a repository.
(ctx context.Context, hostname string, owner string, repo string, number int)
| 512 | |
| 513 | // GetPullRequestDatabaseID retrieves the database ID and URL of a pull request given its number in a repository. |
| 514 | func (c *CAPIClient) GetPullRequestDatabaseID(ctx context.Context, hostname string, owner string, repo string, number int) (int64, string, error) { |
| 515 | // TODO: better int handling so we don't need to do bounds checks |
| 516 | // to both ensure a panic is impossible and that we do not trigger |
| 517 | // CodeQL alerts. |
| 518 | if number <= 0 || number > math.MaxInt32 { |
| 519 | return 0, "", fmt.Errorf("pull request number %d out of bounds", number) |
| 520 | } |
| 521 | |
| 522 | var resp struct { |
| 523 | Repository struct { |
| 524 | PullRequest struct { |
| 525 | FullDatabaseID string `graphql:"fullDatabaseId"` |
| 526 | URL string `graphql:"url"` |
| 527 | } `graphql:"pullRequest(number: $number)"` |
| 528 | } `graphql:"repository(owner: $owner, name: $repo)"` |
| 529 | } |
| 530 | |
| 531 | variables := map[string]interface{}{ |
| 532 | "owner": githubv4.String(owner), |
| 533 | "repo": githubv4.String(repo), |
| 534 | "number": githubv4.Int(number), |
| 535 | } |
| 536 | |
| 537 | apiClient := api.NewClientFromHTTP(c.httpClient) |
| 538 | if err := apiClient.Query(hostname, "GetPullRequestFullDatabaseID", &resp, variables); err != nil { |
| 539 | return 0, "", err |
| 540 | } |
| 541 | |
| 542 | databaseID, err := strconv.ParseInt(resp.Repository.PullRequest.FullDatabaseID, 10, 64) |
| 543 | if err != nil { |
| 544 | return 0, "", err |
| 545 | } |
| 546 | return databaseID, resp.Repository.PullRequest.URL, nil |
| 547 | } |
| 548 | |
| 549 | // generatePullRequestNodeID converts an int64 databaseID and repoID to a GraphQL Node ID format |
| 550 | // with the "PR_" prefix for pull requests |