This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
builds.go
182 lines (154 loc) · 5.16 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
174
175
176
177
178
179
180
181
182
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Fragments of this file have been copied from the go-github (https://github.com/google/go-github)
// project, and is therefore licensed under the following copyright:
// Copyright 2013 The go-github AUTHORS. All rights reserved.
package travis
import (
"fmt"
"net/http"
)
// BuildsService handles communication with the builds
// related methods of the Travis CI API.
type BuildsService struct {
client *Client
}
// Build represents a Travis CI build
type Build struct {
Id uint `json:"id,omitempty"`
RepositoryId uint `json:"repository_id,omitempty"`
Slug string `json:"slug,omitempty"`
CommitId uint `json:"commit_id,omitempty"`
Number string `json:"number,omitempty"`
// Config Config `json:"config,omitempty"`
PullRequest bool `json:"pull_request,omitempty"`
PullRequestTitle string `json:"pull_request_title,omitempty"`
PullRequestNumber uint `json:"pull_request_number,omitempty"`
State string `json:"state,omitempty"`
StartedAt string `json:"started_at,omitempty"`
FinishedAt string `json:"finished_at,omitempty"`
Duration uint `json:"duration,omitempty"`
JobIds []uint `json:"job_ids,omitempty"`
AfterNumber uint `json:"after_number,omitempty"`
EventType string `json:"event_type,omitempty"`
}
// listBuildsResponse represents the response of a call
// to the Travis CI list builds endpoint.
type listBuildsResponse struct {
Builds []Build `json:"builds,omitempty"`
Commits []Commit `json:"commits,omitempty"`
Jobs []Job `json:"jobs,omitempty"`
}
// getBuildResponse represents the response of a call
// to the Travis CI get build endpoint.
type getBuildResponse struct {
Build Build `json:"build"`
Commit Commit `json:"commit"`
Jobs []Job `json:"jobs"`
}
// BuildListOptions specifies the optional parameters to the
// BuildsService.List method.
type BuildListOptions struct {
ListOptions
Ids []uint `url:"ids,omitempty"`
RepositoryId uint `url:"repository_id,omitempty"`
Slug string `url:"slug,omitempty"`
Number string `url:"number,omitempty"`
EventType string `url:"event_type,omitempty"`
}
// List the builds for the authenticated user.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (bs *BuildsService) List(opt *BuildListOptions) ([]Build, []Job, []Commit, *http.Response, error) {
u, err := urlWithOptions("/builds", opt)
if err != nil {
return nil, nil, nil, nil, err
}
req, err := bs.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, nil, nil, err
}
var buildsResp listBuildsResponse
resp, err := bs.client.Do(req, &buildsResp)
if err != nil {
return nil, nil, nil, resp, err
}
return buildsResp.Builds, buildsResp.Jobs, buildsResp.Commits, resp, err
}
// List a repository builds based on it's provided slug.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (bs *BuildsService) ListFromRepository(slug string, opt *BuildListOptions) ([]Build, []Job, []Commit, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/repos/%v/builds", slug), opt)
if err != nil {
return nil, nil, nil, nil, err
}
req, err := bs.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, nil, nil, err
}
var buildsResp listBuildsResponse
resp, err := bs.client.Do(req, &buildsResp)
if err != nil {
return nil, nil, nil, resp, err
}
return buildsResp.Builds, buildsResp.Jobs, buildsResp.Commits, resp, err
}
// Get fetches a build based on the provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (bs *BuildsService) Get(id uint) (*Build, []Job, *Commit, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/builds/%d", id), nil)
if err != nil {
return nil, nil, nil, nil, err
}
req, err := bs.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, nil, nil, err
}
var buildResp getBuildResponse
resp, err := bs.client.Do(req, &buildResp)
if err != nil {
return nil, nil, nil, resp, err
}
return &buildResp.Build, buildResp.Jobs, &buildResp.Commit, resp, err
}
// Cancel build with the provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (bs *BuildsService) Cancel(id uint) (*http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/builds/%d/cancel", id), nil)
if err != nil {
return nil, err
}
req, err := bs.client.NewRequest("POST", u, nil, nil)
if err != nil {
return nil, err
}
resp, err := bs.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
// Restart build with the provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (bs *BuildsService) Restart(id uint) (*http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/builds/%d/restart", id), nil)
if err != nil {
return nil, err
}
req, err := bs.client.NewRequest("POST", u, nil, nil)
if err != nil {
return nil, err
}
resp, err := bs.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}