github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/pull_request/bitbucket_cloud.go (about) 1 package pull_request 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "net/url" 8 9 "github.com/ktrysmt/go-bitbucket" 10 ) 11 12 type BitbucketCloudService struct { 13 client *bitbucket.Client 14 owner string 15 repositorySlug string 16 } 17 18 type BitbucketCloudPullRequest struct { 19 ID int `json:"id"` 20 Source BitbucketCloudPullRequestSource `json:"source"` 21 } 22 23 type BitbucketCloudPullRequestSource struct { 24 Branch BitbucketCloudPullRequestSourceBranch `json:"branch"` 25 Commit BitbucketCloudPullRequestSourceCommit `json:"commit"` 26 } 27 28 type BitbucketCloudPullRequestSourceBranch struct { 29 Name string `json:"name"` 30 } 31 32 type BitbucketCloudPullRequestSourceCommit struct { 33 Hash string `json:"hash"` 34 } 35 36 type PullRequestResponse struct { 37 Page int32 `json:"page"` 38 Size int32 `json:"size"` 39 Pagelen int32 `json:"pagelen"` 40 Next string `json:"next"` 41 Previous string `json:"previous"` 42 Items []PullRequest `json:"values"` 43 } 44 45 var _ PullRequestService = (*BitbucketCloudService)(nil) 46 47 func parseUrl(uri string) (*url.URL, error) { 48 if uri == "" { 49 uri = "https://api.bitbucket.org/2.0" 50 } 51 52 url, err := url.Parse(uri) 53 if err != nil { 54 return nil, err 55 } 56 57 return url, nil 58 } 59 60 func NewBitbucketCloudServiceBasicAuth(baseUrl, username, password, owner, repositorySlug string) (PullRequestService, error) { 61 url, err := parseUrl(baseUrl) 62 if err != nil { 63 return nil, fmt.Errorf("error parsing base url of %s for %s/%s: %v", baseUrl, owner, repositorySlug, err) 64 } 65 66 bitbucketClient := bitbucket.NewBasicAuth(username, password) 67 bitbucketClient.SetApiBaseURL(*url) 68 69 return &BitbucketCloudService{ 70 client: bitbucketClient, 71 owner: owner, 72 repositorySlug: repositorySlug, 73 }, nil 74 } 75 76 func NewBitbucketCloudServiceBearerToken(baseUrl, bearerToken, owner, repositorySlug string) (PullRequestService, error) { 77 url, err := parseUrl(baseUrl) 78 if err != nil { 79 return nil, fmt.Errorf("error parsing base url of %s for %s/%s: %v", baseUrl, owner, repositorySlug, err) 80 } 81 82 bitbucketClient := bitbucket.NewOAuthbearerToken(bearerToken) 83 bitbucketClient.SetApiBaseURL(*url) 84 85 return &BitbucketCloudService{ 86 client: bitbucketClient, 87 owner: owner, 88 repositorySlug: repositorySlug, 89 }, nil 90 } 91 92 func NewBitbucketCloudServiceNoAuth(baseUrl, owner, repositorySlug string) (PullRequestService, error) { 93 // There is currently no method to explicitly not require auth 94 return NewBitbucketCloudServiceBearerToken(baseUrl, "", owner, repositorySlug) 95 } 96 97 func (b *BitbucketCloudService) List(_ context.Context) ([]*PullRequest, error) { 98 opts := &bitbucket.PullRequestsOptions{ 99 Owner: b.owner, 100 RepoSlug: b.repositorySlug, 101 } 102 103 response, err := b.client.Repositories.PullRequests.Gets(opts) 104 if err != nil { 105 return nil, fmt.Errorf("error listing pull requests for %s/%s: %v", b.owner, b.repositorySlug, err) 106 } 107 108 resp, ok := response.(map[string]interface{}) 109 if !ok { 110 return nil, fmt.Errorf("unknown type returned from bitbucket pull requests") 111 } 112 113 repoArray, ok := resp["values"].([]interface{}) 114 if !ok { 115 return nil, fmt.Errorf("unknown type returned from response values") 116 } 117 118 jsonStr, err := json.Marshal(repoArray) 119 if err != nil { 120 return nil, fmt.Errorf("error marshalling response body to json: %v", err) 121 } 122 123 var pulls []BitbucketCloudPullRequest 124 if err := json.Unmarshal(jsonStr, &pulls); err != nil { 125 return nil, fmt.Errorf("error unmarshalling json to type '[]BitbucketCloudPullRequest': %v", err) 126 } 127 128 pullRequests := []*PullRequest{} 129 for _, pull := range pulls { 130 pullRequests = append(pullRequests, &PullRequest{ 131 Number: pull.ID, 132 Branch: pull.Source.Branch.Name, 133 HeadSHA: pull.Source.Commit.Hash, 134 }) 135 } 136 137 return pullRequests, nil 138 }