/
builds.go
173 lines (139 loc) · 4.9 KB
/
builds.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
// Copyright 2014 Mark Wolfe. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package buildkite
import "fmt"
// BuildsService handles communication with the build related
// methods of the buildkite API.
//
// buildkite API docs: https://buildkite.com/docs/api/builds
type BuildsService struct {
client *Client
}
// Build represents a build which has run in buildkite
type Build struct {
ID *string `json:"id,omitempty"`
URL *string `json:"url,omitempty"`
WebURL *string `json:"web_url,omitempty"`
Number *int `json:"number,omitempty"`
State *string `json:"state,omitempty"`
Message *string `json:"message,omitempty"`
Commit *string `json:"commit,omitempty"`
Branch *string `json:"branch,omitempty"`
Env map[string]string `json:"env,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
ScheduledAt *Timestamp `json:"scheduled_at,omitempty"`
StartedAt *Timestamp `json:"started_at,omitempty"`
FinishedAt *Timestamp `json:"finished_at,omitempty"`
MetaData interface{} `json:"meta_data,omitempty"`
// jobs run during the build
Jobs []*Job `json:"jobs,omitempty"`
// the project this build is associated with
Project *Project `json:"project,omitempty"`
}
// Job represents a job run during a build in buildkite
type Job struct {
ID *string `json:"id,omitempty"`
Type *string `json:"type,omitempty"`
Name *string `json:"name,omitempty"`
State *string `json:"state,omitempty"`
LogsURL *string `json:"logs_url,omitempty"`
RawLogsURL *string `json:"raw_log_url,omitempty"`
Command *string `json:"command,omitempty"`
ExitStatus *int `json:"exit_status,omitempty"`
ArtifactPaths *string `json:"artifact_paths,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
ScheduledAt *Timestamp `json:"scheduled_at,omitempty"`
StartedAt *Timestamp `json:"started_at,omitempty"`
FinishedAt *Timestamp `json:"finished_at,omitempty"`
}
// BuildsListOptions specifies the optional parameters to the
// BuildsService.List method.
type BuildsListOptions struct {
// State of builds to list. Possible values are: running, scheduled, passed,
// failed, canceled, skipped and not_run. Default is "".
State string `url:"state,omitempty"`
// Branch filter by the name of the branch. Default is "".
Branch string `url:"branch,omitempty"`
ListOptions
}
// Get fetches a build.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#get-a-build
func (as *BuildsService) Get(org string, project string, id string) (*Build, *Response, error) {
u := fmt.Sprintf("v1/organizations/%s/projects/%s/builds/%s", org, project, id)
req, err := as.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
build := new(Build)
resp, err := as.client.Do(req, build)
if err != nil {
return nil, resp, err
}
return build, resp, err
}
// List the builds for the current user.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#list-all-builds
func (bs *BuildsService) List(opt *BuildsListOptions) ([]Build, *Response, error) {
var u string
u = fmt.Sprintf("v1/builds")
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
orgs := new([]Build)
resp, err := bs.client.Do(req, orgs)
if err != nil {
return nil, resp, err
}
return *orgs, resp, err
}
// ListByOrg lists the builds within the specified orginisation.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#list-builds-for-an-organization
func (bs *BuildsService) ListByOrg(org string, opt *BuildsListOptions) ([]Build, *Response, error) {
var u string
u = fmt.Sprintf("v1/organizations/%s/builds", org)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
orgs := new([]Build)
resp, err := bs.client.Do(req, orgs)
if err != nil {
return nil, resp, err
}
return *orgs, resp, err
}
// ListByProject lists the builds for a project within the specified originisation.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#list-builds-for-a-project
func (bs *BuildsService) ListByProject(org string, project string, opt *BuildsListOptions) ([]Build, *Response, error) {
var u string
u = fmt.Sprintf("v1/organizations/%s/projects/%s/builds", org, project)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
orgs := new([]Build)
resp, err := bs.client.Do(req, orgs)
if err != nil {
return nil, resp, err
}
return *orgs, resp, err
}