-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
224 lines (187 loc) · 8.29 KB
/
index.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
(function(){
'use strict';
// ────────────────────────────────────────────────────────────────────────────────
// MODULES
var through = require('through2');
var assign = require('object-assign');
var jenkinsAPI = require('jenkins-api');
var gutil = require('gulp-util');
// ────────────────────────────────────────────────────────────────────────────────
var PluginError = gutil.PluginError;
var AUTH;
var PLUGIN_NAME = 'gulp-jenkins';
// ────────────────────────────────────────────────────────────────────────────────
// Setup gulp access level
var gulpJenkins = function() {
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}
return cb(null, file);
});
};
/**
* Prepares a request to the jenkins service using a config object
*
* @param {any} c - the auth configurations object
* @returns
*/
function jenkinsService(c) {
// Check if an authorization config object is provided
if(typeof c !== 'undefined') {
if(typeof c.username !== 'undefined' && typeof c.password !== 'undefined') {
return jenkinsAPI.init('http://'+c.username+':'+c.password+'@'+c.url, c);
} else { return new PluginError(PLUGIN_NAME, 'the provided credentials are missing a username and password'); }
} else { return jenkinsAPI.init('http://'+c.url, c); }
}
/**
* Default callback handler
*
* @param {any} err - the error object
* @param {any} data - the data is successful
*/
function callback(err, data) {
if(err) { return new PluginError(PLUGIN_NAME, 'failed to build with params :: ' + err.message); }
gutil.log('Jenkins: '+gutil.colors.green(data.message));
gutil.log('Location: \''+gutil.colors.cyan(data.location)+'\'');
}
/** Checks for an authorized session */
function hasInitialized() {
if(typeof AUTH === 'undefined') {
return false;
} else {
return true;
}
}
gulpJenkins.init = function(creds) {
if(typeof creds.url === 'undefined') {
return new PluginError(PLUGIN_NAME, 'the jenkins url was not provided');
}
AUTH = creds;
};
gulpJenkins.all_jobs = function(cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been intialized'); }
callback = cb || callback;
jenkinsService(AUTH).all_jobs(callback);
return gulpJenkins();
};
gulpJenkins.job_info = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).job_info(job_name, callback);
return gulpJenkins();
};
gulpJenkins.enable_job = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).enable_job(job_name, callback);
return gulpJenkins();
};
gulpJenkins.disable_job = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).disable_job(job_name, callback);
return gulpJenkins();
};
gulpJenkins.build = function(job_name, params, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
if(typeof params !== 'undefined') {
// Building with parameters
var opts = assign({}, params);
if(!job_name) { return new PluginError(PLUGIN_NAME, 'job_name must be provided for publishing builds'); }
if(!opts.tag_name) { return new PluginError(PLUGIN_NAME, 'tag_name must be provided for publishing builds'); }
if(!opts.target_env) { return new PluginError(PLUGIN_NAME, 'target_env must be provided for publishing builds'); }
gutil.log('Building \''+gutil.colors.cyan(job_name+'@'+AUTH.url+'\' with params ...'));
jenkinsService(AUTH)
.build(job_name, {
target_env: opts.target_env,
tag_name: opts.tag_name
}, callback);
return gulpJenkins();
} else {
// Build with no parameters
gutil.log('Building \''+gutil.colors.cyan(job_name+'@'+AUTH.url+'\' ...'));
jenkinsService.build(job_name, callback);
return gulpJenkins();
}
};
gulpJenkins.stop_build = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).stop_build(job_name, callback);
return gulpJenkins();
};
gulpJenkins.build_info = function(job_name, build_number, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).build_info(job_name, build_number, callback);
return gulpJenkins();
};
gulpJenkins.last_build_info = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).last_build_info(job_name, callback);
return gulpJenkins();
};
gulpJenkins.last_build_report = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).last_build_report(job_name, callback);
return gulpJenkins();
};
gulpJenkins.get_config_xml = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).get_config_xml(job_name, callback);
return gulpJenkins();
};
gulpJenkins.copy_job = function(job_to_copy, new_job_title, config_fn, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).copy_job(job_to_copy, new_job_title, config_fn, callback);
return gulpJenkins();
};
gulpJenkins.delete_job = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).delete_job(job_name, callback);
return gulpJenkins();
};
gulpJenkins.last_success = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).last_success(job_name, callback);
return gulpJenkins();
};
gulpJenkins.last_result = function(job_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).last_result(job_name, callback);
return gulpJenkins();
};
gulpJenkins.job_output = function(job_name, build_name, cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).job_output(job_name, callback);
return gulpJenkins();
};
gulpJenkins.queue = function(cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).queue(callback);
return gulpJenkins();
};
gulpJenkins.computers = function(cb) {
if(!hasInitialized()) { return new PluginError(PLUGIN_NAME, 'gulp-jenkins has not been initialized'); }
callback = cb || callback;
jenkinsService(AUTH).computers(callback);
return gulpJenkins();
};
module.exports = gulpJenkins;
})();