-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.go
300 lines (262 loc) · 9.15 KB
/
variables.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
package config
import (
"cmp"
"fmt"
"net/http"
"os"
"slices"
"sort"
mapset "github.com/deckarep/golang-set/v2"
"github.com/google/go-querystring/query"
"github.com/xanzy/go-gitlab"
"gitlab.com/tozd/go/errors"
)
type filter struct {
EnvironmentScope string `url:"environment_scope"`
}
type opts struct {
Filter filter `url:"filter"`
}
// getVariables populates configuration struct with configuration available
// from GitLab project level variables API endpoint.
func (c *GetCommand) getVariables(client *gitlab.Client, configuration *Configuration) (bool, errors.E) {
fmt.Fprintf(os.Stderr, "Getting project variables...\n")
configuration.Variables = []map[string]interface{}{}
descriptions, errE := getVariablesDescriptions(c.DocsRef)
if errE != nil {
return false, errE
}
// We need "key" later on.
if _, ok := descriptions["key"]; !ok {
return false, errors.New(`"key" field is missing in project variables descriptions`)
}
configuration.VariablesComment = formatDescriptions(descriptions)
u := fmt.Sprintf("projects/%s/variables", gitlab.PathEscape(c.Project))
options := &gitlab.ListProjectVariablesOptions{
PerPage: maxGitLabPageSize,
Page: 1,
}
for {
req, err := client.NewRequest(http.MethodGet, u, options, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to get project variables")
errors.Details(errE)["page"] = options.Page
return false, errE
}
variables := []map[string]interface{}{}
response, err := client.Do(req, &variables)
if err != nil {
// When CI/CD is disabled, this call returns 403.
if response.StatusCode == http.StatusForbidden && options.Page == 1 {
break
}
errE := errors.WithMessage(err, "failed to get project variables")
errors.Details(errE)["page"] = options.Page
return false, errE
}
if len(variables) == 0 {
break
}
for _, variable := range variables {
// Only retain those keys which can be edited through the API
// (which are those available in descriptions).
for key := range variable {
_, ok := descriptions[key]
if !ok {
delete(variable, key)
}
}
if c.EncComment != "" {
variable["comment:value"+c.EncSuffix] = c.EncComment
}
if c.EncSuffix != "" {
variable["value"+c.EncSuffix] = variable["value"]
delete(variable, "value")
}
key, ok := variable["key"]
if !ok {
return false, errors.New(`project variable is missing field "key"`)
}
_, ok = key.(string)
if !ok {
errE := errors.New(`project variable's field "key" is not a string`)
errors.Details(errE)["type"] = fmt.Sprintf("%T", key)
errors.Details(errE)["value"] = key
return false, errE
}
configuration.Variables = append(configuration.Variables, variable)
}
if response.NextPage == 0 {
break
}
options.Page = response.NextPage
}
// We sort by variable key so that we have deterministic order.
sort.Slice(configuration.Variables, func(i, j int) bool {
// We checked that key is string above.
return configuration.Variables[i]["key"].(string) < configuration.Variables[j]["key"].(string) //nolint:forcetypeassert
})
return len(configuration.Variables) > 0, nil
}
// parseVariablesDocumentation parses GitLab's documentation in Markdown for
// project level variables API endpoint and extracts description of fields
// used to describe an individual variable.
func parseVariablesDocumentation(input []byte) (map[string]string, errors.E) {
return parseTable(input, "Create a variable", nil)
}
// getVariablesDescriptions obtains description of fields used to describe an individual
// variable from GitLab's documentation for project level variables API endpoint.
func getVariablesDescriptions(gitRef string) (map[string]string, errors.E) {
data, err := downloadFile(fmt.Sprintf("https://gitlab.com/gitlab-org/gitlab/-/raw/%s/doc/api/project_level_variables.md", gitRef))
if err != nil {
return nil, errors.WithMessage(err, "failed to get project variables descriptions")
}
return parseVariablesDocumentation(data)
}
// updateVariables updates GitLab project's variables using GitLab project level
// variables API endpoint based on the configuration struct.
func (c *SetCommand) updateVariables(client *gitlab.Client, configuration *Configuration) errors.E {
if configuration.Variables == nil {
return nil
}
fmt.Fprintf(os.Stderr, "Updating project variables...\n")
options := &gitlab.ListProjectVariablesOptions{
PerPage: maxGitLabPageSize,
Page: 1,
}
variables := []*gitlab.ProjectVariable{}
for {
vs, response, err := client.ProjectVariables.ListVariables(c.Project, options)
if err != nil {
errE := errors.WithMessage(err, "failed to get project variables")
errors.Details(errE)["page"] = options.Page
return errE
}
variables = append(variables, vs...)
if response.NextPage == 0 {
break
}
options.Page = response.NextPage
}
type Variable struct {
Key string
EnvironmentScope string
}
existingVariablesSet := mapset.NewThreadUnsafeSet[Variable]()
for _, variable := range variables {
existingVariablesSet.Add(Variable{
Key: variable.Key,
EnvironmentScope: variable.EnvironmentScope,
})
}
wantedVariablesSet := mapset.NewThreadUnsafeSet[Variable]()
for i, variable := range configuration.Variables {
key, ok := variable["key"]
if !ok {
errE := errors.Errorf(`project variable is missing field "key"`)
errors.Details(errE)["index"] = i
return errE
}
k, ok := key.(string)
if !ok {
errE := errors.New(`project variable's field "key" is not a string`)
errors.Details(errE)["index"] = i
errors.Details(errE)["type"] = fmt.Sprintf("%T", key)
errors.Details(errE)["value"] = key
return errE
}
environmentScope, ok := variable["environment_scope"]
if !ok {
errE := errors.Errorf(`project variable is missing field "environment_scope"`)
errors.Details(errE)["index"] = i
return errE
}
e, ok := environmentScope.(string)
if !ok {
errE := errors.New(`project variable's field "environment_scope" is not a string`)
errors.Details(errE)["index"] = i
errors.Details(errE)["type"] = fmt.Sprintf("%T", environmentScope)
errors.Details(errE)["value"] = environmentScope
return errE
}
wantedVariablesSet.Add(Variable{
Key: k,
EnvironmentScope: e,
})
}
extraVariables := existingVariablesSet.Difference(wantedVariablesSet).ToSlice()
slices.SortFunc(extraVariables, func(a Variable, b Variable) int {
res := cmp.Compare(a.Key, b.Key)
if res != 0 {
return res
}
return cmp.Compare(a.EnvironmentScope, b.EnvironmentScope)
})
for _, variable := range extraVariables {
_, err := client.ProjectVariables.RemoveVariable(
c.Project,
variable.Key,
&gitlab.RemoveProjectVariableOptions{Filter: &gitlab.VariableFilter{EnvironmentScope: variable.EnvironmentScope}},
nil,
)
if err != nil {
errE := errors.WithMessage(err, "failed to remove project variable")
errors.Details(errE)["key"] = variable.Key
errors.Details(errE)["environmentScope"] = variable.EnvironmentScope
return errE
}
}
for i, variable := range configuration.Variables {
// We made sure above that all variables in configuration have a string key and environment scope.
key := variable["key"].(string) //nolint:errcheck,forcetypeassert
environmentScope := variable["environment_scope"].(string) //nolint:errcheck,forcetypeassert
if existingVariablesSet.Contains(Variable{
Key: key,
EnvironmentScope: environmentScope,
}) {
// Update existing variable.
u := fmt.Sprintf("projects/%s/variables/%s", gitlab.PathEscape(c.Project), gitlab.PathEscape(key))
req, err := client.NewRequest(http.MethodPut, u, variable, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to update project variable")
errors.Details(errE)["index"] = i
errors.Details(errE)["key"] = key
errors.Details(errE)["environmentScope"] = environmentScope
}
q, err := query.Values(opts{filter{environmentScope}})
if err != nil {
errE := errors.WithMessage(err, "failed to update project variable")
errors.Details(errE)["index"] = i
errors.Details(errE)["key"] = key
errors.Details(errE)["environmentScope"] = environmentScope
}
req.URL.RawQuery = q.Encode()
_, err = client.Do(req, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to update project variable")
errors.Details(errE)["index"] = i
errors.Details(errE)["key"] = key
errors.Details(errE)["environmentScope"] = environmentScope
}
} else {
// Create new variable.
u := fmt.Sprintf("projects/%s/variables", gitlab.PathEscape(c.Project))
req, err := client.NewRequest(http.MethodPost, u, variable, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to create project variable")
errors.Details(errE)["index"] = i
errors.Details(errE)["key"] = key
errors.Details(errE)["environmentScope"] = environmentScope
}
_, err = client.Do(req, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to create project variable")
errors.Details(errE)["index"] = i
errors.Details(errE)["key"] = key
errors.Details(errE)["environmentScope"] = environmentScope
return errE
}
}
}
return nil
}