-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathindex.js
366 lines (335 loc) · 12.1 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
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
/*
* routes/index.js
*/
var BASE_PATH = "../lib/"
var _ = require('underscore')
, Step = require('step')
, common = require(BASE_PATH + 'common')
, config = require(BASE_PATH + 'config')
, fs = require('fs')
, gh = require(BASE_PATH + 'github')
, jobs = require(BASE_PATH + 'jobs')
, logging = require(BASE_PATH + 'logging')
, User = require(BASE_PATH + 'models').User
, Job = require(BASE_PATH + 'models').Job
, pjson = require('../package.json')
;
var TEST_ONLY = "TEST_ONLY";
var TEST_AND_DEPLOY = "TEST_AND_DEPLOY";
/*
* GET home page.
*/
exports.index = function(req, res){
if (req.loggedIn == false){
}
if (req.session.return_to != null) {
var return_to = req.session.return_to;
req.session.return_to=null;
res.redirect(return_to);
} else {
var code = "";
if (req.param('code') !== undefined) {
code = req.param('code');
res.render('register.html', {invite_code:code});
} else {
if (req.user != undefined) {
req.user.get_repo_config_list(function(err, repo_list) {
if (err) throw err;
res.render('index.html',{total_configured_projects:repo_list.length});
});
} else {
res.render('index.html');
}
}
}
};
function whitelist_repo_config(repo_config) {
var trepo = {
display_name:repo_config.display_url.replace(/^.*com\//gi, ''),
display_url:repo_config.display_url,
url:repo_config.url,
project_type:repo_config.project_type,
webhooks:repo_config.webhooks,
prod_deploy_target:repo_config.deploy_target
};
return trepo;
}
function whitelist_repo_metadata(repo_metadata) {
var trepo = {
display_name:repo_metadata.html_url.replace(/^.*com\//gi, ''),
url:repo_metadata.html_url,
id:repo_metadata.id
};
return trepo;
}
/*
* GET /kickoff - start configuration wizard for a job
*/
exports.kickoff = function(req, res, github) {
var gh = github || gh;
// Assert cached github metadata
if (req.user.github_metadata === undefined
|| req.user.github_metadata[req.user.github.id] === undefined) {
res.statusCode = 400;
res.end("please call /api/github/metadata before this");
} else {
// Find the metadata for the repo we are kicking off on
var kickoff_repo_metadata = req.user.get_repo_metadata(req.params.githubId);
var trepo = whitelist_repo_metadata(kickoff_repo_metadata);
// Check whether someone else has already configured this repository
User.findOne({'github_config.url':trepo.url.toLowerCase()}, function(err, user) {
if (!user) {
res.render('kickoff.html', {repo: JSON.stringify(trepo)})
} else {
res.render('kickoff-conflict.html', {repo: JSON.stringify(trepo)});
}
});
}
};
/*
* GET /account - account settings page
*/
exports.account = function(req, res){
res.render('account.html');
};
/*
* GET /:org/:repo/config - project config page
*/
exports.config = function(req, res)
{
Step(
function() {
req.user.get_repo_config(req.repo_url, this);
},
function(err, repo_config) {
if (err) {
console.error("config() - Error fetching repo config for user %s: %s", req.user.email, err);
res.statusCode = 400;
return res.end("Bad Request");
}
this.repo_config = repo_config;
req.user.get_prod_deploy_target(repo_config.url, this);
},
function(err, deploy_target) {
var wrepo_config = whitelist_repo_config(this.repo_config);
var deploy_on_green = this.repo_config.prod_deploy_target.deploy_on_green;
// Default to true if not set
if (deploy_on_green === undefined) {
deploy_on_green = true;
}
var deploy_target_name = null;
if (deploy_target) {
deploy_target_name = deploy_target.app;
}
var params = {
repo_url: this.repo_config.url,
has_deploy_target: deploy_target != null,
deploy_target_name: deploy_target_name,
deploy_on_green: deploy_on_green
};
var apresParams = JSON.stringify(params);
var projectPanels = common.panels['project_config'];
var r = {
// May be undefined if not configured
display_name: wrepo_config.display_name,
badge_url: config.strider_server_name + '/' + req.user.id + '/' + req.params.org + '/' + req.params.repo + '/badge',
repo_org: req.params.org,
repo_name: req.params.repo,
apresController: "/javascripts/apres/controller/project_config.js",
apresParams: apresParams,
panels: [],
};
// TODO: factor out this logic so other resource handlers can use it later
if (projectPanels) {
// For each panel, read contents from FS or execute callback to get HTML
Step(
function() {
var group = this.group();
projectPanels.forEach(function(p) {
var cb = group();
var f = function(err, res) {
p.contents = res;
cb(err, p);
};
// Panel sources can be a string which assumed to be a filesystem path
// or a function which is assumed to take a callback argument.
if (typeof(p.src) === 'string') {
fs.readFile(p.src, 'utf8', f);
} else if (typeof(p.src) === 'function') {
p.src(f);
}
});
},
function(err, panels) {
if (err) {
console.error("Error loading panels: %s", err);
res.statusCode = 500;
return res.end("Error handling request");
}
r.panels = panels;
return res.render('project_config.html', r);
}
);
} else {
return res.render('project_config.html', r);
}
}
);
};
/*
* POST /webhook - Github push webhook handler
*/
exports.webhook_signature = function(req, res)
{
gh.verify_webhook_req_signature(req, function(isOk, repo, user, payload) {
var active = false;
// Repo can be undefined
if (isOk && repo) {
active = repo.active;
}
// Default to active if property is missing.
if (active === undefined) {
active = true;
}
if (active && isOk && gh.webhook_commit_is_to_master(payload)) {
console.log("received a correctly signed webhook for repo %s on master branch - starting task on user %s's behalf", repo.url, user.email);
var github_commit_id = payload.after;
var github_commit_info = gh.webhook_extract_latest_commit_info(payload);
var repo_ssh_url;
var repo_metadata;
if (user.github.id) {
repo_metadata = _.find(user.github_metadata[user.github.id].repos, function(item) {
return repo.url == item.html_url.toLowerCase();
});
}
// If we have Github metadata, use that. It is loosely coupled and can self-heal things like
// a configured Github Repo being renamed in Github (such as happened with Klingsbo)
// We do not have metadata in the manual setup case
if (repo_metadata) {
repo_ssh_url = repo_metadata.ssh_url;
} else {
// Manual setup case - try to synthesize a Github SSH url from the display URL.
// This is brittle because display urls can change, and the user (currently) has
// no way to change them (other than deleting and re-adding project).
var p = gh.parse_github_url(repo.display_url);
repo_ssh_url = gh.make_ssh_url(p.org, p.repo);
}
console.debug("POST to Github /webhook payload: %j", payload);
if (repo.has_prod_deploy_target) {
var deploy_config = _.find(user[repo.prod_deploy_target.provider], function(item) {
return item.account_id === repo.prod_deploy_target.account_id;
});
jobs.startJob(user, repo, deploy_config, github_commit_info, repo_ssh_url, TEST_AND_DEPLOY);
} else {
jobs.startJob(user, repo, deploy_config, github_commit_info, repo_ssh_url, TEST_ONLY);
}
res.end("webhook good");
} else {
console.log("received an incorrecly signed webhook or is not to master branch.");
res.end("webhook bad or irrelevant");
}
});
};
exports.webhook_secret = function(req, res)
{
gh.verify_webhook_req_secret(req, function(isOk, repo, user, payload) {
var active = repo.active;
// Default to active if property is missing.
if (active === undefined)
active = true;
if (active && isOk && gh.webhook_commit_is_to_master(payload)) {
console.log("received a correctly signed webhook for repo %s on master branch - starting task on user %s's behalf", repo.url, user.email);
var github_commit_id = payload.after;
var github_commit_info = gh.webhook_extract_latest_commit_info(payload);
// We don't have github metadata unless we have a linked github account.
var repo_metadata;
var repo_ssh_url;
if (user.github.id) {
repo_metadata = _.find(user.github_metadata[user.github.id].repos, function(item) {
return repo.url == item.html_url.toLowerCase();
});
}
// If we have Github metadata, use that. It is loosely coupled and can self-heal things like
// a configured Github Repo being renamed in Github (such as happened with Klingsbo)
// We do not have metadata in the manual setup case
if (repo_metadata) {
repo_ssh_url = repo_metadata.ssh_url;
} else {
// Manual setup case - try to synthesize a Github SSH url from the display URL.
// This is brittle because display urls can change, and the user (currently) has
// no way to change them (other than deleting and re-adding project).
var p = gh.parse_github_url(repo.display_url);
repo_ssh_url = gh.make_ssh_url(p.org, p.repo);
}
console.debug("POST to Github /webhook payload: %j", payload);
if (repo.has_prod_deploy_target) {
var deploy_config = _.find(user[repo.prod_deploy_target.provider], function(item) {
return item.account_id === repo.prod_deploy_target.account_id;
});
jobs.startJob(user, repo, deploy_config, github_commit_info, repo_ssh_url, TEST_AND_DEPLOY);
} else {
jobs.startJob(user, repo, deploy_config, github_commit_info, repo_ssh_url, TEST_ONLY);
}
res.end("webhook good");
} else {
console.log("gh: " + gh.webhook_commit_is_to_master(payload));
console.log("received an incorrecly signed webhook or is not to master branch.");
res.end("webhook bad or irrelevant");
}
});
};
/*
* app.get('/:org/:repo/delete', middleware.require_resource_admin, routes.delete_project);
* delete project. should be rewritten as backbone and api call
* todo: should delete github webhook and deploy key
* todo: should add 'archived' flag to jobs
*/
exports.delete_project = function(req,res) {
var repo_url = "https://github.com/" + req.params.org + "/" + req.params.repo;
repo_url = repo_url.toLowerCase();
var conditions = { email: req.user.email }, update = {$pull : {github_config:{url:repo_url}}};
User.update(conditions, update, function(err,numAffected) {
if (err) throw err;
console.log("deleted: " + repo_url);
res.redirect("/");
});
}
/*
* /status endpoint
* Executes a simple database query to verify that system is operational.
* Assumes there is at least 1 user in the system.
* Returns 200 on success.
*
* This is for use by Pingdom and similar monitoring systems.
*/
exports.status = function(req, res) {
function error(message) {
res.statusCode = 500;
var resp = {
status: "error",
version: "StriderCD (http://stridercd.com) " + pjson.version,
results: [],
errors: [{message:message}]
}
return res.end(JSON.stringify(resp, null, "\t"));
}
function ok() {
res.statusCode = 200;
var resp = {
status: "ok",
version: "StriderCD (http://stridercd.com) " + pjson.version,
results: [{message:"system operational"}],
errors: []
}
return res.end(JSON.stringify(resp, null, "\t"));
}
User.findOne(function(err, user) {
if (err) {
return error("error retrieving user from DB: " + err);
}
if (!user) {
return error("no users found in DB - mis-configured?")
}
return ok();
});
};