-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.js
75 lines (66 loc) · 2.41 KB
/
webhook.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
const http = require('http');
const { spawn } = require('child_process') // 子进程, 用来执行脚本
http.createServer((req, res) => {
console.log(`--- ${req.method} --- ${req.url} ---`);
// console.log(`--- headers : ${JSON.stringify(req.headers)} ---`)
// console.log(`--- ${JSON.stringify(req.trailers)} ---`)
res.setHeader("Content-Type", "application/json");
// request
if (req.method === 'POST') {
// 获取body
let body = '';
req.on('data', (data) => {
// console.log(`--- data: ${data} ---`);
body += data;
});
req.on('end', () => {
let payload = JSON.parse(body);
console.log(`--- ${payload.repository.url} ---`);
//* linux和macOS
if (process.platform === 'linux' || process.platform === 'darwin') {
const sh = spawn('sh', [`./sh/${payload.repository.name}.sh`]);
sh.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
sh.stdout.on('end', () => {
console.log('Mission Complete!')
});
sh.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
sh.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}
//* windows系统
if (process.platform === 'win32') {
const bat = spawn('cmd.exe', ['/c', `bat\\${payload.repository.name}.cmd`]);
bat.stdout.on('data', (data) => {
console.log(data.toString());
});
bat.stdout.on('end', () => {
console.log('Mission Complete!')
});
bat.stderr.on('data', (data) => {
console.error(data.toString());
});
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
}
// response
let json = JSON.stringify({
status: "success",
code: 200
});
res.end(json);
});
}
else {
let json = JSON.stringify({
status: "OK",
code: 200
});
res.end(json);
}
}).listen(8008)