summaryrefslogtreecommitdiffstats
path: root/pkg/gui/pull_request_menu_panel.go
blob: 088cae2eb93b7b0306732975ed892254181e9fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gui

import (
	"fmt"
	"github.com/jesseduffield/lazygit/pkg/commands"
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)

func (gui *Gui) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error {
	menuItems := make([]*menuItem, 0, 2)

	if selectedBranch != checkedOutBranch {
		menuItems = append(menuItems, &menuItem{
			displayStrings: []string{
				fmt.Sprintf("%s -> default branch", selectedBranch.Name),
			},
			onPress: func() error {
				return createPullRequest(selectedBranch, nil, gui)
			},
		}, &menuItem{
			displayStrings: []string{
				fmt.Sprintf("%s -> %s", checkedOutBranch.Name, selectedBranch.Name),
			},
			onPress: func() error {
				return createPullRequest(checkedOutBranch, selectedBranch, gui)
			},
		})
	}

	menuItems = append(menuItems, &menuItem{
		displayStrings: []string{
			fmt.Sprintf("%s -> default branch", checkedOutBranch.Name),
		},
		onPress: func() error {
			return createPullRequest(checkedOutBranch, nil, gui)
		},
	})

	return gui.createMenu(fmt.Sprintf(gui.Tr.CreatePullRequest), menuItems, createMenuOptions{showCancel: true})
}

func createPullRequest(checkedOutBranch *models.Branch, selectedBranch *models.Branch, gui *Gui) error {
	pullRequest := commands.NewPullRequest(gui.GitCommand)
	url, err := pullRequest.Create(checkedOutBranch, selectedBranch)
	if err != nil {
		return gui.surfaceError(err)
	}
	gui.OnRunCommand(oscommands.NewCmdLogEntry(fmt.Sprintf("Creating pull request at URL: %s", url), "Create pull request", false))

	return nil
}