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

Function PullRequestForRun

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

Source from the content-addressed store, hash-verified

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