github.com/samcontesse/bitbucket-cascade-merge@v0.0.0-20230227091349-c5ec053235b5/bitbucket.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/ktrysmt/go-bitbucket" 6 ) 7 8 type Bitbucket struct { 9 Client *bitbucket.Client 10 Owner string 11 RepoSlug string 12 } 13 14 func NewBitbucket(username, password, owner, repoSlug string) *Bitbucket { 15 return &Bitbucket{ 16 Client: bitbucket.NewBasicAuth(username, password), 17 Owner: owner, 18 RepoSlug: repoSlug, 19 } 20 } 21 22 func (c *Bitbucket) GetCloneURL(protocols ...string) (string, error) { 23 opt := &bitbucket.RepositoryOptions{ 24 Owner: c.Owner, 25 RepoSlug: c.RepoSlug, 26 } 27 28 r, err := c.Client.Repositories.Repository.Get(opt) 29 if err != nil { 30 return "", err 31 } 32 33 cloneLinks := r.Links["clone"] 34 if cloneLinks != nil { 35 for _, v := range cloneLinks.([]interface{}) { 36 vv := v.(map[string]interface{}) 37 href := vv["href"].(string) 38 name := vv["name"].(string) 39 40 // no given protocol, return the first available 41 if len(protocols) == 0 { 42 return href, nil 43 } 44 45 // try protocols in the given order 46 for _, p := range protocols { 47 if p == name { 48 return href, nil 49 } 50 } 51 52 } 53 } 54 55 return "", fmt.Errorf("cannot determine clone url of %s", r.Full_name) 56 } 57 58 func (c *Bitbucket) GetCascadeOptions(owner, repo string) (*CascadeOptions, error) { 59 opt := &bitbucket.RepositoryBranchingModelOptions{ 60 Owner: c.Owner, 61 RepoSlug: c.RepoSlug, 62 } 63 64 model, err := c.Client.Repositories.Repository.BranchingModel(opt) 65 if err != nil { 66 return nil, err 67 } 68 69 for _, bt := range model.Branch_Types { 70 if bt.Kind == "release" { 71 return &CascadeOptions{ 72 DevelopmentName: model.Development.Name, 73 ReleasePrefix: bt.Prefix, 74 }, nil 75 } 76 } 77 78 return nil, fmt.Errorf("cannot inspect branching model on %s", repo) 79 } 80 81 func (c *Bitbucket) CreatePullRequest(title, description, sourceBranch, destinationBranch string) error { 82 opt := &bitbucket.PullRequestsOptions{ 83 Owner: c.Owner, 84 RepoSlug: c.RepoSlug, 85 Title: title, 86 Description: description, 87 SourceBranch: sourceBranch, 88 DestinationBranch: destinationBranch, 89 } 90 91 _, err := c.Client.Repositories.PullRequests.Create(opt) 92 if err != nil { 93 return err 94 } 95 return nil 96 }