/
calls_variables.go
252 lines (215 loc) · 7.06 KB
/
calls_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
package gitlab
import (
"encoding/json"
"fmt"
"strings"
"github.com/sirupsen/logrus"
)
func (r *gitlabClient) GetVariableFrom(id int, resource string, variable string) (string, error) {
uri := fmt.Sprintf("/%s/%d/variables/%s", resource, id, variable)
fetchUri := fmt.Sprintf("https://%s%s%s", r.BaseUrl, r.ApiPath, uri)
resp, resperr := r.Client.R().
SetHeader("PRIVATE-TOKEN", r.Token).
SetHeader("Content-Type", "application/json").
Get(fetchUri)
if resperr != nil {
logrus.WithError(resperr).Error("Oops")
return "", resperr
}
return string(resp.Body()[:]), nil
}
func getVariablesFrom(r *gitlabClient, id int, resource string) (Variables, error) {
nextPage := "1"
combinedResults := ""
for {
uri := fmt.Sprintf("/%s/%d/variables", resource, id)
fetchUri := fmt.Sprintf("https://%s%s%s?page=%s", r.BaseUrl, r.ApiPath, uri, nextPage)
resp, resperr := r.Client.R().
SetHeader("PRIVATE-TOKEN", r.Token).
SetHeader("Content-Type", "application/json").
Get(fetchUri)
if resperr != nil {
logrus.WithError(resperr).Error("Oops")
return Variables{}, resperr
}
items := strings.TrimPrefix(string(resp.Body()[:]), "[")
items = strings.TrimSuffix(items, "]")
if combinedResults == "" {
combinedResults += items
} else {
combinedResults += fmt.Sprintf(", %s", items)
}
currentPage := resp.Header().Get("X-Page")
nextPage = resp.Header().Get("X-Next-Page")
totalPages := resp.Header().Get("X-Total-Pages")
if currentPage == totalPages {
break
}
}
surroundArray := fmt.Sprintf("[%s]", combinedResults)
var variables Variables
marshErr := json.Unmarshal([]byte(surroundArray), &variables)
if marshErr != nil {
logrus.Fatal("Cannot marshall Pipeline", marshErr)
return Variables{}, marshErr
}
if resource == "projects" {
projectInfo, perr := r.GetProject(id)
if perr != nil {
logrus.Error("Cannot marshall Pipeline", marshErr)
return variables, perr
}
// for _, v := range variables {
for k := range variables {
// v.Source = projectInfo.Path
variables[k].Source = projectInfo.PathWithNamespace
}
}
if resource == "groups" {
groupInfo, gerr := r.GetGroup(id)
if gerr != nil {
logrus.Error("Cannot marshall Pipeline", marshErr)
return variables, gerr
}
for k := range variables {
// variables[k].Source = groupInfo.Path
variables[k].Source = groupInfo.FullPath
}
}
return variables, nil
}
// GetCicdVariables - Returns all CICD Variables for a ProjectID
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/project_level_variables.html
// https://docs.gitlab.com/ee/api/group_level_variables.html
func (r *gitlabClient) GetCicdVariables(projectID int) (Variables, error) {
// curl -Ls --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://git.alteryx.com/api/v4/projects/5844/variables" | jq .
// Fetch the ProjectID, extract .namespace.id (this is the immediate containing group)
// Fetch the GroupID, extract .parent_id until 'null'
uri := fmt.Sprintf("/projects/%d/variables", projectID)
fetchUri := fmt.Sprintf("https://%s%s%s", r.BaseUrl, r.ApiPath, uri)
resp, resperr := r.Client.R().
SetHeader("PRIVATE-TOKEN", r.Token).
SetHeader("Content-Type", "application/json").
Get(fetchUri)
if resperr != nil {
logrus.WithError(resperr).Error("Oops")
return Variables{}, resperr
}
var variables Variables
marshErr := json.Unmarshal(resp.Body(), &variables)
if marshErr != nil {
logrus.Fatal("Cannot marshall Pipeline", marshErr)
return Variables{}, resperr
}
projectInfo, perr := r.GetProject(projectID)
if perr != nil {
logrus.Error("Cannot marshall Pipeline", marshErr)
return variables, perr
}
for k := range variables {
variables[k].Source = projectInfo.PathWithNamespace
}
if projectInfo.Namespace.ID > 0 {
groupID := projectInfo.Namespace.ID
for {
parentVariables, verr := getVariablesFrom(r, groupID, "groups")
if verr != nil {
logrus.Error("Cannot marshall Pipeline", marshErr)
return variables, resperr
}
variables = append(variables, parentVariables...)
groupInfo, gerr := r.GetGroup(groupID)
if gerr != nil {
logrus.Error("Cannot marshall Pipeline", marshErr)
return variables, gerr
}
if groupInfo.ParentID == 0 {
break
}
groupID = groupInfo.ParentID
}
}
return variables, nil
}
// GetCicdVariablesFromGroup - Returns all CICD Variables for a ProjectID
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/project_level_variables.html
// https://docs.gitlab.com/ee/api/group_level_variables.html
func (r *gitlabClient) GetCicdVariablesFromGroup(groupID int, includeProjects bool) (Variables, error) {
// curl -Ls --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://git.alteryx.com/api/v4/projects/5844/variables" | jq .
// Fetch the ProjectID, extract .namespace.id (this is the immediate containing group)
// Fetch the GroupID, extract .parent_id until 'null'
var variables Variables
topVariables, verr := getVariablesFrom(r, groupID, "groups")
if verr != nil {
logrus.Error("Cannot marshall Pipeline", verr)
return variables, verr
}
variables = append(variables, topVariables...)
if includeProjects {
topProjects, perr := r.GetGroupProjects(groupID)
if perr != nil {
logrus.Error("Failed to get top level group Projects", perr)
return variables, perr
}
for _, v := range topProjects {
projVariables, verr := getVariablesFrom(r, v.ID, "projects")
if verr != nil {
logrus.Error("Failed to get variables from topProjects ", verr)
return variables, verr
}
variables = append(variables, projVariables...)
}
}
subGroups, gerr := r.GetDescendantGroups(groupID)
if gerr != nil {
logrus.Error("Failed to get Top Project SubGroups", gerr)
return variables, gerr
}
for _, v := range subGroups {
grpVariables, verr := getVariablesFrom(r, v.ID, "groups")
if verr != nil {
logrus.Error("Failed to get variables from subGroups ", verr)
return variables, verr
}
variables = append(variables, grpVariables...)
if includeProjects {
grpProjects, perr := r.GetGroupProjects(v.ID)
if perr != nil {
logrus.Error("Failed to get Projects for SubGroup ", perr)
return variables, perr
}
for _, p := range grpProjects {
projVariables, verr := getVariablesFrom(r, p.ID, "projects")
if verr != nil {
logrus.Error("Failed to get variables from topProjects ", verr)
return variables, verr
}
variables = append(variables, projVariables...)
}
}
}
return variables, nil
}
func (r *gitlabClient) UpdateVariableFrom(id int, resource string, variable string, value string) (string, error) {
escapedValue := strings.ReplaceAll(value, "\"", "\\\"")
uri := fmt.Sprintf("/%s/%d/variables/%s", resource, id, variable)
fetchUri := fmt.Sprintf("https://%s%s%s", r.BaseUrl, r.ApiPath, uri)
variableTemplate := `{
"value": "%s"
}`
body := fmt.Sprintf(variableTemplate, escapedValue)
resp, resperr := r.Client.R().
SetHeader("PRIVATE-TOKEN", r.Token).
SetHeader("Content-Type", "application/json").
SetBody(body).
Put(fetchUri)
if resperr != nil {
logrus.WithError(resperr).Error("Oops")
return "", resperr
}
return string(resp.Body()[:]), nil
}