forked from jansepar/node-jenkins-api
/
main.js
229 lines (200 loc) · 7.55 KB
/
main.js
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
var util = require('util')
,qs = require('querystring')
,request = require('request');
API = '/api/json';
NEWJOB = '%s/createItem/?name=%s';
DELETE = '%s/job/%s/doDelete';
BUILD = '%s/job/%s/build';
BUILDWITHPARAMS = '%s/job/%s/buildWithParameters';
CONFIG = '%s/job/%s/config.xml';
JOBINFO = '%s/job/%s' + API;
LIST = '%s' + API;
LAST_SUCCESS = '%s/job/%s/lastSuccessfulBuild' + API;
TEST_REPORT = '%s/job/%s/lastSuccessfulBuild/testReport' + API;
LAST_BUILD = '%s/job/%s/lastBuild' + API;
LAST_REPORT = '%s/job/%s/lastBuild/testReport' + API;
var init = exports.init = function(host) {
//Jenkin variables
var host = host;
//Helper Functions
var build_url = function(command) {
/*
Builds REST url to Jenkins
*/
// Create the url using the format function
var url = util.format.call(this, command, host);
// if command is the only arg, we are done
if (arguments.length == 1) return url;
// Grab the arguments except for the first (command)
var args = Array.prototype.slice.call(arguments).slice(1);
// Append url to front of args array
args.unshift(url);
// Create full url with all the arguments
url = util.format.apply(this, args);
return url;
}
return {
build: function(jobname, callback, params) {
/*
Trigger Jenkins to build.
*/
if (!params) {
request({method: 'POST', url: build_url(BUILD, jobname)}, function(error, response, body) {
});
}else {
request({method: 'POST', url: build_url(BUILDWITHPARAMS+"?"+qs.stringify(params), jobname)}, function(error, response, body) {
});
}
},
all_jobs: function(callback) {
/*
Return a list of object literals containing the name and color of all jobs on the Jenkins server
*/
request({method: 'GET', url: build_url(LIST)}, function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = JSON.parse(body.toString()).jobs;
callback(null, data);
});
},
job_info: function(jobname, callback) {
/*
Get all information for a job
*/
request({method: 'GET', url: build_url(JOBINFO, jobname)}, function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = JSON.parse(body.toString());
callback(null, data);
});
},
last_build_info: function(jobname, callback) {
/*
Get information for the last build of a job
*/
request({method: 'GET', url: build_url(LAST_BUILD, jobname)}, function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = JSON.parse(body.toString());
callback(null, data);
});
},
last_build_report: function(jobname, callback) {
/*
Get the last build report for a job
*/
request({method: 'GET', url: build_url(LAST_REPORT, jobname)}, function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = JSON.parse(body.toString());
callback(null, data);
});
},
get_config_xml: function(jobname, callback) {
/*
Get the config xml for a job
*/
request({method: 'GET', url: build_url(CONFIG, jobname)}, function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = body;
callback(null, data);
});
},
create_job: function(jobname, job_config, callback) {
/*
Create a new job based on a job_config string
*/
console.log(build_url(NEWJOB, jobname));
request(
{method: 'POST'
,url: build_url(NEWJOB, jobname)
,body: job_config
,headers: { "content-type": "application/xml"}
},
function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = body;
callback(null, data);
}
);
},
copy_job: function(jobname, new_job, modifyfunction, callback) {
/*
Copies a job and allows you to pass in a function to modify the configuration
of the job you would like to copy
*/
var self = this;
self.get_config_xml(jobname, function(error, data) {
if (error) {
callback(error, data);
return;
}
self.create_job(new_job, modifyfunction(data), function(error, data) {
if (error) {
callback(error, data);
return;
}
callback(null, data);
});
});
},
delete_job: function(jobname, callback) {
/*
Deletes a job
*/
request({method: 'POST', url: build_url(DELETE, jobname)}, function(error, response, body) {
if ( error || response.statusCode === 404 ) {
callback(error || true, response);
return;
}
callback(null, body);
});
},
last_success: function(jobname, callback) {
/*
Get the last build report for a job
*/
request({method: 'POST', url: build_url(LAST_SUCCESS, jobname)}, function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = JSON.parse(body);
callback(null, data);
});
},
last_result: function(jobname, callback) {
/*
Get the last result for a job
*/
self = this;
self.job_info(jobname, function(error, data) {
last_result_url = data['lastBuild']['url'];
request({method: 'GET', url: build_url(last_result_url + API, jobname)}, function(error, response, body) {
if ( error || response.statusCode !== 200 ) {
callback(error || true, response);
return;
}
data = JSON.parse(body);
callback(null, data);
});
});
},
}
}
if (!module.parent) {
}