-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgithubHook.js
81 lines (71 loc) · 3.16 KB
/
githubHook.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
var fs = require('fs'),
colors = require('colors'),
utils = require('./utils/general'),
contributors = require('./utils/contributors'),
shaValidator = require('./utils/shaValidator'),
NUPIC_STATUS_PREFIX = 'NuPIC Status:',
VALIDATOR_DIR = './validators',
dynamicValidatorModules = [],
repoClients;
function handlePullRequest(payload, repoClient) {
var action = payload.action,
githubUser = payload.pull_request.user.login,
head = payload.pull_request.head,
base = payload.pull_request.base;
console.log('Received pull request "' + action + '" from ' + githubUser);
if (action == 'closed') {
// if this pull request just got merged, we need to re-validate the
// fast-forward status of all the other open pull requests
if (payload.pull_request.merged) {
console.log('A PR just merged. Re-validating open pull requests...');
contributors.getAll(repoClient.contributorsUrl, function(err, contributors) {
shaValidator.revalidateAllOpenPullRequests(contributors, repoClient, dynamicValidatorModules);
});
}
} else {
// Only process PRs against the master branch.
if (payload.pull_request.base.ref == 'master') {
shaValidator.performCompleteValidation(head.sha, githubUser, repoClient, dynamicValidatorModules, true);
} else {
console.log(('Ignoring pull request against ' + payload.pull_request.base.label).yellow);
}
}
}
function handleStateChange(payload, repoClient) {
console.log('State of ' + payload.sha + ' updated to ' + payload.state);
// Ignore state changes on closed pull requests
if (payload.pull_request && payload.pull_request.state == 'closed') {
console.log(('Ignoring status of closed pull request (' + payload.sha + ')').yellow);
return;
}
// Get statuses and check the latest one
repoClient.getAllStatusesFor(payload.sha, function(err, statusHistory) {
var latestStatus = statusHistory[0];
if (latestStatus && latestStatus.description.indexOf(NUPIC_STATUS_PREFIX) == 0) {
// ignore statuses that were created by this server
console.log(('Ignoring "' + payload.state + '" status created by nupic.tools.').yellow);
} else {
shaValidator.performCompleteValidation(payload.sha, payload.sender.login, repoClient, dynamicValidatorModules, true);
}
});
}
module.exports = function(clients) {
repoClients = clients;
dynamicValidatorModules = utils.initializeModulesWithin(VALIDATOR_DIR);
return function(req, res) {
var payload = JSON.parse(req.body.payload),
repoName = payload.name || payload.repository.full_name,
repoClient = repoClients[repoName];
if (! repoClient) {
console.log(('No repository client available for ' + repoName).red);
return res.end();
}
console.log(repoClient.toString().magenta);
if (payload.state) {
handleStateChange(payload, repoClient);
} else {
handlePullRequest(payload, repoClient);
}
res.end();
};
};