-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkins.cfc
261 lines (176 loc) · 5.15 KB
/
Jenkins.cfc
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
component {
config = "";
function init(username, token, serverURL, projectName) {
variables.username = username;
variables.token = token;
variables.serverURL = reReplaceNoCase(serverURL, "(https?://)", "\1#username#:#token#@");
if (NOT isNull(projectName)) {
setProjectName(projectName);
}
return this;
}
function setProjectName(projectName) {
variables.projectName = projectName;
return this;
}
function getProjectName() {
return variables.projectName;
}
function setConfig(config) {
variables.config = isXML(config) ? toString(config) : config;
return this;
}
function getConfig() {
return config;
}
function loadConfig() {
http url="#getURL('config.xml')#";
setConfig(cfhttp.fileContent);
return cfhttp.status_code EQ 200;
}
function saveConfig() {
http url="#getURL('config.xml')#" method="post" {
httpparam type="body" value="#getConfig()#";
}
return cfhttp.status_code EQ 200;
}
function getURL(path) {
return "#variables.serverURL#/job/#variables.projectName#/#path#";
}
function build() {
http url="#getURL('build')#";
return isBuildSuccessful(cfhttp.status_code);
}
function buildWithParameters(params = []) {
http url="#getURL('buildWithParameters')#" {
for (param in params) {
key = param.keyArray()[1];
value = param[key];
httpparam type="url" name="#key#" value="#value#";
}
}
return isBuildSuccessful(cfhttp.status_code);
}
function isBuildSuccessful(statusCode) {
// NOTE: Since 1.519 201 is returned instead of 302 for a successfully created job
return [201, 302].find(statusCode) GT 0;
}
function getParameter(name, scope = "one") {
parameters = getParameters();
params = [];
for (param in parameters) {
// We check all because there might be more than one param with the same name
if (param.name EQ name) {
if (scope EQ "one") {
return param;
} else {
params.append(param);
}
}
}
return params.len() EQ 1 ? params[1] : params;
}
function getParameters(type) {
params = [];
for (node in getParameterNodes().xmlChildren) {
nodeType = getMappedParameterType(node) ?: "";
if (isNull(type) OR (NOT isNull(type) AND type EQ nodeType)) {
param = {
"name" = node.xmlChildren[1].xmlText,
"description" = node.XmlChildren[2].xmlText,
"type" = nodeType
};
if ([
"string",
"boolean",
"text",
"password"
].findNoCase(param.type)) {
param["defaultValue"] = node.XmlChildren[3].xmlText;
} else if (["hudson.model.ChoiceParameterDefinition"].findNoCase(node.xmlName)) {
param["choices"] = [];
for (choice in node.xmlChildren[3].xmlChildren[1].xmlChildren) {
param.choices.append(choice.xmlText);
}
}
params.append(param);
}
}
return params;
}
function getParameterNodes(config = xmlParse(getConfig())) {
for (node in config.project.XmlChildren) {
if (node.XmlName EQ "properties") {
for (node in node.XmlChildren) {
if (node.XmlName EQ "hudson.model.ParametersDefinitionProperty") {
for (node in node.XmlChildren) {
if (node.XmlName EQ "parameterDefinitions") {
return node;
}
}
return null;
}
}
}
}
}
function updateParameter(name, any value, scope = "one") {
// Update basic parameters
config = config = xmlParse(variables.config);
nodes = xmlSearch(
getParameterNodes(config),
"//parameterDefinitions"
)[1].xmlChildren;
for (node in nodes) {
if (getParameterName(node) EQ name) {
if (["string", "boolean", "text", "password"].find(getMappedParameterType(node))) {
node.xmlChildren[3].xmlText = value;
if (scope EQ "one") {
break;
}
} else if (getMappedParameterType(node) EQ "choice") {
choiceElements = node.xmlChildren[3].xmlChildren[1].xmlChildren;
// FOR NOW: Three choices, three changes
// TODO: Need to remove all choices and add new ones
// MAYBE: xmlElemNew() is the way to do it
choiceElements.each(function(choiceElement, i) {
choiceElement.xmlText = value[i];
});
}
}
}
setConfig(config);
}
function getMappedParameterType(any parameter) {
paramTypeMap = {
"hudson.model.StringParameterDefinition" = "string",
"hudson.model.BooleanParameterDefinition" = "boolean",
"hudson.model.TextParameterDefinition" = "text",
"hudson.model.ChoiceParameterDefinition" = "choice",
"hudson.model.PasswordParameterDefinition" = "password"
};
if (isXMLElem(parameter)) {
return paramTypeMap[parameter.xmlName] ?: "";
} else {
return paramTypeMap[parameter] ?: "";
}
}
function getParameterName(node) {
return node.xmlChildren[1].xmlText;
}
function updateDescription(description) {
config = xmlParse(getConfig());
config.project.description.xmlText = description;
setConfig(config);
return this;
}
function getDescription() {
return xmlParse(config).project.description.xmlText;
}
function saveDescription(description = getDescription()) {
http url="#getURL('submitDescription')#" method="post" {
httpparam type="formfield" name="description" value="#description#";
}
return cfhttp.status_code EQ "200";
}
}