| 109 | } |
| 110 | |
| 111 | func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, error) { |
| 112 | // If we have a URL, we don't need git stuff |
| 113 | if len(opts.Fields) == 0 { |
| 114 | return nil, nil, errors.New("Find error: no fields specified") |
| 115 | } |
| 116 | |
| 117 | if repo, prNumber, _, err := ParseURL(opts.Selector); err == nil { |
| 118 | f.prNumber = prNumber |
| 119 | f.baseRefRepo = repo |
| 120 | } |
| 121 | |
| 122 | if f.baseRefRepo == nil { |
| 123 | repo, err := f.baseRepoFn() |
| 124 | if err != nil { |
| 125 | return nil, nil, err |
| 126 | } |
| 127 | f.baseRefRepo = repo |
| 128 | } |
| 129 | |
| 130 | var prRefs PRFindRefs |
| 131 | if opts.Selector == "" { |
| 132 | // You must be in a git repo for this case to work |
| 133 | currentBranchName, err := f.branchFn() |
| 134 | if err != nil { |
| 135 | return nil, nil, err |
| 136 | } |
| 137 | f.branchName = currentBranchName |
| 138 | |
| 139 | // Get the branch config for the current branchName |
| 140 | branchConfig, err := f.gitConfigClient.ReadBranchConfig(context.Background(), f.branchName) |
| 141 | if err != nil { |
| 142 | return nil, nil, err |
| 143 | } |
| 144 | |
| 145 | // Determine if the branch is configured to merge to a special PR ref |
| 146 | prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) |
| 147 | if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { |
| 148 | prNumber, _ := strconv.Atoi(m[1]) |
| 149 | f.prNumber = prNumber |
| 150 | } |
| 151 | |
| 152 | // Determine the PullRequestRefs from config |
| 153 | if f.prNumber == 0 { |
| 154 | prRefsResolver := NewPullRequestFindRefsResolver( |
| 155 | // We requested the branch config already, so let's cache that |
| 156 | CachedBranchConfigGitConfigClient{ |
| 157 | CachedBranchConfig: branchConfig, |
| 158 | GitConfigClient: f.gitConfigClient, |
| 159 | }, |
| 160 | f.remotesFn, |
| 161 | ) |
| 162 | prRefs, err = prRefsResolver.ResolvePullRequestRefs(f.baseRefRepo, opts.BaseBranch, f.branchName) |
| 163 | if err != nil { |
| 164 | return nil, nil, err |
| 165 | } |
| 166 | } |
| 167 | } else if f.prNumber == 0 { |
| 168 | // You gave me a selector but I couldn't find a PR number (it wasn't a URL) |