This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.js
245 lines (213 loc) · 5.17 KB
/
hooks.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
const http = require("http");
const path = require("path");
const exec = require("child_process").exec;
const async = require("async");
const tmp = require("tmp");
const ncp = require("ncp").ncp;
const rmdir = require("rimraf");
const mkdirp = require("mkdirp");
const fetch = require("node-fetch");
const createGitHandler = require("github-webhook-handler");
const createTravisHandler = require("travisci-webhook-handler");
const config = require("./config.json");
let gitHandler, travisHandler;
setupHandlers()
.then(() => {
console.log(
"Handlers are set up, loading the http server on port " + config.PORT
);
http
.createServer((request, response) => {
async.each(
[gitHandler, travisHandler],
(handler, callback) => {
handler(request, response, callback);
},
err => {
if (err) {
console.log("Error: " + err);
res.statusCode = 404;
return res.end("-.-");
}
res.statusCode = 200;
res.end("Webhook triggered!");
}
);
})
.listen(config.PORT, "0.0.0.0", () => {
console.log("Server is running!");
});
})
.catch(err => {
console.log(err);
});
async function setupHandlers() {
const travisPublicKey = await fetch("https://api.travis-ci.org/config")
.then(res => {
return res.json();
})
.then(json => {
return json.config.notifications.webhook.public_key;
})
.catch(err => {
console.log(err);
});
console.log("travis-ci.org public key: " + travisPublicKey);
gitHandler = createGitHandler({
path: "/webhook/github",
secret: config.GITHUB_SECRET
});
travisHandler = createTravisHandler({
path: "/webhook/travis",
public_key: travisPublicKey
});
gitHandler.on("error", err => {
console.error("Error:", err.message);
});
travisHandler.on("error", err => {
console.error("Error:", err.message);
});
gitHandler.on("push", event => {
const { payload } = event;
console.log(
"Received a push event for %s to %s",
payload.repository.full_name,
payload.ref
);
console.log("Commits: " + payload.commits.length);
//Check whether it is an automated build
if (payload.commits.length === 1) {
console.log("Message: " + payload.commits[0].message);
if (
payload.commits[0].message === config.GITHUB_AUTOMATED_BUILD_MESSAGE
) {
if (payload.repository.full_name in config.PROJECTS.github) {
handleProject(
payload.repository.full_name,
payload.repository.name,
config.PROJECTS.github[payload.repository.full_name]
);
}
}
}
});
travisHandler.on("success", event => {
const { payload } = event,
full_name = payload.repository.owner_name + "/" + payload.repository.name;
console.log(
"Build %s success for %s branch %s",
payload.number,
full_name,
payload.branch
);
if (full_name in config.PROJECTS.travis) {
handleProject(
full_name,
payload.repository.name,
config.PROJECTS.travis[full_name]
);
}
});
}
function handleProject(projectName, shortName, project) {
let tmpDir = tmp.dirSync();
console.log(
"Cloning " +
"git@github.com:" +
projectName +
".git (" +
project.branch +
") to " +
tmpDir.name
);
//no need to escape, variables are safe, no user input
exec(
"cd " +
tmpDir.name +
" && git clone -b " +
project.branch +
" --single-branch https://github.com/" +
projectName +
".git",
(err, stdout, stderr) => {
if (err) {
cleanUp([tmpDir]);
return console.log(err);
}
console.log(stdout);
console.log(stderr);
//Delete files
exec(
"find " +
project.path +
" -maxdepth 1 -type f " +
project.persistent
.map(name => {
return "-not -name '" + name + "' ";
})
.join(" ") +
" -delete",
(err, stdout, stderr) => {
if (err) {
cleanUp([tmpDir]);
return console.log(err);
}
console.log(stdout, stderr);
if (project.type === "git-folder") {
let folder = path.dirname(
path.resolve(project.path, project.repo_path)
);
console.log(
"Copying " +
path.resolve(tmpDir.name, shortName, project.repo_path) +
" to " +
path.resolve(project.path, project.repo_path)
);
ncp(
path.resolve(tmpDir.name, shortName, project.repo_path),
path.resolve(project.path),
err => {
if (err) {
cleanUp([tmpDir]);
return console.log(err);
}
cleanUp([tmpDir]);
}
);
} else if (project.type === "docker-compose") {
async.each(
[...project.compose_files, ...project.other_files],
(name, callback) => {
console.log("Copying " + name + " to " + project.path);
ncp(
path.resolve(tmpDir.name, shortName, name),
path.resolve(project.path, name),
callback
);
},
err => {
if (err) {
cleanUp([tmpDir]);
return console.log(err);
}
cleanUp([tmpDir]);
//we're done here, docker is run by root now
}
);
}
}
);
}
);
}
function cleanUp(tmpFolders) {
async.each(
tmpFolders,
(tmpFolder, callback) => {
rmdir(tmpFolder.name, callback);
},
err => {
console.log(err);
}
);
}