This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
226 lines (179 loc) · 4.66 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
215
216
217
218
219
220
221
222
223
224
225
226
package travisci
import (
"encoding/json"
"net/http"
"net/url"
"os"
"github.com/pkg/errors"
)
var (
orgURI = "https://api.travis-ci.org/"
proURI = "https://api.travis-ci.com/"
)
type Client struct {
endpoint string
endpointURL *url.URL
// If githubToken is set, we'll exchange it to get token.
githubToken string
token string
}
// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
type Option func(*Client)
func AccessToken(token string) Option {
if token == "" {
panic(errors.Errorf("empty access token"))
}
return func(c *Client) {
c.token = token
c.githubToken = ""
}
}
func GitHubAccessToken(token string) Option {
if token == "" {
panic(errors.Errorf("empty GitHub access token"))
}
return func(c *Client) {
c.githubToken = token
// Set token to a non-empty value that we'll overwrite later.
// This way we won't try to read a token e.g. from config.
c.token = "github"
}
}
func Endpoint(endpoint string) Option {
if endpoint == "" {
panic(errors.Errorf("empty endpoint"))
}
return func(c *Client) {
c.endpoint = endpoint
}
}
func OrgEndpoint() Option {
return Endpoint(orgURI)
}
func ProEndpoint() Option {
return Endpoint(proURI)
}
// Set token and endpoint the way travis.rb would:
// Endpoint: first env var, then config, then fall back to travis-ci.org
// Token: first env var, then config for chosen endpoint
func (c *Client) configureTokenAndEndpoint() error {
if c.token != "" && c.endpoint != "" {
// Already configured explicitly.
return nil
}
// Try environment variables.
if c.token == "" {
c.token = os.Getenv("TRAVIS_TOKEN")
}
if c.endpoint == "" {
c.endpoint = os.Getenv("TRAVIS_ENDPOINT")
}
// Exit now if we're done and don't need to read config.
if c.token != "" && c.endpoint != "" {
return nil
}
// Read config to look for default endpoint and saved token.
config, err := readTravisConfig()
if err != nil {
return errors.Wrap(err, "reading Travis config file")
}
if c.endpoint == "" {
c.endpoint = config.DefaultEndpoint
}
// If we don't have an endpoint by now, fall back to travis-ci.org.
// We need to choose an endpoint before looking for a saved token.
if c.endpoint == "" {
c.endpoint = orgURI
}
if c.token == "" {
if endpointConfig, ok := config.Endpoints[c.endpoint]; ok {
c.token = endpointConfig.AccessToken
}
}
if c.token == "" {
return errors.New("no access token provided")
}
return nil
}
func NewClient(options ...Option) (*Client, error) {
c := &Client{}
for _, option := range options {
option(c)
}
var err error
if err = c.configureTokenAndEndpoint(); err != nil {
return nil, err
}
c.endpointURL, err = url.Parse(c.endpoint)
if err != nil {
return nil, errors.Wrapf(err, "can't parse endpoint URL %s", c.endpoint)
}
// If we have a GitHub access token, get a Travis token from it.
if c.githubToken != "" {
c.token, err = travisTokenFromGitHubToken(c.githubToken, c.endpoint)
if err != nil {
return nil, err
}
c.githubToken = "" // No need to keep this around.
}
return c, nil
}
func (c *Client) newRequest(method, path string) (*http.Request, error) {
pathURL, err := url.Parse(path)
if err != nil {
// This is a library bug
panic(errors.Errorf("can't parse path %s as URL", path))
}
url := c.endpointURL.ResolveReference(pathURL)
req, err := http.NewRequest(method, url.String(), nil)
if err != nil {
return nil, errors.Wrapf(err, "can't create request to %s", url.String())
}
req.Header.Add("Travis-API-Version", "3")
req.Header.Add("Authorization", "token "+c.token)
return req, nil
}
func (c *Client) getJSON(path string, output interface{}) error {
req, err := c.newRequest("GET", path)
if err != nil {
return errors.Wrapf(err, "creating request")
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrapf(err, "making request")
}
defer res.Body.Close()
if res.StatusCode != 200 {
return errors.New(res.Status)
}
err = json.NewDecoder(res.Body).Decode(output)
if err != nil {
return errors.Wrapf(err, "decoding response as JSON")
}
return nil
}
func (c *Client) CurrentUser() (*User, error) {
var user User
err := c.getJSON("user", &user)
if err != nil {
return nil, err
}
return &user, nil
}
// todo: pagination
func (c *Client) EnvVarsForRepository(repositorySlug string) (*EnvVars, error) {
var envvars EnvVars
err := c.getJSON("repo/"+url.PathEscape(repositorySlug)+"/env_vars", &envvars)
if err != nil {
return nil, err
}
return &envvars, nil
}
func (c *Client) RepositoriesForCurrentUser() (*Repositories, error) {
var repos Repositories
err := c.getJSON("repos?limit=5", &repos)
if err != nil {
return nil, err
}
return &repos, nil
}