diff options
author | Denis Palashevskii <palash.denis@outlook.com> | 2021-04-21 13:56:14 +0400 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-07-27 21:30:08 +1000 |
commit | f2645da16a4bd4335c4c77e43517b5fd8d8ca259 (patch) | |
tree | 98c22bc54c89d72a79fb49f8d83ece8b90ce80f5 | |
parent | f8f596d097942d0a0e0cd0ab76e48ec1c2471d75 (diff) |
Extract git service URL formatting to a separate method
-rw-r--r-- | pkg/commands/pull_request.go | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go index 575027229..ac60af536 100644 --- a/pkg/commands/pull_request.go +++ b/pkg/commands/pull_request.go @@ -12,7 +12,7 @@ import ( // Service is a service that repository is on (Github, Bitbucket, ...) type Service struct { Name string - PullRequestURL string + PullRequestURL func(owner string, repository string, from string, to string) string } // PullRequest opens a link in browser to create new pull request @@ -35,18 +35,27 @@ func NewService(typeName string, repositoryDomain string, siteDomain string) *Se switch typeName { case "github": service = &Service{ - Name: repositoryDomain, - PullRequestURL: fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/compare/%s?expand=1"), + Name: repositoryDomain, + PullRequestURL: func(owner string, repository string, from string, to string) string { + urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/compare/%s?expand=1") + return fmt.Sprintf(urlFormat, owner, repository, from) + }, } case "bitbucket": service = &Service{ Name: repositoryDomain, - PullRequestURL: fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/pull-requests/new?source=%s&t=1"), + PullRequestURL: func(owner string, repository string, from string, to string) string { + urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/pull-requests/new?source=%s&t=1") + return fmt.Sprintf(urlFormat, owner, repository, from) + }, } case "gitlab": service = &Service{ Name: repositoryDomain, - PullRequestURL: fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/merge_requests/new?merge_request[source_branch]=%s"), + PullRequestURL: func(owner string, repository string, from string, to string) string { + urlFormat := fmt.Sprintf("https://%s%s", siteDomain, "/%s/%s/merge_requests/new?merge_request[source_branch]=%s") + return fmt.Sprintf(urlFormat, owner, repository, from) + }, } } @@ -131,9 +140,7 @@ func (pr *PullRequest) getPullRequestURL(branch *models.Branch) (string, error) } repoInfo := getRepoInfoFromURL(repoURL) - pullRequestURL := fmt.Sprintf( - gitService.PullRequestURL, repoInfo.Owner, repoInfo.Repository, branch.Name, - ) + pullRequestURL := gitService.PullRequestURL(repoInfo.Owner, repoInfo.Repository, branch.Name, "") return pullRequestURL, nil } |