1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-24 05:36:19 +02:00
lazygit/pkg/gui/pull_request_menu_panel.go

78 lines
2.3 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"
)
func (gui *Gui) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error {
menuItems := make([]*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)}
}
menuItemsForBranch := func(branch *models.Branch) []*menuItem {
return []*menuItem{
{
2021-07-27 20:05:53 +10:00
displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.LcDefaultBranch),
2021-07-27 20:02:52 +10:00
onPress: func() error {
return gui.createPullRequest(branch.Name, "")
},
2021-04-21 15:23:36 +04:00
},
2021-07-27 20:02:52 +10:00
{
2021-07-27 20:05:53 +10:00
displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.LcSelectBranch),
2021-07-27 20:02:52 +10:00
onPress: func() error {
return gui.prompt(promptOpts{
title: branch.Name + " →",
findSuggestionsFunc: gui.getBranchNameSuggestionsFunc(),
2021-07-27 20:02:52 +10:00
handleConfirm: func(targetBranchName string) error {
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,
&menuItem{
displayStrings: fromToDisplayStrings(checkedOutBranch.Name, selectedBranch.Name),
onPress: func() error {
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
2021-04-21 17:05:57 +04:00
return gui.createMenu(fmt.Sprintf(gui.Tr.CreatePullRequestOptions), menuItems, createMenuOptions{showCancel: true})
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.surfaceError(err)
}
2022-01-06 11:44:41 +11:00
gui.logAction(gui.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.surfaceError(err)
}
2021-04-21 15:23:36 +04:00
return nil
}
func (gui *Gui) getHostingServiceMgr() *hosting_service.HostingServiceMgr {
2022-01-08 14:10:01 +11:00
remoteUrl := gui.Git.Config.GetRemoteURL()
2021-12-29 11:50:20 +11:00
configServices := gui.UserConfig.Services
return hosting_service.NewHostingServiceMgr(gui.Log, gui.Tr, remoteUrl, configServices)
}