MCPcopy Create free account
hub / github.com/cli/cli / PullRequestForRun

Function PullRequestForRun

pkg/cmd/run/shared/shared.go:616–682  ·  view source on GitHub ↗
(client *api.Client, repo ghrepo.Interface, run Run)

Source from the content-addressed store, hash-verified

614}
615
616func PullRequestForRun(client *api.Client, repo ghrepo.Interface, run Run) (int, error) {
617 type response struct {
618 Repository struct {
619 PullRequests struct {
620 Nodes []struct {
621 Number int
622 HeadRepository struct {
623 Owner struct {
624 Login string
625 }
626 Name string
627 }
628 }
629 }
630 }
631 Number int
632 }
633
634 variables := map[string]any{
635 "owner": repo.RepoOwner(),
636 "repo": repo.RepoName(),
637 "headRefName": run.HeadBranch,
638 }
639
640 query := `
641 query PullRequestForRun($owner: String!, $repo: String!, $headRefName: String!) {
642 repository(owner: $owner, name: $repo) {
643 pullRequests(headRefName: $headRefName, first: 1, orderBy: { field: CREATED_AT, direction: DESC }) {
644 nodes {
645 number
646 headRepository {
647 owner {
648 login
649 }
650 name
651 }
652 }
653 }
654 }
655 }`
656
657 var resp response
658
659 err := client.GraphQL(repo.RepoHost(), query, variables, &resp)
660 if err != nil {
661 return -1, err
662 }
663
664 prs := resp.Repository.PullRequests.Nodes
665 if len(prs) == 0 {
666 return -1, fmt.Errorf("no matching PR found for %s", run.HeadBranch)
667 }
668
669 number := -1
670
671 for _, pr := range prs {
672 if pr.HeadRepository.Owner.Login == run.HeadRepository.Owner.Login && pr.HeadRepository.Name == run.HeadRepository.Name {
673 number = pr.Number

Callers 2

runViewFunction · 0.92
watchRunFunction · 0.92

Calls 5

GraphQLMethod · 0.80
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RepoHostMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected