1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/pull_request_menu_panel.go

78 lines
2.3 KiB
Go
Raw Normal View History

2021-04-21 13:23:36 +02:00
package gui
import (
"fmt"
2021-07-27 12:02:52 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/hosting_service"
2021-04-21 13:23:36 +02: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 13:23:36 +02:00
2021-07-27 12:02:52 +02: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 12:05:53 +02:00
displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.LcDefaultBranch),
2021-07-27 12:02:52 +02:00
onPress: func() error {
return gui.createPullRequest(branch.Name, "")
},
2021-04-21 13:23:36 +02:00
},
2021-07-27 12:02:52 +02:00
{
2021-07-27 12:05:53 +02:00
displayStrings: fromToDisplayStrings(branch.Name, gui.Tr.LcSelectBranch),
2021-07-27 12:02:52 +02:00
onPress: func() error {
return gui.prompt(promptOpts{
title: branch.Name + " →",
findSuggestionsFunc: gui.getBranchNameSuggestionsFunc(),
2021-07-27 12:02:52 +02:00
handleConfirm: func(targetBranchName string) error {
return gui.createPullRequest(branch.Name, targetBranchName)
}},
)
},
2021-04-21 13:23:36 +02:00
},
2021-07-27 12:02:52 +02: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 13:23:36 +02:00
},
2021-07-27 12:02:52 +02:00
)
menuItems = append(menuItems, menuItemsForBranch(checkedOutBranch)...)
2021-04-21 13:23:36 +02:00
}
2021-07-27 12:02:52 +02:00
menuItems = append(menuItems, menuItemsForBranch(selectedBranch)...)
2021-04-21 13:23:36 +02:00
2021-04-21 15:05:57 +02:00
return gui.createMenu(fmt.Sprintf(gui.Tr.CreatePullRequestOptions), menuItems, createMenuOptions{showCancel: true})
2021-04-21 13:23:36 +02:00
}
2021-07-27 12:02:52 +02:00
func (gui *Gui) createPullRequest(from string, to string) error {
hostingServiceMgr := gui.getHostingServiceMgr()
url, err := hostingServiceMgr.GetPullRequestURL(from, to)
2021-04-21 13:23:36 +02:00
if err != nil {
return gui.surfaceError(err)
}
2022-01-06 02:44:41 +02:00
gui.logAction(gui.Tr.Actions.OpenPullRequest)
2022-01-05 03:01:59 +02:00
2022-01-07 06:01:07 +02:00
if err := gui.OSCommand.OpenLink(url); err != nil {
return gui.surfaceError(err)
}
2021-04-21 13:23:36 +02:00
return nil
}
func (gui *Gui) getHostingServiceMgr() *hosting_service.HostingServiceMgr {
2022-01-08 05:10:01 +02:00
remoteUrl := gui.Git.Config.GetRemoteURL()
2021-12-29 02:50:20 +02:00
configServices := gui.UserConfig.Services
return hosting_service.NewHostingServiceMgr(gui.Log, gui.Tr, remoteUrl, configServices)
}