| 80 | } |
| 81 | |
| 82 | func statusRun(opts *StatusOptions) error { |
| 83 | ctx := context.Background() |
| 84 | httpClient, err := opts.HttpClient() |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | |
| 89 | baseRefRepo, err := opts.BaseRepo() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | var currentBranchName string |
| 95 | var currentPRNumber int |
| 96 | var currentHeadRefBranchName string |
| 97 | |
| 98 | if !opts.HasRepoOverride { |
| 99 | // We must be in a repo without the override |
| 100 | currentBranchName, err = opts.Branch() |
| 101 | if err != nil && !errors.Is(err, git.ErrNotOnAnyBranch) { |
| 102 | return fmt.Errorf("could not query for pull request for current branch: %w", err) |
| 103 | } |
| 104 | |
| 105 | if !errors.Is(err, git.ErrNotOnAnyBranch) { |
| 106 | branchConfig, err := opts.GitClient.ReadBranchConfig(ctx, currentBranchName) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | // Determine if the branch is configured to merge to a special PR ref |
| 111 | prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) |
| 112 | if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { |
| 113 | currentPRNumber, _ = strconv.Atoi(m[1]) |
| 114 | } |
| 115 | |
| 116 | if currentPRNumber == 0 { |
| 117 | prRefsResolver := shared.NewPullRequestFindRefsResolver( |
| 118 | // We requested the branch config already, so let's cache that |
| 119 | shared.CachedBranchConfigGitConfigClient{ |
| 120 | CachedBranchConfig: branchConfig, |
| 121 | GitConfigClient: opts.GitClient, |
| 122 | }, |
| 123 | opts.Remotes, |
| 124 | ) |
| 125 | |
| 126 | prRefs, err := prRefsResolver.ResolvePullRequestRefs(baseRefRepo, "", currentBranchName) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | |
| 131 | currentHeadRefBranchName = prRefs.QualifiedHeadRef() |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | options := requestOptions{ |
| 137 | Username: "@me", |
| 138 | CurrentPR: currentPRNumber, |
| 139 | HeadRef: currentHeadRefBranchName, |