-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
109 lines (90 loc) · 3.45 KB
/
agent.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
(function() {
'use strict';
var crypto = require('crypto'),
path = require('path'),
fs = require('fs-extra'),
workerFarm = require('worker-farm'),
websiteReleaseWorker = workerFarm(require.resolve('./features/website-release.js')),
githubControllerWorker = workerFarm(require.resolve('./features/github-controller.js')),
Server = require('./features/server.js'),
buildStatus = require('./features/build-status/build-status.js'),
SECRET = fs.readFileSync('SECRET', 'utf8').trim(),
SERVER_PORT = 8124,
USER_AGENT = 'MemoryOverflowAgent',
USER_AGENT_EMAIL = '[email protected]',
MEMORYOVERFLOW_OWNER = 'CodeCorico',
MEMORYOVERFLOW_PROJECT = 'MemoryOverflow',
MEMORYOVERFLOW_REPO = 'https://github.com/' + MEMORYOVERFLOW_OWNER + '/' + MEMORYOVERFLOW_PROJECT + '.git',
MEMORYOVERFLOW_PATH = 'memoryoverflow',
WEBSITE_REPO = 'https://github.com/CodeCorico/MemoryOverflow-website.git',
WEBSITE_PATH = 'website',
THEMACHINE_PATH = 'the-machine',
COMMIT_LABEL = 'release: master-{commitID}\n\nMemoryOverflow commit origin: {commitUrl}';
buildStatus.status('ok');
new Server(SERVER_PORT, function(request, response, body) {
if (request.method == 'GET' && request.url == '/status.svg') {
var filePath = path.join(__dirname, 'status.svg');
var stat = fs.statSync(filePath);
response.response.writeHead(200, {
'Content-Type': 'image/svg+xml',
'Content-Length': stat.size
});
var readStream = fs.createReadStream(filePath);
readStream.pipe(response.response);
return;
}
if(request.method != 'POST') {
return response.forbidden();
}
var event = request.headers['x-github-event'] || null,
signature = request.headers['x-hub-signature'] || null,
hash = crypto.createHmac('sha1', SECRET).update(body).digest('hex'),
post = JSON.parse(body);
signature = signature ? signature.replace('sha1=', '') : signature;
if(signature != hash) {
return response.forbidden();
}
if(event == 'push') {
var commitID = post.after || null,
commitUrl = post.commits && post.commits.length ? post.commits[0].url : null;
if(!commitID || !commitUrl) {
return response.forbidden();
}
buildStatus.status('processing');
websiteReleaseWorker({
USER_AGENT: USER_AGENT,
USER_AGENT_EMAIL: USER_AGENT_EMAIL,
SECRET: SECRET,
MEMORYOVERFLOW_REPO: MEMORYOVERFLOW_REPO,
MEMORYOVERFLOW_PATH: MEMORYOVERFLOW_PATH,
THEMACHINE_PATH: THEMACHINE_PATH,
WEBSITE_REPO: WEBSITE_REPO,
WEBSITE_PATH: WEBSITE_PATH,
COMMIT_LABEL: COMMIT_LABEL,
commitID: commitID,
commitUrl: commitUrl
}, function(success) {
buildStatus.status(success ? 'ok' : 'error');
});
}
else if(
(event == 'issues' && post.action == 'opened') ||
(event == 'issue_comment' && post.action == 'created') ||
(event == 'pull_request' && post.action == 'opened')
) {
githubControllerWorker({
USER_AGENT: USER_AGENT,
USER_AGENT_EMAIL: USER_AGENT_EMAIL,
SECRET: SECRET,
MEMORYOVERFLOW_OWNER: MEMORYOVERFLOW_OWNER,
MEMORYOVERFLOW_PROJECT: MEMORYOVERFLOW_PROJECT,
event: event,
post: post
}, function() {});
}
else {
return response.forbidden();
}
response.ok();
});
})();