diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-07-27 20:02:52 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-07-27 21:30:08 +1000 |
commit | 58ddbae4d12d397a1409eaf961cfdc80f42f9168 (patch) | |
tree | bdf90e87a0fb46cb212a39966c7c20add8e431c7 /pkg/commands | |
parent | 3802b563b03171607965b8d4771ea8f9d206553b (diff) |
Minor refactor
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/pull_request.go | 42 | ||||
-rw-r--r-- | pkg/commands/pull_request_test.go | 91 | ||||
-rw-r--r-- | pkg/commands/remotes.go | 6 |
3 files changed, 50 insertions, 89 deletions
diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go index a8c96e328..46dcbfd68 100644 --- a/pkg/commands/pull_request.go +++ b/pkg/commands/pull_request.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/go-errors/errors" - "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/config" ) @@ -13,20 +12,7 @@ import ( type Service struct { Name string pullRequestURLIntoDefaultBranch func(owner string, repository string, from string) string - pullRequestURLIntoTargetBranch func(owner string, repository string, from string, to string) 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 + pullRequestURLIntoTargetBranch func(owner string, repository string, from string, to string) string } // NewService builds a Service based on the host type @@ -77,6 +63,19 @@ func (s *Service) PullRequestURL(owner string, repository string, from string, t } } +// 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(config config.AppConfigurer) []*Service { services := []*Service{ NewService("github", "github.com", "github.com"), @@ -114,7 +113,7 @@ func NewPullRequest(gitCommand *GitCommand) *PullRequest { } // Create opens link to new pull request in browser -func (pr *PullRequest) Create(from *models.Branch, to *models.Branch) (string, error) { +func (pr *PullRequest) Create(from string, to string) (string, error) { pullRequestURL, err := pr.getPullRequestURL(from, to) if err != nil { return "", err @@ -124,7 +123,7 @@ func (pr *PullRequest) Create(from *models.Branch, to *models.Branch) (string, e } // CopyURL copies the pull request URL to the clipboard -func (pr *PullRequest) CopyURL(from *models.Branch, to *models.Branch) (string, error) { +func (pr *PullRequest) CopyURL(from string, to string) (string, error) { pullRequestURL, err := pr.getPullRequestURL(from, to) if err != nil { return "", err @@ -133,7 +132,7 @@ func (pr *PullRequest) CopyURL(from *models.Branch, to *models.Branch) (string, return pullRequestURL, pr.GitCommand.OSCommand.CopyToClipboard(pullRequestURL) } -func (pr *PullRequest) getPullRequestURL(from *models.Branch, to *models.Branch) (string, error) { +func (pr *PullRequest) getPullRequestURL(from string, to string) (string, error) { branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(from) if !branchExistsOnRemote { @@ -155,11 +154,8 @@ func (pr *PullRequest) getPullRequestURL(from *models.Branch, to *models.Branch) } repoInfo := getRepoInfoFromURL(repoURL) - toBranchName := "" - if to != nil { - toBranchName = to.Name - } - pullRequestURL := gitService.PullRequestURL(repoInfo.Owner, repoInfo.Repository, from.Name, toBranchName) + + pullRequestURL := gitService.PullRequestURL(repoInfo.Owner, repoInfo.Repository, from, to) return pullRequestURL, nil } diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go index 1a9137be5..2db5b8ade 100644 --- a/pkg/commands/pull_request_test.go +++ b/pkg/commands/pull_request_test.go @@ -5,7 +5,6 @@ import ( "strings" "testing" - "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/stretchr/testify/assert" ) @@ -48,8 +47,8 @@ func TestGetRepoInfoFromURL(t *testing.T) { func TestCreatePullRequest(t *testing.T) { type scenario struct { testName string - from *models.Branch - to *models.Branch + from string + to string remoteUrl string command func(string, ...string) *exec.Cmd test func(url string, err error) @@ -57,10 +56,8 @@ func TestCreatePullRequest(t *testing.T) { scenarios := []scenario{ { - testName: "Opens a link to new pull request on bitbucket", - from: &models.Branch{ - Name: "feature/profile-page", - }, + testName: "Opens a link to new pull request on bitbucket", + from: "feature/profile-page", remoteUrl: "git@bitbucket.org:johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -78,10 +75,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on bitbucket with http remote url", - from: &models.Branch{ - Name: "feature/events", - }, + testName: "Opens a link to new pull request on bitbucket with http remote url", + from: "feature/events", remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -99,10 +94,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on github", - from: &models.Branch{ - Name: "feature/sum-operation", - }, + testName: "Opens a link to new pull request on github", + from: "feature/sum-operation", remoteUrl: "git@github.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -120,13 +113,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on bitbucket with specific target branch", - from: &models.Branch{ - Name: "feature/profile-page/avatar", - }, - to: &models.Branch{ - Name: "feature/profile-page", - }, + testName: "Opens a link to new pull request on bitbucket with specific target branch", + from: "feature/profile-page/avatar", + to: "feature/profile-page", remoteUrl: "git@bitbucket.org:johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -144,13 +133,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on bitbucket with http remote url with specified target branch", - from: &models.Branch{ - Name: "feature/remote-events", - }, - to: &models.Branch{ - Name: "feature/events", - }, + testName: "Opens a link to new pull request on bitbucket with http remote url with specified target branch", + from: "feature/remote-events", + to: "feature/events", remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -168,13 +153,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on github with specific target branch", - from: &models.Branch{ - Name: "feature/sum-operation", - }, - to: &models.Branch{ - Name: "feature/operations", - }, + testName: "Opens a link to new pull request on github with specific target branch", + from: "feature/sum-operation", + to: "feature/operations", remoteUrl: "git@github.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -192,10 +173,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab", - from: &models.Branch{ - Name: "feature/ui", - }, + testName: "Opens a link to new pull request on gitlab", + from: "feature/ui", remoteUrl: "git@gitlab.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -213,10 +192,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab in nested groups", - from: &models.Branch{ - Name: "feature/ui", - }, + testName: "Opens a link to new pull request on gitlab in nested groups", + from: "feature/ui", remoteUrl: "git@gitlab.com:peter/public/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -234,13 +211,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab with specific target branch", - from: &models.Branch{ - Name: "feature/commit-ui", - }, - to: &models.Branch{ - Name: "epic/ui", - }, + testName: "Opens a link to new pull request on gitlab with specific target branch", + from: "feature/commit-ui", + to: "epic/ui", remoteUrl: "git@gitlab.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -258,13 +231,9 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Opens a link to new pull request on gitlab with specific target branch in nested groups", - from: &models.Branch{ - Name: "feature/commit-ui", - }, - to: &models.Branch{ - Name: "epic/ui", - }, + testName: "Opens a link to new pull request on gitlab with specific target branch in nested groups", + from: "feature/commit-ui", + to: "epic/ui", remoteUrl: "git@gitlab.com:peter/public/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { // Handle git remote url call @@ -282,10 +251,8 @@ func TestCreatePullRequest(t *testing.T) { }, }, { - testName: "Throws an error if git service is unsupported", - from: &models.Branch{ - Name: "feature/divide-operation", - }, + testName: "Throws an error if git service is unsupported", + from: "feature/divide-operation", remoteUrl: "git@something.com:peter/calculator.git", command: func(cmd string, args ...string) *exec.Cmd { return secureexec.Command("echo") diff --git a/pkg/commands/remotes.go b/pkg/commands/remotes.go index 535149c1a..75dee0b46 100644 --- a/pkg/commands/remotes.go +++ b/pkg/commands/remotes.go @@ -2,8 +2,6 @@ package commands import ( "fmt" - - "github.com/jesseduffield/lazygit/pkg/commands/models" ) func (c *GitCommand) AddRemote(name string, url string) error { @@ -28,10 +26,10 @@ func (c *GitCommand) DeleteRemoteBranch(remoteName string, branchName string, pr } // CheckRemoteBranchExists Returns remote branch -func (c *GitCommand) CheckRemoteBranchExists(branch *models.Branch) bool { +func (c *GitCommand) CheckRemoteBranchExists(branchName string) bool { _, err := c.OSCommand.RunCommandWithOutput( "git show-ref --verify -- refs/remotes/origin/%s", - branch.Name, + branchName, ) return err == nil |