2018-10-12 14:06:03 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-02-11 12:30:27 +02:00
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
2018-10-12 14:06:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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",
|
2019-03-15 16:14:26 +02:00
|
|
|
PullRequestURL: "https://bitbucket.org/%s/%s/pull-requests/new?source=%s&t=1",
|
2018-10-12 14:06:03 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "gitlab.com",
|
|
|
|
PullRequestURL: "https://gitlab.com/%s/%s/merge_requests/new?merge_request[source_branch]=%s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPullRequest creates new instance of PullRequest
|
2018-10-15 11:00:19 +02:00
|
|
|
func NewPullRequest(gitCommand *GitCommand) *PullRequest {
|
2018-10-12 14:06:03 +02:00
|
|
|
return &PullRequest{
|
|
|
|
GitServices: getServices(),
|
|
|
|
GitCommand: gitCommand,
|
2018-10-15 11:00:19 +02:00
|
|
|
}
|
2018-10-12 14:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create opens link to new pull request in browser
|
|
|
|
func (pr *PullRequest) Create(branch *Branch) error {
|
2018-10-17 14:20:15 +02:00
|
|
|
branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(branch)
|
|
|
|
|
|
|
|
if !branchExistsOnRemote {
|
|
|
|
return errors.New(pr.GitCommand.Tr.SLocalize("NoBranchOnRemote"))
|
|
|
|
}
|
|
|
|
|
2018-10-12 14:06:03 +02:00
|
|
|
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)
|
|
|
|
|
2018-10-17 14:20:15 +02:00
|
|
|
return pr.GitCommand.OSCommand.OpenLink(fmt.Sprintf(
|
2018-10-12 14:06:03 +02:00
|
|
|
gitService.PullRequestURL, repoInfo.Owner, repoInfo.Repository, branch.Name,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoInfoFromURL(url string) *RepoInformation {
|
|
|
|
isHTTP := strings.HasPrefix(url, "http")
|
|
|
|
|
|
|
|
if isHTTP {
|
|
|
|
splits := strings.Split(url, "/")
|
|
|
|
owner := splits[len(splits)-2]
|
2018-10-17 14:20:15 +02:00
|
|
|
repo := strings.TrimSuffix(splits[len(splits)-1], ".git")
|
2018-10-12 14:06:03 +02:00
|
|
|
|
|
|
|
return &RepoInformation{
|
|
|
|
Owner: owner,
|
|
|
|
Repository: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpSplit := strings.Split(url, ":")
|
|
|
|
splits := strings.Split(tmpSplit[1], "/")
|
|
|
|
owner := splits[0]
|
2018-10-17 14:20:15 +02:00
|
|
|
repo := strings.TrimSuffix(splits[1], ".git")
|
2018-10-12 14:06:03 +02:00
|
|
|
|
|
|
|
return &RepoInformation{
|
|
|
|
Owner: owner,
|
|
|
|
Repository: repo,
|
|
|
|
}
|
|
|
|
}
|