diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index d4060e34e..e93eaa4ba 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -208,7 +208,7 @@ func (self *BranchesController) GetOnRenderToMain() func() { rendererTask := types.NewRunDiffRendererTask(cmdObj.GetCmd()) task = rendererTask - pr, ok := self.c.Model().PullRequestsMap[branch.Name] + pr, ok := self.c.Helpers().Host.PullRequestForBranch(branch.Name) if ok && presentation.ShouldShowPrForBranch(pr, branch.Name, self.c.UserConfig()) { rendererTask.Prefix = presentation.FormatPullRequestHeader(pr, self.c.Tr) rendererTask.Prefix += strings.Repeat("─", self.c.Contexts().Normal.GetView().InnerWidth()) + "\n" @@ -462,7 +462,7 @@ func (self *BranchesController) handleCreatePullRequestMenu(selectedBranch *mode func (self *BranchesController) getPullRequestURL() (string, error) { branch := self.context().GetSelected() - if pr, ok := self.c.Model().PullRequestsMap[branch.Name]; ok { + if pr, ok := self.c.Helpers().Host.PullRequestForBranch(branch.Name); ok { return pr.Url, nil } @@ -855,15 +855,11 @@ func (self *BranchesController) branchIsReal(branch *models.Branch) *types.Disab } func (self *BranchesController) branchHasPR(branch *models.Branch) *types.DisabledReason { - if _, ok := self.c.Model().PullRequestsMap[branch.Name]; !ok { - return &types.DisabledReason{Text: self.c.Tr.NoPullRequestForBranch, ShowErrorInPanel: true} - } - - return nil + return self.c.Helpers().Host.NoPullRequestDisabledReason(branch.Name) } func (self *BranchesController) openPRInBrowser(branch *models.Branch) error { - pr, ok := self.c.Model().PullRequestsMap[branch.Name] + pr, ok := self.c.Helpers().Host.PullRequestForBranch(branch.Name) if !ok { // Should be guarded against by the DisabledReason check, but be defensive in case // PullRequestsMap was updated concurrently by a background refresh diff --git a/pkg/gui/controllers/helpers/host_helper.go b/pkg/gui/controllers/helpers/host_helper.go index 42115e86f..66c2310c6 100644 --- a/pkg/gui/controllers/helpers/host_helper.go +++ b/pkg/gui/controllers/helpers/host_helper.go @@ -2,9 +2,12 @@ package helpers import ( "github.com/jesseduffield/lazygit/pkg/commands/hosting_service" + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/gui/types" ) -// this helper just wraps our hosting_service package +// this helper wraps our hosting_service package, and answers what we know about the +// repo's pull requests on it type HostHelper struct { c *HelperCommon @@ -18,6 +21,24 @@ func NewHostHelper( } } +// PullRequestForBranch returns the pull request the given local branch is the head of, +// and false where it has none. That covers knowing of no pull requests at all: the repo +// may not be on GitHub, or the answer may not have arrived yet. +func (self *HostHelper) PullRequestForBranch(branchName string) (*models.GithubPullRequest, bool) { + pr, ok := self.c.Model().PullRequestsMap[branchName] + return pr, ok +} + +// NoPullRequestDisabledReason disables a command that acts on a branch's pull request +// while that branch has none. +func (self *HostHelper) NoPullRequestDisabledReason(branchName string) *types.DisabledReason { + if _, ok := self.PullRequestForBranch(branchName); !ok { + return &types.DisabledReason{Text: self.c.Tr.NoPullRequestForBranch, ShowErrorInPanel: true} + } + + return nil +} + func (self *HostHelper) GetPullRequestURL(from string, to string) (string, error) { mgr, err := self.getHostingServiceMgr() if err != nil { diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 983ca789f..b5db039d8 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -667,15 +667,11 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ } func (self *LocalCommitsController) checkedOutBranchHasPR() *types.DisabledReason { - branch := self.c.Model().CheckedOutBranch - if _, ok := self.c.Model().PullRequestsMap[branch]; !ok { - return &types.DisabledReason{Text: self.c.Tr.NoPullRequestForBranch, ShowErrorInPanel: true} - } - return nil + return self.c.Helpers().Host.NoPullRequestDisabledReason(self.c.Model().CheckedOutBranch) } func (self *LocalCommitsController) openPRInBrowser() error { - pr, ok := self.c.Model().PullRequestsMap[self.c.Model().CheckedOutBranch] + pr, ok := self.c.Helpers().Host.PullRequestForBranch(self.c.Model().CheckedOutBranch) if !ok { // Should be guarded against by the DisabledReason check, but be defensive in case // PullRequestsMap was updated concurrently by a background refresh