2021-04-21 13:23:36 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-27 12:02:52 +02:00
|
|
|
|
2021-12-28 04:58:09 +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 {
|
2021-06-26 11:49:49 +02:00
|
|
|
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 + " →",
|
2021-10-23 01:46:02 +02:00
|
|
|
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 {
|
2021-12-28 04:58:09 +02:00
|
|
|
hostingServiceMgr := gui.getHostingServiceMgr()
|
|
|
|
url, err := hostingServiceMgr.GetPullRequestURL(from, to)
|
2021-04-21 13:23:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return gui.surfaceError(err)
|
|
|
|
}
|
2021-12-28 04:58:09 +02:00
|
|
|
|
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 {
|
2021-12-28 04:58:09 +02:00
|
|
|
return gui.surfaceError(err)
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:23:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-28 04:58:09 +02:00
|
|
|
|
|
|
|
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
|
2021-12-28 04:58:09 +02:00
|
|
|
return hosting_service.NewHostingServiceMgr(gui.Log, gui.Tr, remoteUrl, configServices)
|
|
|
|
}
|