MCPcopy Index your code
hub / github.com/cli/cli / PullRequestForRun

Function PullRequestForRun

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

Source from the content-addressed store, hash-verified

587}
588
589func PullRequestForRun(client *api.Client, repo ghrepo.Interface, run Run) (int, error) {
590 type response struct {
591 Repository struct {
592 PullRequests struct {
593 Nodes []struct {
594 Number int
595 HeadRepository struct {
596 Owner struct {
597 Login string
598 }
599 Name string
600 }
601 }
602 }
603 }
604 Number int
605 }
606
607 variables := map[string]interface{}{
608 "owner": repo.RepoOwner(),
609 "repo": repo.RepoName(),
610 "headRefName": run.HeadBranch,
611 }
612
613 query := `
614 query PullRequestForRun($owner: String!, $repo: String!, $headRefName: String!) {
615 repository(owner: $owner, name: $repo) {
616 pullRequests(headRefName: $headRefName, first: 1, orderBy: { field: CREATED_AT, direction: DESC }) {
617 nodes {
618 number
619 headRepository {
620 owner {
621 login
622 }
623 name
624 }
625 }
626 }
627 }
628 }`
629
630 var resp response
631
632 err := client.GraphQL(repo.RepoHost(), query, variables, &resp)
633 if err != nil {
634 return -1, err
635 }
636
637 prs := resp.Repository.PullRequests.Nodes
638 if len(prs) == 0 {
639 return -1, fmt.Errorf("no matching PR found for %s", run.HeadBranch)
640 }
641
642 number := -1
643
644 for _, pr := range prs {
645 if pr.HeadRepository.Owner.Login == run.HeadRepository.Owner.Login && pr.HeadRepository.Name == run.HeadRepository.Name {
646 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