summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Palashevskii <palash.denis@outlook.com>2021-06-08 11:46:50 +0400
committerJesse Duffield <jessedduffield@gmail.com>2021-07-27 21:30:08 +1000
commitd5ec0fdcd17b8e3695defcdebd57e27ca33c3c48 (patch)
treed9930cab0e81613cd35bd2cf4a55f81ffc5b0a66
parent0a63f701e5bc5d3d78b34dcb71f7343e520ae385 (diff)
Remove doubled string formatting in pull request URL generation
-rw-r--r--pkg/commands/pull_request.go18
-rw-r--r--pkg/commands/pull_request_test.go45
2 files changed, 51 insertions, 12 deletions
diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go
index 40cabd0c7..72eec9ff4 100644
--- a/pkg/commands/pull_request.go
+++ b/pkg/commands/pull_request.go
@@ -38,11 +38,9 @@ func NewService(typeName string, repositoryDomain string, siteDomain string) *Se
Name: repositoryDomain,
PullRequestURL: func(owner string, repository string, from string, to string) string {
if to == "" {
- urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/compare/%s?expand=1")
- return fmt.Sprintf(urlFormat, owner, repository, from)
+ return fmt.Sprintf("https://%s/%s/%s/compare/%s?expand=1",siteDomain, owner, repository, from)
} else {
- urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/compare/%s...%s?expand=1")
- return fmt.Sprintf(urlFormat, owner, repository, to, from)
+ return fmt.Sprintf("https://%s/%s/%s/compare/%s...%s?expand=1", siteDomain, owner, repository, to, from)
}
},
}
@@ -51,11 +49,9 @@ func NewService(typeName string, repositoryDomain string, siteDomain string) *Se
Name: repositoryDomain,
PullRequestURL: func(owner string, repository string, from string, to string) string {
if to == "" {
- urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/pull-requests/new?source=%s&t=1")
- return fmt.Sprintf(urlFormat, owner, repository, from)
+ return fmt.Sprintf("https://%s/%s/%s/pull-requests/new?source=%s&t=1", siteDomain, owner, repository, from)
} else {
- urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/pull-requests/new?source=%s&dest=%s&t=1")
- return fmt.Sprintf(urlFormat, owner, repository, from, to)
+ return fmt.Sprintf("https://%s/%s/%s/pull-requests/new?source=%s&dest=%s&t=1", siteDomain, owner, repository, from, to)
}
},
}
@@ -64,11 +60,9 @@ func NewService(typeName string, repositoryDomain string, siteDomain string) *Se
Name: repositoryDomain,
PullRequestURL: func(owner string, repository string, from string, to string) string {
if to == "" {
- urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/merge_requests/new?merge_request[source_branch]=%s")
- return fmt.Sprintf(urlFormat, owner, repository, from)
+ return fmt.Sprintf("https://%s/%s/%s/merge_requests/new?merge_request[source_branch]=%s", siteDomain, owner, repository, from)
} else {
- urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/merge_requests/new?merge_request[source_branch]=%s&merge_request[target_branch]=%s")
- return fmt.Sprintf(urlFormat, owner, repository, from, to)
+ return fmt.Sprintf("https://%s/%s/%s/merge_requests/new?merge_request[source_branch]=%s&merge_request[target_branch]=%s", siteDomain, owner, repository, from, to)
}
},
}
diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go
index 0413022d6..1a9137be5 100644
--- a/pkg/commands/pull_request_test.go
+++ b/pkg/commands/pull_request_test.go
@@ -213,6 +213,27 @@ func TestCreatePullRequest(t *testing.T) {
},
},
{
+ testName: "Opens a link to new pull request on gitlab in nested groups",
+ from: &models.Branch{
+ Name: "feature/ui",
+ },
+ remoteUrl: "git@gitlab.com:peter/public/calculator.git",
+ command: func(cmd string, args ...string) *exec.Cmd {
+ // Handle git remote url call
+ if strings.HasPrefix(cmd, "git") {
+ return secureexec.Command("echo", "git@gitlab.com:peter/calculator.git")
+ }
+
+ assert.Equal(t, cmd, "open")
+ assert.Equal(t, args, []string{"https://gitlab.com/peter/public/calculator/merge_requests/new?merge_request[source_branch]=feature/ui"})
+ return secureexec.Command("echo")
+ },
+ test: func(url string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, "https://gitlab.com/peter/public/calculator/merge_requests/new?merge_request[source_branch]=feature/ui", url)
+ },
+ },
+ {
testName: "Opens a link to new pull request on gitlab with specific target branch",
from: &models.Branch{
Name: "feature/commit-ui",
@@ -237,6 +258,30 @@ 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",
+ },
+ remoteUrl: "git@gitlab.com:peter/public/calculator.git",
+ command: func(cmd string, args ...string) *exec.Cmd {
+ // Handle git remote url call
+ if strings.HasPrefix(cmd, "git") {
+ return secureexec.Command("echo", "git@gitlab.com:peter/calculator.git")
+ }
+
+ assert.Equal(t, cmd, "open")
+ assert.Equal(t, args, []string{"https://gitlab.com/peter/public/calculator/merge_requests/new?merge_request[source_branch]=feature/commit-ui&merge_request[target_branch]=epic/ui"})
+ return secureexec.Command("echo")
+ },
+ test: func(url string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, "https://gitlab.com/peter/public/calculator/merge_requests/new?merge_request[source_branch]=feature/commit-ui&merge_request[target_branch]=epic/ui", url)
+ },
+ },
+ {
testName: "Throws an error if git service is unsupported",
from: &models.Branch{
Name: "feature/divide-operation",