forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
214 lines (187 loc) · 5.14 KB
/
client.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package bitbucket
import (
"encoding/json"
"fmt"
"log"
"os"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/bitbucket"
)
type Client struct {
Auth *auth
Users users
User user
Teams teams
Repositories *Repositories
Pagelen uint64
}
type auth struct {
app_id, secret string
user, password string
token oauth2.Token
}
func NewOAuth(i, s string) *Client {
a := &auth{app_id: i, secret: s}
ctx := context.Background()
conf := &oauth2.Config{
ClientID: i,
ClientSecret: s,
Endpoint: bitbucket.Endpoint,
}
// Redirect user to consent page to ask for permission
// for the scopes specified above.
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
fmt.Printf("Visit the URL for the auth dialog:\n%v", url)
// Use the authorization code that is pushed to the redirect
// URL. Exchange will do the handshake to retrieve the
// initial access token. The HTTP Client returned by
// conf.Client will refresh the token as necessary.
var code string
fmt.Printf("Enter the code in the return URL: ")
if _, err := fmt.Scan(&code); err != nil {
log.Fatal(err)
}
tok, err := conf.Exchange(ctx, code)
if err != nil {
log.Fatal(err)
}
a.token = *tok
return injectClient(a)
}
func NewBasicAuth(u, p string) *Client {
a := &auth{user: u, password: p}
return injectClient(a)
}
// NewEnvVarAuth creates a client using the environment
// variables BITBUCKET_USERNAME and BITBUCKET_PASSWORD
func NewEnvVarAuth() *Client {
u, p := os.Getenv("BITBUCKET_USERNAME"), os.Getenv("BITBUCKET_PASSWORD")
if u == "" || p == "" {
log.Fatal("BITBUCKET_USERNAME or BITBUCKET_PASSWORD not set")
}
a := &auth{user: u, password: p}
return injectClient(a)
}
const DEFAULT_PAGE_LENGHT = 10
func injectClient(a *auth) *Client {
c := &Client{Auth: a, Pagelen: DEFAULT_PAGE_LENGHT}
c.Repositories = &Repositories{
c: c,
PullRequests: &PullRequests{c: c},
Repository: &Repository{c: c},
Commits: &Commits{c: c},
Diff: &Diff{c: c},
BranchRestrictions: &BranchRestrictions{c: c},
Webhooks: &Webhooks{c: c},
}
c.Users = &Users{c: c}
c.User = &User{c: c}
c.Teams = &Teams{c: c}
return c
}
func (c *Client) executeRaw(method string, urlStr string, text string) ([]byte, error) {
body := strings.NewReader(text)
req, err := http.NewRequest(method, urlStr, body)
if text != "" {
req.Header.Set("Content-Type", "application/json")
}
if err != nil {
return nil, err
}
if c.Auth.user != "" && c.Auth.password != "" {
req.SetBasicAuth(c.Auth.user, c.Auth.password)
} else if c.Auth.token.Valid() {
c.Auth.token.SetAuthHeader(req)
}
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode == http.StatusNoContent {
return nil, nil
}
if (resp.StatusCode != http.StatusOK) && (resp.StatusCode != http.StatusCreated) {
return nil, fmt.Errorf(resp.Status)
}
if resp.Body == nil {
return nil, fmt.Errorf("response body is nil")
}
return ioutil.ReadAll(resp.Body)
}
func (c *Client) execute(method string, urlStr string, text string) (interface{}, error) {
// Use pagination if changed from default value
const DEC_RADIX = 10
if strings.Contains(urlStr, "/repositories/") {
if c.Pagelen != DEFAULT_PAGE_LENGHT {
urlObj, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
q := urlObj.Query()
q.Set("pagelen", strconv.FormatUint(c.Pagelen, DEC_RADIX))
urlObj.RawQuery = q.Encode()
urlStr = urlObj.String()
}
}
b, err := c.executeRaw(method, urlStr, text)
if err != nil {
return nil, err
}
var result interface{}
err = json.Unmarshal(b, &result)
if err != nil {
return nil, err
}
resultMap, isMap := result.(map[string]interface{})
if isMap {
nextIn := resultMap["next"]
valuesIn := resultMap["values"]
if nextIn != nil && valuesIn != nil {
nextUrl := nextIn.(string)
if nextUrl != "" {
valuesSlice := valuesIn.([]interface{})
if valuesSlice != nil {
nextResult, err := c.execute(method, nextUrl, text)
if err != nil {
return nil, err
}
nextResultMap, isNextMap := nextResult.(map[string]interface{})
if !isNextMap {
return nil, fmt.Errorf("next page result is not map, it's %T", nextResult)
}
nextValuesIn := nextResultMap["values"]
if nextValuesIn == nil {
return nil, fmt.Errorf("next page result has no values")
}
nextValuesSlice, isSlice := nextValuesIn.([]interface{})
if !isSlice {
return nil, fmt.Errorf("next page result 'values' is not slice")
}
valuesSlice = append(valuesSlice, nextValuesSlice...)
resultMap["values"] = valuesSlice
delete(resultMap, "page")
delete(resultMap, "pagelen")
delete(resultMap, "size")
result = resultMap
}
}
}
}
return result, nil
}
func (c *Client) requestUrl(template string, args ...interface{}) string {
if len(args) == 1 && args[0] == "" {
return GetApiBaseURL() + template
}
return GetApiBaseURL() + fmt.Sprintf(template, args...)
}