Repository: jesseduffield/lazygit
File: pkg/commands/pull_request.go (Go)

Code before:
package commands

import (
	"fmt"
	"strings"

	"github.com/go-errors/errors"
	"github.com/jesseduffield/lazygit/pkg/config"
)

// Service is a service that repository is on (Github, Bitbucket, ...)
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
}

// NewService builds a Service based on the host type
func NewService(typeName string, repositoryDomain string, siteDomain string) *Service {
	var service *Service

	switch typeName {
	case "github":
		service = &Service{
			Name: repositoryDomain,
			pullRequestURLIntoDefaultBranch: func(owner string, repository string, from string) string {
				return fmt.Sprintf("https://%s/%s/%s/compare/%s?expand=1", siteDomain, owner, repository, from)
			},
			pullRequestURLIntoTargetBranch: func(owner string, repository string, from string, to string) string {
				return fmt.Sprintf("https://%s/%s/%s/compare/%s...%s?expand=1", siteDomain, owner, repository, to, from)
			},
		}
	case "bitbucket":
		service = &Service{
			Name: repositoryDomain,
			pullRequestURLIntoDefaultBranch: func(owner string, repository string, from string) string {
				return fmt.Sprintf("https://%s/%s/%s/pull-requests/new?source=%s&t=1", siteDomain, owner, repository, from)
			},
			pullRequestURLIntoTargetBranch: func(owner string, repository string, from string, to string) string {
				return fmt.Sprintf("https://%s/%s/%s/pull-requests/new?source=%s&dest=%s&t=1", siteDomain, owner, repository, from, to)
			},
		}
	case "gitlab":
		service = &Service{

Review comment (suggestion):
removed this function for the sake of doing away with a type key, but then later realised because serialization is involved (i.e. user-defined services in the user's yaml config) we kind of have to use a type key one way or another. At any rate we don't need a switch statement so I'm instead using a map directly

Code after:
package commands

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/go-errors/errors"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

// if you want to make a custom regex for a given service feel free to test it out
// at regoio.herokuapp.com
var defaultUrlRegexStrings = []string{
	`^https?://.*/(?P<owner>.*)/(?P<repo>.*?)(\.git)?$`,
	`^git@.*:(?P<owner>.*)/(?P<repo>.*?)(\.git)?$`,
}

// Service is a service that repository is on (Github, Bitbucket, ...)
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
	URLRegexStrings                 []string
}

func NewGithubService(repositoryDomain string, siteDomain string) *Service {
	return &Service{
		Name: repositoryDomain,
		pullRequestURLIntoDefaultBranch: func(owner string, repository string, from string) string {
			return fmt.Sprintf("https://%s/%s/%s/compare/%s?expand=1", siteDomain, owner, repository, from)
		},
		pullRequestURLIntoTargetBranch: func(owner string, repository string, from string, to string) string {
			return fmt.Sprintf("https://%s/%s/%s/compare/%s...%s?expand=1", siteDomain, owner, repository, to, from)
		},
		URLRegexStrings: defaultUrlRegexStrings,
	}
}

func NewBitBucketService(repositoryDomain string, siteDomain string) *Service {
	return &Service{
		Name: repositoryDomain,
		pullRequestURLIntoDefaultBranch: func(owner string, repository string, from string) string {
			return fmt.Sprintf("https://%s/%s/%s/pull-requests/new?source=%s&t=1", siteDomain, owner, repository, from)