This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
crons.go
266 lines (225 loc) · 7.58 KB
/
crons.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
// 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.
package travis
import (
"context"
"fmt"
"net/http"
"net/url"
)
// CronsService handles communication with the crons
// related methods of the Travis CI API.
type CronsService struct {
client *Client
}
// Cron is a standard representation of an individual cron
//
// Travis CI API docs: https://developer.travis-ci.com/resource/cron#standard-representation
type Cron struct {
// Value uniquely identifying the cron
Id *uint `json:"id,omitempty"`
// Github repository to which this cron belongs
Repository *Repository `json:"repository,omitempty"`
// Git branch of repository to which this cron belongs
Branch *Branch `json:"branch,omitempty"`
// Interval at which the cron will run (can be "daily", "weekly" or "monthly")
Interval *string `json:"interval,omitempty"`
// Whether a cron build should run if there has been a build on this branch in the last 24 hours
DontRunIfRecentBuildExists *bool `json:"dont_run_if_recent_build_exists,omitempty"`
// When the cron ran last
LastRun *string `json:"last_run,omitempty"`
// When the cron is scheduled to run next
NextRun *string `json:"next_run,omitempty"`
// When the cron was created
CreatedAt *string `json:"created_at,omitempty"`
// Whether the cron is active or not
Active *bool `json:"active,omitempty"`
*Metadata
}
// CronBody specifies body for
// creating cron.
type CronBody struct {
// Interval at which the cron will run (can be "daily", "weekly" or "monthly")
Interval string `json:"cron.interval,omitempty"`
// Whether a cron build should run if there has been a build on this branch in the last 24 hours
DontRunIfRecentBuildExists bool `json:"cron.dont_run_if_recent_build_exists"`
}
// CronOption specifies options for
// fetching cron.
type CronOption struct {
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}
// CronsOption specifies options for
// fetching crons.
type CronsOption struct {
// How many crons to include in the response
Limit int `url:"limit,omitempty"`
// How many crons to skip before the first entry in the response
Offset int `url:"offset,omitempty"`
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}
// cronsResponse represents a response
// from crons endpoints
type cronsResponse struct {
Crons []*Cron `json:"crons,omitempty"`
}
const (
CronIntervalDaily = "daily"
CronIntervalWeekly = "weekly"
CronIntervalMonthly = "monthly"
)
// Find fetches a cron based on the provided id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/cron#find
func (cs *CronsService) Find(ctx context.Context, id uint, opt *CronOption) (*Cron, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("cron/%d", id), opt)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cron Cron
resp, err := cs.client.Do(ctx, req, &cron)
if err != nil {
return nil, resp, err
}
return &cron, resp, err
}
// FindByRepoId fetches a cron based on the provided repository id and branch name
//
// Travis CI API docs: https://developer.travis-ci.com/resource/cron#for_branch
func (cs *CronsService) FindByRepoId(ctx context.Context, repoId uint, branch string, opt *CronOption) (*Cron, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/branch/%s/cron", repoId, branch), opt)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cron Cron
resp, err := cs.client.Do(ctx, req, &cron)
if err != nil {
return nil, resp, err
}
return &cron, resp, err
}
// FindByRepoSlug fetches a cron based on the provided repository slug and branch name
//
// Travis CI API docs: https://developer.travis-ci.com/resource/cron#for_branch
func (cs *CronsService) FindByRepoSlug(ctx context.Context, repoSlug string, branch string, opt *CronOption) (*Cron, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/branch/%s/cron", url.QueryEscape(repoSlug), branch), opt)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cron Cron
resp, err := cs.client.Do(ctx, req, &cron)
if err != nil {
return nil, resp, err
}
return &cron, resp, err
}
// ListByRepoId fetches crons based on the provided repository id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/crons#for_repository
func (cs *CronsService) ListByRepoId(ctx context.Context, repoId uint, opt *CronsOption) ([]*Cron, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/crons", repoId), opt)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cr cronsResponse
resp, err := cs.client.Do(ctx, req, &cr)
if err != nil {
return nil, resp, err
}
return cr.Crons, resp, err
}
// ListByRepoSlug fetches crons based on the provided repository slug
//
// Travis CI API docs: https://developer.travis-ci.com/resource/crons#for_repository
func (cs *CronsService) ListByRepoSlug(ctx context.Context, repoSlug string, opt *CronsOption) ([]*Cron, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/crons", url.QueryEscape(repoSlug)), opt)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cr cronsResponse
resp, err := cs.client.Do(ctx, req, &cr)
if err != nil {
return nil, resp, err
}
return cr.Crons, resp, err
}
// CreateByRepoId creates a cron based on the provided repository id and branch name
//
// Travis CI API docs: https://developer.travis-ci.com/resource/cron#create
func (cs *CronsService) CreateByRepoId(ctx context.Context, repoId uint, branchName string, cron *CronBody) (*Cron, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/branch/%s/cron", repoId, branchName), nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodPost, u, cron, nil)
if err != nil {
return nil, nil, err
}
var c Cron
resp, err := cs.client.Do(ctx, req, &c)
if err != nil {
return nil, resp, err
}
return &c, resp, err
}
// CreateByRepoSlug creates a cron based on the provided repository slug and branch name
//
// Travis CI API docs: https://developer.travis-ci.com/resource/cron#create
func (cs *CronsService) CreateByRepoSlug(ctx context.Context, repoSlug string, branchName string, cron *CronBody) (*Cron, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/branch/%s/cron", url.QueryEscape(repoSlug), branchName), nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodPost, u, cron, nil)
if err != nil {
return nil, nil, err
}
var c Cron
resp, err := cs.client.Do(ctx, req, &c)
if err != nil {
return nil, resp, err
}
return &c, resp, err
}
// Delete deletes a cron based on the provided id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/cron#delete
func (cs *CronsService) Delete(ctx context.Context, id uint) (*http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("cron/%d", id), nil)
if err != nil {
return nil, err
}
req, err := cs.client.NewRequest(http.MethodDelete, u, nil, nil)
if err != nil {
return nil, err
}
resp, err := cs.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, err
}