/
bitbucket_cloud.go
177 lines (157 loc) · 4.79 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package scm_provider
import (
"context"
"fmt"
"net/http"
"strings"
bitbucket "github.com/ktrysmt/go-bitbucket"
)
type BitBucketCloudProvider struct {
client *ExtendedClient
allBranches bool
owner string
}
type ExtendedClient struct {
*bitbucket.Client
username string
password string
owner string
}
func (c *ExtendedClient) GetContents(repo *Repository, path string) (bool, error) {
urlStr := c.GetApiBaseURL()
// Getting file contents from V2 defined at https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#api-repositories-workspace-repo-slug-src-commit-path-get
urlStr += fmt.Sprintf("/repositories/%s/%s/src/%s/%s?format=meta", c.owner, repo.Repository, repo.SHA, path)
body := strings.NewReader("")
req, err := http.NewRequest(http.MethodGet, urlStr, body)
if err != nil {
return false, err
}
req.SetBasicAuth(c.username, c.password)
resp, err := c.HttpClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return false, nil
}
if resp.StatusCode == http.StatusOK {
return true, nil
}
return false, fmt.Errorf(resp.Status)
}
var _ SCMProviderService = &BitBucketCloudProvider{}
func NewBitBucketCloudProvider(ctx context.Context, owner string, user string, password string, allBranches bool) (*BitBucketCloudProvider, error) {
client := &ExtendedClient{
bitbucket.NewBasicAuth(user, password),
user,
password,
owner,
}
return &BitBucketCloudProvider{client: client, owner: owner, allBranches: allBranches}, nil
}
func (g *BitBucketCloudProvider) GetBranches(ctx context.Context, repo *Repository) ([]*Repository, error) {
repos := []*Repository{}
branches, err := g.listBranches(repo)
if err != nil {
return nil, fmt.Errorf("error listing branches for %s/%s: %v", repo.Organization, repo.Repository, err)
}
for _, branch := range branches {
hash, ok := branch.Target["hash"].(string)
if !ok {
return nil, fmt.Errorf("error getting SHA for branch for %s/%s/%s: %v", g.owner, repo.Repository, branch.Name, err)
}
repos = append(repos, &Repository{
Organization: repo.Organization,
Repository: repo.Repository,
URL: repo.URL,
Branch: branch.Name,
SHA: hash,
Labels: repo.Labels,
RepositoryId: repo.RepositoryId,
})
}
return repos, nil
}
func (g *BitBucketCloudProvider) ListRepos(ctx context.Context, cloneProtocol string) ([]*Repository, error) {
if cloneProtocol == "" {
cloneProtocol = "ssh"
}
opt := &bitbucket.RepositoriesOptions{
Owner: g.owner,
Role: "member",
}
repos := []*Repository{}
accountReposResp, err := g.client.Repositories.ListForAccount(opt)
if err != nil {
return nil, fmt.Errorf("error listing repositories for %s: %v", g.owner, err)
}
for _, bitBucketRepo := range accountReposResp.Items {
cloneUrl, err := findCloneURL(cloneProtocol, &bitBucketRepo)
if err != nil {
return nil, fmt.Errorf("error fetching clone url for repo %s: %v", bitBucketRepo.Slug, err)
}
repos = append(repos, &Repository{
Organization: g.owner,
Repository: bitBucketRepo.Slug,
Branch: bitBucketRepo.Mainbranch.Name,
URL: *cloneUrl,
Labels: []string{},
RepositoryId: bitBucketRepo.Uuid,
})
}
return repos, nil
}
func (g *BitBucketCloudProvider) RepoHasPath(ctx context.Context, repo *Repository, path string) (bool, error) {
contents, err := g.client.GetContents(repo, path)
if err != nil {
return false, err
}
if contents {
return true, nil
}
return false, nil
}
func (g *BitBucketCloudProvider) listBranches(repo *Repository) ([]bitbucket.RepositoryBranch, error) {
if !g.allBranches {
repoBranch, err := g.client.Repositories.Repository.GetBranch(&bitbucket.RepositoryBranchOptions{
Owner: g.owner,
RepoSlug: repo.Repository,
BranchName: repo.Branch,
})
if err != nil {
return nil, err
}
return []bitbucket.RepositoryBranch{
*repoBranch,
}, nil
}
branches, err := g.client.Repositories.Repository.ListBranches(&bitbucket.RepositoryBranchOptions{
Owner: g.owner,
RepoSlug: repo.Repository,
})
if err != nil {
return nil, err
}
return branches.Branches, nil
}
func findCloneURL(cloneProtocol string, repo *bitbucket.Repository) (*string, error) {
cloneLinks, ok := repo.Links["clone"].([]interface{})
if !ok {
return nil, fmt.Errorf("unknown type returned from repo links")
}
for _, link := range cloneLinks {
linkEntry, ok := link.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unknown type returned from clone link")
}
if linkEntry["name"] == cloneProtocol {
url, ok := linkEntry["href"].(string)
if !ok {
return nil, fmt.Errorf("could not find href for clone link")
}
return &url, nil
}
}
return nil, fmt.Errorf("unknown clone protocol for Bitbucket cloud %v", cloneProtocol)
}