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