diff options
author | Kristijan Husak <husakkristijan@gmail.com> | 2018-10-12 14:06:03 +0200 |
---|---|---|
committer | Kristijan Husak <husakkristijan@gmail.com> | 2018-10-13 22:54:51 +0200 |
commit | df0e3e52fea02950d78af5233a859f592092d475 (patch) | |
tree | 7210a491186833ca2d0481b2602add1dfa0d5e15 /pkg | |
parent | d5f64602a8e10ab5c8162140d426ce677fe884ee (diff) |
Add option to create pull request form branches panel.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git.go | 6 | ||||
-rw-r--r-- | pkg/commands/pull_request.go | 101 | ||||
-rw-r--r-- | pkg/commands/pull_request_test.go | 151 | ||||
-rw-r--r-- | pkg/gui/branches_panel.go | 11 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 6 | ||||
-rw-r--r-- | pkg/i18n/dutch.go | 6 | ||||
-rw-r--r-- | pkg/i18n/english.go | 6 | ||||
-rw-r--r-- | pkg/i18n/polish.go | 6 |
8 files changed, 293 insertions, 0 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 0e974b567..9764d75d7 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -555,6 +555,12 @@ func (c *GitCommand) Show(sha string) (string, error) { return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git show --color %s", sha)) } +// GetRemoteURL returns current repo remote url +func (c *GitCommand) GetRemoteURL() string { + url, _ := c.OSCommand.RunCommandWithOutput("git config --get remote.origin.url") + return utils.TrimTrailingNewline(url) +} + // Diff returns the diff of a file func (c *GitCommand) Diff(file *File) string { cachedArg := "" diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go new file mode 100644 index 000000000..4eb030042 --- /dev/null +++ b/pkg/commands/pull_request.go @@ -0,0 +1,101 @@ +package commands + +import ( + "errors" + "fmt" + "regexp" + "strings" +) + +// Service is a service that repository is on (Github, Bitbucket, ...) +type Service struct { + Name string + PullRequestURL string +} + +// PullRequest opens a link in browser to create new pull request +// with selected branch +type PullRequest struct { + GitServices []*Service + GitCommand *GitCommand +} + +// RepoInformation holds some basic information about the repo +type RepoInformation struct { + Owner string + Repository string +} + +func getServices() []*Service { + return []*Service{ + { + Name: "github.com", + PullRequestURL: "https://github.com/%s/%s/compare/%s?expand=1", + }, + { + Name: "bitbucket.org", + PullRequestURL: "https://bitbucket.org/%s/%s/pull-requests/new?t=%s", + }, + { + Name: "gitlab.com", + PullRequestURL: "https://gitlab.com/%s/%s/merge_requests/new?merge_request[source_branch]=%s", + }, + } +} + +// NewPullRequest creates new instance of PullRequest +func NewPullRequest(gitCommand *GitCommand) (*PullRequest, error) { + return &PullRequest{ + GitServices: getServices(), + GitCommand: gitCommand, + }, nil +} + +// Create opens link to new pull request in browser +func (pr *PullRequest) Create(branch *Branch) error { + repoURL := pr.GitCommand.GetRemoteURL() + var gitService *Service + + for _, service := range pr.GitServices { + if strings.Contains(repoURL, service.Name) { + gitService = service + break + } + } + + if gitService == nil { + return errors.New(pr.GitCommand.Tr.SLocalize("UnsupportedGitService")) + } + + repoInfo := getRepoInfoFromURL(repoURL) + + return pr.GitCommand.OSCommand.OpenFile(fmt.Sprintf( + gitService.PullRequestURL, repoInfo.Owner, repoInfo.Repository, branch.Name, + )) +} + +func getRepoInfoFromURL(url string) *RepoInformation { + isHTTP := strings.HasPrefix(url, "http") + removeGitExtension := regexp.MustCompile(`\.git$`) + + if isHTTP { + splits := strings.Split(url, "/") + owner := splits[len(splits)-2] + repo := removeGitExtension.ReplaceAllString(splits[len(splits)-1], "") + + return &RepoInformation{ + Owner: owner, + Repository: repo, + } + } + + tmpSplit := strings.Split(url, ":") + splits := strings.Split(tmpSplit[1], "/") + owner := splits[0] + repo := removeGitExtension.ReplaceAllString(splits[1], "") + + return &RepoInformation{ + Owner: owner, + Repository: repo, + } +} diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go new file mode 100644 index 000000000..8173edc71 --- /dev/null +++ b/pkg/commands/pull_request_test.go @@ -0,0 +1,151 @@ +package commands + +import ( + "github.com/stretchr/testify/assert" + "os/exec" + "strings" + "testing" +) + +func TestGetRepoInfoFromURL(t *testing.T) { + type scenario struct { + testName string + repoURL string + test func(*RepoInformation) + } + + scenarios := []scenario{ + { + "Returns repository information for git remote url", + "git@github.com:petersmith/super_calculator", + func(repoInfo *RepoInformation) { + assert.EqualValues(t, repoInfo.Owner, "petersmith") + assert.EqualValues(t, repoInfo.Repository, "super_calculator") + }, + }, + { + "Returns repository information for http remote url", + "https://my_username@bitbucket.org/johndoe/social_network.git", + func(repoInfo *RepoInformation) { + assert.EqualValues(t, repoInfo.Owner, "johndoe") + assert.EqualValues(t, repoInfo.Repository, "social_network") + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + s.test(getRepoInfoFromURL(s.repoURL)) + }) + } +} + +func TestCreatePullRequest(t *testing.T) { + type scenario struct { + testName string + branch *Branch + command func(string, ...string) *exec.Cmd + test func(err error) + } + + scenarios := []scenario{ + { + "Opens a link to new pull request on bitbucket", + &Branch{ + Name: "feature/profile-page", + }, + func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return exec.Command("echo", "git@bitbucket.org:johndoe/social_network.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://bitbucket.org/johndoe/social_network/pull-requests/new?t=feature/profile-page"}) + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + { + "Opens a link to new pull request on bitbucket with http remote url", + &Branch{ + Name: "feature/events", + }, + func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return exec.Command("echo", "https://my_username@bitbucket.org/johndoe/social_network.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://bitbucket.org/johndoe/social_network/pull-requests/new?t=feature/events"}) + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + { + "Opens a link to new pull request on github", + &Branch{ + Name: "feature/sum-operation", + }, + func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return exec.Command("echo", "git@github.com:peter/calculator.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://github.com/peter/calculator/compare/feature/sum-operation?expand=1"}) + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + { + "Opens a link to new pull request on gitlab", + &Branch{ + Name: "feature/ui", + }, + func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return exec.Command("echo", "git@gitlab.com:peter/calculator.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://gitlab.com/peter/calculator/merge_requests/new?merge_request[source_branch]=feature/ui"}) + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + }, + { + "Throws an error if git service is unsupported", + &Branch{ + Name: "feature/divide-operation", + }, + func(cmd string, args ...string) *exec.Cmd { + return exec.Command("echo", "git@something.com:peter/calculator.git") + }, + func(err error) { + assert.Error(t, err) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCommand := newDummyGitCommand() + gitCommand.OSCommand.command = s.command + gitCommand.OSCommand.Config.GetUserConfig().Set("os.openCommand", "open {{filename}}") + dummyPullRequest, _ := NewPullRequest(gitCommand) + s.test(dummyPullRequest.Create(s.branch)) + }) + } +} diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 0f66533b1..b4ba151b5 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -22,6 +22,17 @@ func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error { return gui.refreshSidePanels(g) } +func (gui *Gui) handleCreatePullRequestPress(g *gocui.Gui, v *gocui.View) error { + branch := gui.getSelectedBranch(gui.getBranchesView(g)) + pullRequest, _ := commands.NewPullRequest(gui.GitCommand) + + if err := pullRequest.Create(branch); err != nil { + return gui.createErrorPanel(g, err.Error()) + } + + return nil +} + func (gui *Gui) handleForceCheckout(g *gocui.Gui, v *gocui.View) error { branch := gui.getSelectedBranch(v) message := gui.Tr.SLocalize("SureForceCheckout") diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 78271a3f7..cc1edc24b 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -279,6 +279,12 @@ func (gui *Gui) GetKeybindings() []*Binding { Description: gui.Tr.SLocalize("checkout"), }, { ViewName: "branches", + Key: 'o', + Modifier: gocui.ModNone, + Handler: gui.handleCreatePullRequestPress, + Description: gui.Tr.SLocalize("createPullRequest"), + }, { + ViewName: "branches", Key: 'c', Modifier: gocui.ModNone, Handler: gui.handleCheckoutByName, diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index 6b24a5174..c95b3a99c 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -379,6 +379,12 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "ConfirmQuit", Other: `Weet je zeker dat je dit programma wil sluiten?`, + }, &i18n.Message{ + ID: "UnsupportedGitService", + Other: `Niet-ondersteunde git-service`, + }, &i18n.Message{ + ID: "createPullRequest", + Other: `maak een pull-aanvraag`, }, ) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 9ead5a54e..a549c3d58 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -402,6 +402,12 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "SwitchRepo", Other: `switch to a recent repo`, + }, &i18n.Message{ + ID: "UnsupportedGitService", + Other: `Unsupported git service`, + }, &i18n.Message{ + ID: "createPullRequest", + Other: `create pull request`, }, ) } diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index c37bfe972..e7003ca36 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -377,6 +377,12 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "ConfirmQuit", Other: `Na pewno chcesz wyjść z programu?`, + }, &i18n.Message{ + ID: "UnsupportedGitService", + Other: `Nieobsługiwana usługa git`, + }, &i18n.Message{ + ID: "createPullRequest", + Other: `utwórz żądanie wyciągnięcia`, }, ) } |