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
/
settings.go
207 lines (174 loc) · 5.76 KB
/
settings.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
// 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"
)
// SettingsService handles communication with the setting
// related methods of the Travis CI API.
type SettingsService struct {
client *Client
}
// Setting represents a Travis CI setting.
//
// Travis CI API docs: https://developer.travis-ci.com/resource/setting#standard-representation
type Setting struct {
// The setting's name
Name *string `json:"name,omitempty"`
// The setting's value
// Currently value can be boolean or integer
Value interface{} `json:"value,omitempty"`
*Metadata
}
// SettingBody is a body to update setting
type SettingBody struct {
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
}
// settingsResponse represents response from the settings endpoints
type settingsResponse struct {
Settings []*Setting `json:"settings,omitempty"`
}
const (
// BuildsOnlyWithTravisYmlSetting is a setting name for builds_only_with_travis_yml
BuildsOnlyWithTravisYmlSetting = "builds_only_with_travis_yml"
// BuildPushesSetting is a setting name for build_pushes
BuildPushesSetting = "build_pushes"
// BuildPullRequestsSetting is a setting name for build_pull_requests
BuildPullRequestsSetting = "build_pull_requests"
// MaximumNumberOfBuildsSetting is a setting name for maximum_number_of_builds
MaximumNumberOfBuildsSetting = "maximum_number_of_builds"
// AutoCancelPushesSetting is a setting name for auto_cancel_pushes
AutoCancelPushesSetting = "auto_cancel_pushes"
// AutoCancelPullRequestsSetting is a setting name for auto_cancel_pull_requests
AutoCancelPullRequestsSetting = "auto_cancel_pull_requests"
)
// FindByRepoId fetches a setting of given repository id and setting name
//
// Travis CI API docs: https://developer.travis-ci.com/resource/setting#find
func (ss *SettingsService) FindByRepoId(ctx context.Context, repoId uint, name string) (*Setting, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/setting/%s", repoId, name), nil)
if err != nil {
return nil, nil, err
}
req, err := ss.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var setting Setting
resp, err := ss.client.Do(ctx, req, &setting)
if err != nil {
return nil, resp, err
}
return &setting, resp, err
}
// FindByRepoSlug fetches a setting of given repository slug and setting name
//
// Travis CI API docs: https://developer.travis-ci.com/resource/setting#find
func (ss *SettingsService) FindByRepoSlug(ctx context.Context, repoSlug string, name string) (*Setting, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/setting/%s", url.QueryEscape(repoSlug), name), nil)
if err != nil {
return nil, nil, err
}
req, err := ss.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var setting Setting
resp, err := ss.client.Do(ctx, req, &setting)
if err != nil {
return nil, resp, err
}
return &setting, resp, err
}
// ListByRepoId fetches a list of settings of given repository id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/settings#for_repository
func (ss *SettingsService) ListByRepoId(ctx context.Context, repoId uint) ([]*Setting, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/settings", repoId), nil)
if err != nil {
return nil, nil, err
}
req, err := ss.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var sr settingsResponse
resp, err := ss.client.Do(ctx, req, &sr)
if err != nil {
return nil, resp, err
}
return sr.Settings, resp, err
}
// ListByRepoSlug fetches a list of settings of given repository slug
//
// Travis CI API docs: https://developer.travis-ci.com/resource/settings#for_repository
func (ss *SettingsService) ListByRepoSlug(ctx context.Context, repoSlug string) ([]*Setting, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/settings", url.QueryEscape(repoSlug)), nil)
if err != nil {
return nil, nil, err
}
req, err := ss.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var sr settingsResponse
resp, err := ss.client.Do(ctx, req, &sr)
if err != nil {
return nil, resp, err
}
return sr.Settings, resp, err
}
// UpdateByRepoId updates a setting with setting property
//
// Travis CI API docs: https://developer.travis-ci.com/resource/setting#update
func (ss *SettingsService) UpdateByRepoId(ctx context.Context, repoId uint, setting *SettingBody) (*Setting, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/setting/%s", repoId, setting.Name), nil)
if err != nil {
return nil, nil, err
}
req, err := ss.client.NewRequest(
http.MethodPatch,
u,
map[string]interface{}{"setting.value": setting.Value},
nil,
)
if err != nil {
return nil, nil, err
}
var s Setting
resp, err := ss.client.Do(ctx, req, &s)
if err != nil {
return nil, resp, err
}
return &s, resp, err
}
// UpdateByRepoSlug updates a setting with setting property
//
// Travis CI API docs: https://developer.travis-ci.com/resource/setting#update
func (ss *SettingsService) UpdateByRepoSlug(ctx context.Context, repoSlug string, setting *SettingBody) (*Setting, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/setting/%s", url.QueryEscape(repoSlug), setting.Name), nil)
if err != nil {
return nil, nil, err
}
req, err := ss.client.NewRequest(
http.MethodPatch,
u,
map[string]interface{}{"setting.value": setting.Value},
nil,
)
if err != nil {
return nil, nil, err
}
var s Setting
resp, err := ss.client.Do(ctx, req, &s)
if err != nil {
return nil, resp, err
}
return &s, resp, err
}