1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-03 15:02:52 +02:00
lazygit/pkg/gui/pull_request_menu_panel.go

79 lines
2.4 KiB
Go
Raw Normal View History

2021-04-21 15:23:36 +04:00
package gui
import (
"fmt"
2021-07-27 20:02:52 +10:00
"github.com/jesseduffield/lazygit/pkg/commands/hosting_service"
2021-04-21 15:23:36 +04:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
2022-01-29 19:09:20 +11:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2021-04-21 15:23:36 +04:00
)
func (gui *Gui) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error {
2022-01-29 19:09:20 +11:00
menuItems := make([]*types.MenuItem, 0, 4)
2021-04-21 15:23:36 +04:00
2021-07-27 20:02:52 +10:00
fromToDisplayStrings := func(from string, to string) []string {
return []string{fmt.Sprintf("%s → %s", from, to)}
}
2022-01-29 19:09:20 +11:00
menuItemsForBranch := func(branch *models.Branch) []*types.MenuItem {
return []*types.MenuItem{
2021-07-27 20:02:52 +10:00
{
DisplayStrings: fromToDisplayStrings(branch.Name, gui.c.Tr.LcDefaultBranch),
2022-01-28 20:44:36 +11:00
OnPress: func() error {
2021-07-27 20:02:52 +10:00
return gui.createPullRequest(branch.Name, "")
},
2021-04-21 15:23:36 +04:00
},
2021-07-27 20:02:52 +10:00
{
DisplayStrings: fromToDisplayStrings(branch.Name, gui.c.Tr.LcSelectBranch),
2022-01-28 20:44:36 +11:00
OnPress: func() error {
2022-01-29 19:09:20 +11:00
return gui.c.Prompt(types.PromptOpts{
2022-01-28 20:44:36 +11:00
Title: branch.Name + " →",
2022-01-30 10:40:48 +11:00
FindSuggestionsFunc: gui.helpers.suggestions.GetBranchNameSuggestionsFunc(),
2022-01-28 20:44:36 +11:00
HandleConfirm: func(targetBranchName string) error {
2021-07-27 20:02:52 +10:00
return gui.createPullRequest(branch.Name, targetBranchName)
}},
)
},
2021-04-21 15:23:36 +04:00
},
2021-07-27 20:02:52 +10:00
}
}
if selectedBranch != checkedOutBranch {
menuItems = append(menuItems,
2022-01-29 19:09:20 +11:00
&types.MenuItem{
2022-01-28 20:44:36 +11:00
DisplayStrings: fromToDisplayStrings(checkedOutBranch.Name, selectedBranch.Name),
OnPress: func() error {
2021-07-27 20:02:52 +10:00
return gui.createPullRequest(checkedOutBranch.Name, selectedBranch.Name)
},
2021-04-21 15:23:36 +04:00
},
2021-07-27 20:02:52 +10:00
)
menuItems = append(menuItems, menuItemsForBranch(checkedOutBranch)...)
2021-04-21 15:23:36 +04:00
}
2021-07-27 20:02:52 +10:00
menuItems = append(menuItems, menuItemsForBranch(selectedBranch)...)
2021-04-21 15:23:36 +04:00
2022-01-29 19:09:20 +11:00
return gui.c.Menu(types.CreateMenuOptions{Title: fmt.Sprintf(gui.c.Tr.CreatePullRequestOptions), Items: menuItems})
2021-04-21 15:23:36 +04:00
}
2021-07-27 20:02:52 +10:00
func (gui *Gui) createPullRequest(from string, to string) error {
hostingServiceMgr := gui.getHostingServiceMgr()
url, err := hostingServiceMgr.GetPullRequestURL(from, to)
2021-04-21 15:23:36 +04:00
if err != nil {
return gui.c.Error(err)
2021-04-21 15:23:36 +04:00
}
gui.c.LogAction(gui.c.Tr.Actions.OpenPullRequest)
2022-01-05 12:01:59 +11:00
2022-01-07 15:01:07 +11:00
if err := gui.OSCommand.OpenLink(url); err != nil {
return gui.c.Error(err)
}
2021-04-21 15:23:36 +04:00
return nil
}
func (gui *Gui) getHostingServiceMgr() *hosting_service.HostingServiceMgr {
remoteUrl := gui.git.Config.GetRemoteURL()
configServices := gui.c.UserConfig.Services
return hosting_service.NewHostingServiceMgr(gui.Log, gui.Tr, remoteUrl, configServices)
}