-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
73 lines (61 loc) · 1.93 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
const request = require('request');
exports = module.exports = function (options) {
return function CodingHookDingDing (req, res, next) {
let event = req.headers['x-coding-event'],
body = req.body;
switch (event){
case "push":
return Push(req, res, next);
case "ping":
return res.json({
"token": "123",
"zen": "Coding! 让开发更简单"
});
default:
return res.json(body);
}
};
};
function Push(req, res, next) {
let body = req.body;
let ding = body.token;
let count = body.commits.length;
let whopush = body.commits[0].committer.name;
let repourl = `${body.repository.owner.web_url}/p/${body.repository.name}/git/tree/${getbranch(body.ref)}`;
let branch = `# [[${body.repository.name}/${getbranch(body.ref)}]](${repourl})`;
let text = `${branch} ${count} commit,push by ${whopush}\n ## commits:\n`;
for (let i = 0; i < count; i++){
let com = body.commits[i];
let name = com.committer.name;
let mes = com.short_message;
let hash6 = com.sha.substring(0,6);
let commit = `> </> ${name}: [[${hash6}]](${com.web_url})-${mes}\n`;
text = text + commit;
}
console.log(text);
let rqbody = {
msgtype: "markdown",
markdown: {
title:"仓库有新的代码更新咯!",
text: text
}
};
let options = {
url: ding,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(rqbody)
};
request.post(options, (err, res, body)=>{
if(!err && res.statusCode == 200){
let info = JSON.parse(body);
console.log(info);
}
});
res.json({mes: 'ok'});
}
function getbranch(str) {
let arr = str.split('/');
return arr[arr.length-1];
}