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