forked from yext/teamcity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
452 lines (404 loc) · 13.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package teamcity
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"strconv"
"github.com/yext/teamcity/locate"
)
var Logger = log.New(ioutil.Discard, "", 0)
const (
basePathSuffix = "/httpAuth/app/rest/"
projectsPath = "projects"
buildsPath = "builds"
buildTypesPath = "buildTypes"
buildQueuePath = "buildQueue"
changesPath = "changes"
parametersPath = "parameters"
templatePath = "template"
statsPath = "statistics"
artifactDependencyPath = "artifact-dependencies"
snapshotDependencyPath = "snapshot-dependencies"
triggerPath = "triggers"
vcsRootsPath = "vcs-roots"
tagsPath = "tags"
locatorParamKey = "?locator="
fieldsParamKey = "&fields="
artifactDependencyType = "artifact_dependency"
snapshotDependencyType = "snapshot_dependency"
jsonContentType = "application/json"
textContentType = "text/plain"
)
// Client is an http client and authorization details used to make http requests to TeamCity's API
type Client struct {
httpClient *http.Client
host string
username string
password string
}
// NewClient creates a new Client with specified authorization details
func NewClient(host, username, password string) *Client {
return &Client{
httpClient: http.DefaultClient,
host: host,
username: username,
password: password,
}
}
// ListProjects gets a list of all projects
func (c *Client) ListProjects() (*Projects, error) {
v := &Projects{}
if err := c.doRequest("GET", projectsPath, "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectProject gets the project with specified selector
// See https://confluence.jetbrains.com/display/TCD9/REST+API#RESTAPI-ProjectsandBuildConfiguration/TemplatesLists
// for more information about constructing selector.
func (c *Client) SelectProject(selector string) (*Project, error) {
v := &Project{}
if err := c.doRequest("GET", path.Join(projectsPath, selector), "", nil, v); err != nil {
return nil, err
}
return v, nil
}
type selectOption interface {
// updatePath gets old path on the input and returns amended path.
updatePath(path string) string
}
// WithFields is an option for SelectBuilds to query certain fields of the builds.
type WithFields string
func (f WithFields) updatePath(path string) string {
return path + fieldsParamKey + url.QueryEscape(string(f))
}
// SelectBuilds gets the build with the specified buildLocator.
// See https://confluence.jetbrains.com/display/TCD9/REST+API#RESTAPI-BuildLocator
// for more information about constructing buildLocator string.
func (c *Client) SelectBuilds(selector string, options ...selectOption) (*Builds, error) {
v := &Builds{}
path := buildsPath + locatorParamKey + url.QueryEscape(selector)
for _, opt := range options {
path = opt.updatePath(path)
}
if err := c.doRequest("GET", path, "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// BuildFromId gets the build details for the build with specified id
func (c *Client) BuildFromID(id int) (*Build, error) {
v := &Build{}
if err := c.doRequest("GET", path.Join(buildsPath, locate.ById(strconv.Itoa(id)).String()), "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectChange gets the Change with the specified selector
func (c *Client) SelectChange(selector string) (*Change, error) {
v := &Change{}
if err := c.doRequest("GET", path.Join(changesPath, selector), "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectBuildType gets the build configuration with the specified selector
func (c *Client) SelectBuildType(selector string) (*BuildType, error) {
v := &BuildType{}
if err := c.doRequest("GET", path.Join(buildTypesPath, selector), "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectBuildTypes gets the build configurations with the specified selector
func (c *Client) SelectBuildTypes(selector string) (*BuildTypes, error) {
v := &BuildTypes{}
path := buildTypesPath + locatorParamKey + selector
if err := c.doRequest("GET", path, "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectBuildTypeBuilds gets the builds belonging to the build configuration with the specified selector
func (c *Client) SelectBuildTypeBuilds(selector string) (*Builds, error) {
v := &Builds{}
if err := c.doRequest("GET", path.Join(buildTypesPath, selector, buildsPath), "", nil, v); err != nil {
return nil, err
}
return v, nil
}
func (c *Client) SelectBuildStats(selector string) (*PropertyList, error) {
v := &PropertyList{}
if err := c.doRequest("GET", path.Join(buildsPath, selector, statsPath), "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectVcsRoot gets the VcsRoot belonging to properties specified by the specified selector
func (c *Client) SelectVcsRoot(selector string) (*VcsRoot, error) {
v := &VcsRoot{}
if err := c.doRequest("GET", path.Join(vcsRootsPath, selector), "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// TriggerBuildID runs a build for the given build ID and change ID in TeamCity
func (c *Client) TriggerBuildID(buildTypeId string, changeId int, pushDescription string) (*Build, error) {
v := &Build{}
build := &Build{
BuildType: BuildType{
Id: buildTypeId,
},
Properties: Params{
Properties: []Property{
Property{
Name: "env.PUSH_DESCRIPTION",
Value: pushDescription,
},
Property{
Name: "reverse.dep.*.env.PUSH_DESCRIPTION",
Value: pushDescription,
},
},
},
}
if changeId > 0 {
build.LastChanges = Changes{
Changes: []Change{
Change{Id: changeId},
},
}
}
if len(pushDescription) > 0 {
build.Comment = Comment{
Text: pushDescription,
}
}
if err := c.doJSONRequest("POST", buildQueuePath, build, v); err != nil {
return nil, err
}
return v, nil
}
// TriggerBuild runs a build using the given provided *Build.
func (c *Client) TriggerBuild(build *Build, pushDescription string) (*Build, error) {
if len(pushDescription) > 0 {
build.Comment = Comment{Text: pushDescription}
}
if err := c.doJSONRequest("POST", buildQueuePath, build, build); err != nil {
return nil, err
}
return build, nil
}
// UpdateParameter updates the parameter provided for the specified project name
func (c *Client) UpdateParameter(projectLocator string, property *Property) (*Property, error) {
p := path.Join(projectsPath, projectLocator, parametersPath, property.Name)
v := &Property{}
if err := c.doJSONRequest("PUT", p, property, v); err != nil {
return nil, err
}
return v, nil
}
// UpdateBuildTypeParameter updates the parameter provided for the specified build type
func (c *Client) UpdateBuildTypeParameter(buildTypeLocator string, property *Property) (*Property, error) {
p := path.Join(buildTypesPath, buildTypeLocator, parametersPath, property.Name)
v := &Property{}
if err := c.doJSONRequest("PUT", p, property, v); err != nil {
return nil, err
}
return v, nil
}
// CreateProject creates a new project
func (c *Client) CreateProject(project *Project) (*Project, error) {
v := &Project{}
if err := c.doJSONRequest("POST", projectsPath, project, v); err != nil {
return nil, err
}
return v, nil
}
// CreateBuildType creates a new build type under designated project
func (c *Client) CreateBuildType(projectLocator string, buildType *BuildType) (*BuildType, error) {
v := &BuildType{}
p := path.Join(projectsPath, projectLocator, buildTypesPath)
if err := c.doJSONRequest("POST", p, buildType, v); err != nil {
return nil, err
}
return v, nil
}
// SelectSnapshotDependency selects a snapshot dependency with given id
func (c *Client) SelectSnapshotDependency(buildTypeSelector string, dependencyId string) (*Dependency, error) {
v := &Dependency{}
p := path.Join(buildTypesPath, buildTypeSelector, snapshotDependencyPath, dependencyId)
if err := c.doRequest("GET", p, "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectArtifactDependencies selects all artifact dependencies for the given build type
func (c *Client) SelectArtifactDependencies(buildTypeSelector string) (*ArtifactDependencies, error) {
v := &ArtifactDependencies{}
p := path.Join(buildTypesPath, buildTypeSelector, artifactDependencyPath)
if err := c.doRequest("GET", p, "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// SelectSnapshotDependencies selects all snapshot dependencies for the given build type
func (c *Client) SelectSnapshotDependencies(buildTypeSelector string) (*SnapshotDependencies, error) {
v := &SnapshotDependencies{}
p := path.Join(buildTypesPath, buildTypeSelector, snapshotDependencyPath)
if err := c.doRequest("GET", p, "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// DeleteSnapshotDependency deletes a snapshot dependency
func (c *Client) DeleteSnapshotDependency(buildTypeSelector string, dependency *Dependency) error {
dependency.Type = snapshotDependencyType
p := path.Join(buildTypesPath, buildTypeSelector, snapshotDependencyPath, dependency.Id)
if err := c.doJSONRequest("DELETE", p, nil, nil); err != nil {
return err
}
return nil
}
// CreateSnapshotDependency creates a snapshot dependency
func (c *Client) CreateSnapshotDependency(buildTypeSelector string, dependency *Dependency) (*Dependency, error) {
v := &Dependency{}
dependency.Type = snapshotDependencyType
p := path.Join(buildTypesPath, buildTypeSelector, snapshotDependencyPath)
if err := c.doJSONRequest("POST", p, dependency, v); err != nil {
return nil, err
}
return v, nil
}
// CreateArtifactDependency creates a artifact dependency
func (c *Client) CreateArtifactDependency(buildTypeSelector string, dependency *Dependency) (*Dependency, error) {
v := &Dependency{}
dependency.Type = artifactDependencyType
p := path.Join(buildTypesPath, buildTypeSelector, artifactDependencyPath)
if err := c.doJSONRequest("POST", p, dependency, v); err != nil {
return nil, err
}
return v, nil
}
// CreateTrigger creates a trigger for a build type
func (c *Client) CreateTrigger(buildTypeSelector string, trigger *Trigger) (*Trigger, error) {
p := path.Join(buildTypesPath, buildTypeSelector, triggerPath)
if err := c.doJSONRequest("POST", p, trigger, trigger); err != nil {
return nil, err
}
return trigger, nil
}
//SelectTriggers selects all triggers for the given build type
func (c *Client) SelectTriggers(buildTypeSelector string) (*Triggers, error) {
v := &Triggers{}
p := path.Join(buildTypesPath, buildTypeSelector, triggerPath)
if err := c.doRequest("GET", p, "", nil, v); err != nil {
return nil, err
}
return v, nil
}
// ApplyTemplate applies a build type template to specified build type
func (c *Client) ApplyTemplate(buildTypeSelector string, templateSelector string) (*BuildType, error) {
v := &BuildType{}
p := path.Join(buildTypesPath, buildTypeSelector, templatePath)
if err := c.doRequest("PUT", p, "text/plain", []byte(templateSelector), v); err != nil {
return nil, err
}
return v, nil
}
func (c *Client) GetTagByLocator(locator string) (*Tags, error) {
v := &Tags{}
p := path.Join(buildsPath, locator, tagsPath)
if err := c.doJSONRequest("GET", p, nil, v); err != nil {
return nil, err
}
return v, nil
}
func (c *Client) SetTagByLocator(locator string, tags *Tags) (*Tags, error) {
p := path.Join(buildsPath, locator, tagsPath)
if err := c.doJSONRequest("PUT", p, tags, tags); err != nil {
return nil, err
}
return tags, nil
}
// CancelBuildRequest is a body content of a CancelBuild request.
type CancelBuildRequest struct {
Comment string `json:"comment,omitempty"`
ReturnIntoQueue bool `json:"readdIntoQueue"`
}
// CancelQueuedBuild cancels a queued build.
func (c *Client) CancelQueuedBuild(locator string, req *CancelBuildRequest) (*Build, error) {
v := &Build{}
path := path.Join(buildQueuePath, url.PathEscape(locator))
in := struct {
req *CancelBuildRequest `json:"buildCancelRequest"`
}{req}
if err := c.doJSONRequest("POST", path, in, v); err != nil {
return nil, err
}
return v, nil
}
// CancelBuild cancels a running build.
func (c *Client) CancelBuild(locator string, req *CancelBuildRequest) (*Build, error) {
v := &Build{}
path := path.Join(buildsPath, url.PathEscape(locator))
in := struct {
req *CancelBuildRequest `json:"buildCancelRequest"`
}{req}
if err := c.doJSONRequest("POST", path, in, v); err != nil {
return nil, err
}
return v, nil
}
func (c *Client) doJSONRequest(method, path string, t, v interface{}) error {
body, err := json.Marshal(t)
if err != nil {
return err
}
if err := c.doRequest(method, path, jsonContentType, body, v); err != nil {
return err
}
return nil
}
func (c *Client) doRequest(method string, path string, contentType string, data []byte, v interface{}) error {
Logger.Println(method, path, "\nbody:\n", string(data))
url := c.host + basePathSuffix + path
var body io.Reader
if data != nil {
body = bytes.NewBuffer(data)
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return err
}
rawAuth := []byte(fmt.Sprintf("%v:%v", c.username, c.password))
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(rawAuth))
req.Header.Set("Accept", "application/json")
if len(contentType) > 0 {
req.Header.Set("Content-Type", contentType)
} else {
req.Header.Set("Content-Type", jsonContentType)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if v != nil {
b, _ := ioutil.ReadAll(resp.Body)
Logger.Println("response:\n", string(b))
if json.Unmarshal(b, v) != nil {
return errors.New(string(b))
}
return nil
}
return nil
}