-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotected_branches.go
378 lines (333 loc) · 12.5 KB
/
protected_branches.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package config
import (
"fmt"
"net/http"
"os"
"slices"
"sort"
mapset "github.com/deckarep/golang-set/v2"
"github.com/xanzy/go-gitlab"
"gitlab.com/tozd/go/errors"
)
// getProtectedBranches populates configuration struct with configuration available
// from GitLab protected branches API endpoint.
func (c *GetCommand) getProtectedBranches(client *gitlab.Client, configuration *Configuration) (bool, errors.E) { //nolint:unparam
fmt.Fprintf(os.Stderr, "Getting protected branches...\n")
configuration.ProtectedBranches = []map[string]interface{}{}
descriptions, errE := getProtectedBranchesDescriptions(c.DocsRef)
if errE != nil {
return false, errE
}
// We need "name" later on.
if _, ok := descriptions["name"]; !ok {
return false, errors.New(`"name" field is missing in protected branches descriptions`)
}
configuration.ProtectedBranchesComment = formatDescriptions(descriptions)
u := fmt.Sprintf("projects/%s/protected_branches", gitlab.PathEscape(c.Project))
options := &gitlab.ListProtectedBranchesOptions{ //nolint:exhaustruct
ListOptions: gitlab.ListOptions{
PerPage: maxGitLabPageSize,
Page: 1,
},
}
for {
req, err := client.NewRequest(http.MethodGet, u, options, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to get protected branches")
errors.Details(errE)["page"] = options.Page
return false, errE
}
protectedBranches := []map[string]interface{}{}
response, err := client.Do(req, &protectedBranches)
if err != nil {
errE := errors.WithMessage(err, "failed to get protected branches")
errors.Details(errE)["page"] = options.Page
return false, errE
}
if len(protectedBranches) == 0 {
break
}
for _, protectedBranch := range protectedBranches {
// We rename to be consistent between getting and updating.
protectedBranch["allowed_to_push"] = protectedBranch["push_access_levels"]
protectedBranch["allowed_to_merge"] = protectedBranch["merge_access_levels"]
protectedBranch["allowed_to_unprotect"] = protectedBranch["unprotect_access_levels"]
// Making sure ids and levels are an integer.
castFloatsToInts(protectedBranch)
// Only retain those keys which can be edited through the API
// (which are those available in descriptions).
for key := range protectedBranch {
_, ok := descriptions[key]
if !ok {
delete(protectedBranch, key)
}
}
// Make the description be a comment for the sequence item.
renameMapField(protectedBranch, "access_level_description", "comment:")
name, ok := protectedBranch["name"]
if !ok {
return false, errors.New(`protected branch is missing field "name"`)
}
_, ok = name.(string)
if !ok {
errE := errors.New(`protected branch's field "name" is not a string`)
errors.Details(errE)["type"] = fmt.Sprintf("%T", name)
errors.Details(errE)["value"] = name
return false, errE
}
configuration.ProtectedBranches = append(configuration.ProtectedBranches, protectedBranch)
}
if response.NextPage == 0 {
break
}
options.Page = response.NextPage
}
// We sort by protected branch's name so that we have deterministic order.
sort.Slice(configuration.ProtectedBranches, func(i, j int) bool {
// We checked that name is string above.
return configuration.ProtectedBranches[i]["name"].(string) < configuration.ProtectedBranches[j]["name"].(string) //nolint:forcetypeassert,errcheck
})
return false, nil
}
// parseProtectedBranchesDocumentation parses GitLab's documentation in Markdown for
// protected branches API endpoint and extracts description of fields used to describe
// protected branches.
func parseProtectedBranchesDocumentation(input []byte) (map[string]string, errors.E) {
return parseTable(input, "Update a protected branch", nil)
}
// getProtectedBranchesDescriptions obtains description of fields used to describe
// an individual protected branch from GitLab's documentation for protected branches API endpoint.
func getProtectedBranchesDescriptions(gitRef string) (map[string]string, errors.E) {
data, err := downloadFile(fmt.Sprintf("https://gitlab.com/gitlab-org/gitlab/-/raw/%s/doc/api/protected_branches.md", gitRef))
if err != nil {
return nil, errors.WithMessage(err, "failed to get protected branches descriptions")
}
return parseProtectedBranchesDocumentation(data)
}
// updateProtectedBranches updates GitLab project's protected branches using GitLab
// protected branches API endpoint based on the configuration struct.
//
// Access levels without the ID field are matched to existing access labels based on
// their fields. Unmatched access levels are created as new.
func (c *SetCommand) updateProtectedBranches(client *gitlab.Client, configuration *Configuration) errors.E { //nolint:maintidx
if configuration.ProtectedBranches == nil {
return nil
}
fmt.Fprintf(os.Stderr, "Updating protected branches...\n")
options := &gitlab.ListProtectedBranchesOptions{ //nolint:exhaustruct
ListOptions: gitlab.ListOptions{
PerPage: maxGitLabPageSize,
Page: 1,
},
}
protectedBranches := []*gitlab.ProtectedBranch{}
for {
pb, response, err := client.ProtectedBranches.ListProtectedBranches(c.Project, options)
if err != nil {
errE := errors.WithMessage(err, "failed to get protected branches")
errors.Details(errE)["page"] = options.Page
return errE
}
protectedBranches = append(protectedBranches, pb...)
if response.NextPage == 0 {
break
}
options.Page = response.NextPage
}
existingProtectedBranches := map[string]*gitlab.ProtectedBranch{}
existingProtectedBranchesSet := mapset.NewThreadUnsafeSet[string]()
for _, protectedBranch := range protectedBranches {
existingProtectedBranchesSet.Add(protectedBranch.Name)
existingProtectedBranches[protectedBranch.Name] = protectedBranch
}
wantedProtectedBranchesSet := mapset.NewThreadUnsafeSet[string]()
for i, protectedBranch := range configuration.ProtectedBranches {
name, ok := protectedBranch["name"]
if !ok {
errE := errors.Errorf(`protected branch is missing field "name"`)
errors.Details(errE)["index"] = i
return errE
}
n, ok := name.(string)
if !ok {
errE := errors.New(`protected branch's field "name" is not a string`)
errors.Details(errE)["index"] = i
errors.Details(errE)["type"] = fmt.Sprintf("%T", name)
errors.Details(errE)["value"] = name
return errE
}
wantedProtectedBranchesSet.Add(n)
}
extraProtectedBranchesSlice := existingProtectedBranchesSet.Difference(wantedProtectedBranchesSet).ToSlice()
slices.Sort(extraProtectedBranchesSlice)
for _, protectedBranchName := range extraProtectedBranchesSlice {
_, err := client.ProtectedBranches.UnprotectRepositoryBranches(c.Project, protectedBranchName)
if err != nil {
errE := errors.WithMessage(err, "failed to unprotect branch")
errors.Details(errE)["branch"] = protectedBranchName
return errE
}
}
// We do not add branch index to errors because we use
// index in errors for various access level types.
for i, protectedBranch := range configuration.ProtectedBranches {
// We made sure above that all protected branches in configuration have a string name.
name := protectedBranch["name"].(string) //nolint:errcheck,forcetypeassert
// If project already have this protected branch, we update it.
// Others are updated if they contain an ID or created new if they do not contain an ID.
if existingProtectedBranchesSet.Contains(name) { //nolint:nestif
// We know it exists.
existingProtectedBranch := existingProtectedBranches[name]
// We have to mark any access level which does not exist anymore for deletion.
for _, ii := range []struct {
Name string
AccessLevels []*gitlab.BranchAccessDescription
}{
{"allowed_to_push", existingProtectedBranch.PushAccessLevels},
{"allowed_to_merge", existingProtectedBranch.MergeAccessLevels},
{"allowed_to_unprotect", existingProtectedBranch.UnprotectAccessLevels},
} {
existingAccessLevelsSet := mapset.NewThreadUnsafeSet[int]()
accessLevelToIDs := map[int]int{}
userIDtoIDs := map[int]int{}
groupIDtoIDs := map[int]int{}
for _, accessLevel := range ii.AccessLevels {
if accessLevel.AccessLevel != 0 {
accessLevelToIDs[int(accessLevel.AccessLevel)] = accessLevel.ID
}
if accessLevel.UserID != 0 {
userIDtoIDs[accessLevel.UserID] = accessLevel.ID
}
if accessLevel.GroupID != 0 {
groupIDtoIDs[accessLevel.GroupID] = accessLevel.ID
}
existingAccessLevelsSet.Add(accessLevel.ID)
}
wantedAccessLevels, ok := protectedBranch[ii.Name]
if !ok {
wantedAccessLevels = []interface{}{}
}
levels, ok := wantedAccessLevels.([]interface{})
if !ok {
errE := errors.New("invalid access levels for protected branch")
errors.Details(errE)["index"] = i
errors.Details(errE)["accessLevels"] = ii.Name
errors.Details(errE)["branch"] = name
return errE
}
// Set access level IDs if a matching existing access level can be found.
for j, level := range levels {
l, ok := level.(map[string]interface{})
if !ok {
errE := errors.New("invalid access level for protected branch")
errors.Details(errE)["index"] = i
errors.Details(errE)["levelIndex"] = j
errors.Details(errE)["accessLevels"] = ii.Name
errors.Details(errE)["branch"] = name
return errE
}
// Is access level ID already set?
id, ok := l["id"]
if ok {
// If ID is provided, the access level should exist.
iid, ok := id.(int) //nolint:govet
if !ok {
errE := errors.New(`access level's field "id" for protected branch is not an integer`)
errors.Details(errE)["index"] = i
errors.Details(errE)["levelIndex"] = j
errors.Details(errE)["accessLevels"] = ii.Name
errors.Details(errE)["branch"] = name
errors.Details(errE)["type"] = fmt.Sprintf("%T", id)
errors.Details(errE)["value"] = id
return errE
}
if existingAccessLevelsSet.Contains(iid) {
continue
}
// Access level does not exist with that ID. We remove the ID and leave to matching to
// find the correct ID, if it exists. Otherwise we will just create a new access level.
delete(l, "id")
}
accessLevel, ok := l["access_level"]
if ok {
a, ok := accessLevel.(int) //nolint:govet
if ok {
id, ok = accessLevelToIDs[a]
if ok {
l["id"] = id
}
}
}
userID, ok := l["user_id"]
if ok {
u, ok := userID.(int) //nolint:govet
if ok {
id, ok = userIDtoIDs[u]
if ok {
l["id"] = id
}
}
}
groupID, ok := l["group_id"]
if ok {
g, ok := groupID.(int)
if ok {
id, ok = groupIDtoIDs[g]
if ok {
l["id"] = id
}
}
}
}
wantedAccessLevelsSet := mapset.NewThreadUnsafeSet[int]()
for _, level := range levels {
// We know it has to be a map.
id, ok := level.(map[string]interface{})["id"] //nolint:errcheck
if ok {
// We checked that id is int above.
wantedAccessLevelsSet.Add(id.(int)) //nolint:forcetypeassert,errcheck
}
}
extraAccessLevels := existingAccessLevelsSet.Difference(wantedAccessLevelsSet).ToSlice()
slices.Sort(extraAccessLevels)
for _, accessLevelID := range extraAccessLevels {
protectedBranch[ii.Name] = append(levels, map[string]interface{}{
"id": accessLevelID,
"_destroy": true,
})
}
}
req, err := client.NewRequest(http.MethodPatch, fmt.Sprintf("projects/%s/protected_branches/%s", gitlab.PathEscape(c.Project), name), protectedBranch, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to update protected branch")
errors.Details(errE)["index"] = i
errors.Details(errE)["branch"] = name
return errE
}
_, err = client.Do(req, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to update protected branch")
errors.Details(errE)["index"] = i
errors.Details(errE)["branch"] = name
return errE
}
} else {
// We create a new protected branch.
req, err := client.NewRequest(http.MethodPost, fmt.Sprintf("projects/%s/protected_branches", gitlab.PathEscape(c.Project)), protectedBranch, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to protect branch")
errors.Details(errE)["index"] = i
errors.Details(errE)["branch"] = name
return errE
}
_, err = client.Do(req, nil)
if err != nil {
errE := errors.WithMessage(err, "failed to protect branch")
errors.Details(errE)["index"] = i
errors.Details(errE)["branch"] = name
return errE
}
}
}
return nil
}