-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
bitbucket.go
105 lines (84 loc) 路 2.39 KB
/
bitbucket.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
package scm
import (
"net/url"
"os"
"github.com/gabrie30/ghorg/colorlog"
"github.com/ktrysmt/go-bitbucket"
)
var (
_ Client = Bitbucket{}
)
func init() {
registerClient(Bitbucket{})
}
type Bitbucket struct {
// extend the bitbucket client
*bitbucket.Client
}
func (_ Bitbucket) GetType() string {
return "bitbucket"
}
// GetOrgRepos gets org repos
func (c Bitbucket) GetOrgRepos(targetOrg string) ([]Repo, error) {
resp, err := c.Repositories.ListForAccount(&bitbucket.RepositoriesOptions{Owner: targetOrg})
if err != nil {
return []Repo{}, err
}
return c.filter(resp.Items)
}
// GetUserRepos gets user repos from bitbucket
func (c Bitbucket) GetUserRepos(targetUser string) ([]Repo, error) {
resp, err := c.Repositories.ListForAccount(&bitbucket.RepositoriesOptions{Owner: targetUser})
if err != nil {
return []Repo{}, err
}
return c.filter(resp.Items)
}
// NewClient create new bitbucket scm client
func (_ Bitbucket) NewClient() (Client, error) {
user := os.Getenv("GHORG_BITBUCKET_USERNAME")
password := os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")
oAuth := os.Getenv("GHORG_BITBUCKET_OAUTH")
var c *bitbucket.Client
if oAuth != "" {
c = bitbucket.NewOAuthbearerToken(oAuth)
} else {
c = bitbucket.NewBasicAuth(user, password)
}
if os.Getenv("GHORG_SCM_BASE_URL") != "" {
u, _ := url.Parse(os.Getenv("GHORG_SCM_BASE_URL"))
c.SetApiBaseURL(*u)
}
return Bitbucket{c}, nil
}
func (_ Bitbucket) filter(resp []bitbucket.Repository) (repoData []Repo, err error) {
cloneData := []Repo{}
for _, a := range resp {
links := a.Links["clone"].([]interface{})
for _, l := range links {
link := l.(map[string]interface{})["href"]
linkType := l.(map[string]interface{})["name"]
if os.Getenv("GHORG_TOPICS") != "" {
colorlog.PrintError("WARNING: Filtering by topics is not supported for Bitbucket SCM")
}
r := Repo{}
r.Name = a.Name
r.Path = r.Name
if os.Getenv("GHORG_BRANCH") == "" {
r.CloneBranch = a.Mainbranch.Name
} else {
r.CloneBranch = os.Getenv("GHORG_BRANCH")
}
if os.Getenv("GHORG_CLONE_PROTOCOL") == "ssh" && linkType == "ssh" {
r.URL = link.(string)
r.CloneURL = link.(string)
cloneData = append(cloneData, r)
} else if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" && linkType == "https" {
r.URL = link.(string)
r.CloneURL = link.(string)
cloneData = append(cloneData, r)
}
}
}
return cloneData, nil
}