github.com/andrewrech/lazygit@v0.8.1/pkg/commands/pull_request.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/go-errors/errors"
     8  )
     9  
    10  // Service is a service that repository is on (Github, Bitbucket, ...)
    11  type Service struct {
    12  	Name           string
    13  	PullRequestURL string
    14  }
    15  
    16  // PullRequest opens a link in browser to create new pull request
    17  // with selected branch
    18  type PullRequest struct {
    19  	GitServices []*Service
    20  	GitCommand  *GitCommand
    21  }
    22  
    23  // RepoInformation holds some basic information about the repo
    24  type RepoInformation struct {
    25  	Owner      string
    26  	Repository string
    27  }
    28  
    29  func getServices() []*Service {
    30  	return []*Service{
    31  		{
    32  			Name:           "github.com",
    33  			PullRequestURL: "https://github.com/%s/%s/compare/%s?expand=1",
    34  		},
    35  		{
    36  			Name:           "bitbucket.org",
    37  			PullRequestURL: "https://bitbucket.org/%s/%s/pull-requests/new?source=%s&t=1",
    38  		},
    39  		{
    40  			Name:           "gitlab.com",
    41  			PullRequestURL: "https://gitlab.com/%s/%s/merge_requests/new?merge_request[source_branch]=%s",
    42  		},
    43  	}
    44  }
    45  
    46  // NewPullRequest creates new instance of PullRequest
    47  func NewPullRequest(gitCommand *GitCommand) *PullRequest {
    48  	return &PullRequest{
    49  		GitServices: getServices(),
    50  		GitCommand:  gitCommand,
    51  	}
    52  }
    53  
    54  // Create opens link to new pull request in browser
    55  func (pr *PullRequest) Create(branch *Branch) error {
    56  	branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(branch)
    57  
    58  	if !branchExistsOnRemote {
    59  		return errors.New(pr.GitCommand.Tr.SLocalize("NoBranchOnRemote"))
    60  	}
    61  
    62  	repoURL := pr.GitCommand.GetRemoteURL()
    63  	var gitService *Service
    64  
    65  	for _, service := range pr.GitServices {
    66  		if strings.Contains(repoURL, service.Name) {
    67  			gitService = service
    68  			break
    69  		}
    70  	}
    71  
    72  	if gitService == nil {
    73  		return errors.New(pr.GitCommand.Tr.SLocalize("UnsupportedGitService"))
    74  	}
    75  
    76  	repoInfo := getRepoInfoFromURL(repoURL)
    77  
    78  	return pr.GitCommand.OSCommand.OpenLink(fmt.Sprintf(
    79  		gitService.PullRequestURL, repoInfo.Owner, repoInfo.Repository, branch.Name,
    80  	))
    81  }
    82  
    83  func getRepoInfoFromURL(url string) *RepoInformation {
    84  	isHTTP := strings.HasPrefix(url, "http")
    85  
    86  	if isHTTP {
    87  		splits := strings.Split(url, "/")
    88  		owner := splits[len(splits)-2]
    89  		repo := strings.TrimSuffix(splits[len(splits)-1], ".git")
    90  
    91  		return &RepoInformation{
    92  			Owner:      owner,
    93  			Repository: repo,
    94  		}
    95  	}
    96  
    97  	tmpSplit := strings.Split(url, ":")
    98  	splits := strings.Split(tmpSplit[1], "/")
    99  	owner := splits[0]
   100  	repo := strings.TrimSuffix(splits[1], ".git")
   101  
   102  	return &RepoInformation{
   103  		Owner:      owner,
   104  		Repository: repo,
   105  	}
   106  }