-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
162 lines (154 loc) · 5.45 KB
/
server.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
const config = require("./config.json");
const http = require('http');
const request = require('request');
var EventEmitter = require("events").EventEmitter;
var data = new EventEmitter();
var apiUrl = "https://api.github.com";
// Default request options
var options = {
method: "GET",
headers: {
'Content-type': 'application/json',
'User-Agent': 'releaser-bot',
'Authorization': 'token '+config.token
}, json: true
};
// Server for webhooks
http.createServer(function (req, res) {
if (req.method == 'POST') {
var b = '';
req.on('data', function (d) {
b += d;
});
req.on('end', function () {
try {
var post = JSON.parse(b);
if (post.ref == "refs/heads/master") {
console.log("webhook detected for "+post.repository.full_name);
checkRepos(post.repository.full_name);
} else {
console.log("webhook detected for "+post.repository.full_name+" but is not master branch");
}
res.writeHead(200, {"Content-Type": "application/json"});
res.write('{"message": "Webhook received!"}');
res.end();
} catch (err) {
console.log(err);
res.writeHead(500, {"Content-Type": "application/json"});
res.write('{"message": "Bad Post Data."}');
res.end();
}
});
} else {
res.end('not found');
}
}).listen(9898, "0.0.0.0");
// Check repos
function checkRepos(name) {
for (var i=0; i < config.repositories.length; i++) {
if (name == config.repositories[i]) {
options.url = apiUrl+"/repos/"+config.repositories[i];
// Charge repos
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
data.json = body;
data.emit('commit');
} else {
console.log("Reposiroty does not exist or unauthorized access");
}
});
}
}
}
// Check last commit
data.on('commit', function () {
options.url = apiUrl+"/repos/"+data.json.full_name+"/commits";
// Charge last commit
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var regMessage = new RegExp(config.commitMessage, "gi");
// check and compare commit sha
if (body[0].commit.message.match(regMessage) != null
&& (typeof data.commit == "undefined" || data.commit != body[0].sha.substr(0, 6))) {
// Generate release description
var split = body[0].commit.message.split("\n\n");
var array = [];
data.titleCommit = split[0];
if (split.length > 1) {
for (var i=0; i < split.length; i++) {
if (i > 0) {
array.push(split[i]);
}
}
data.description = array.join("\n\n");
} else {
data.description = "";
}
// Storage commit sha
data.commit = body[0].sha.substr(0, 6);
data.emit('release');
} else {
console.log("last commit is not new release");
}
}
});
});
// Check last release
data.on('release', function () {
options.url = apiUrl+"/repos/"+data.json.full_name+"/releases";
// Charge last release
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
data.releases = body;
data.emit('create');
}
});
});
// Create new release
data.on('create', function () {
var newRelease = {};
var lastRelease = '';
if (data.releases.length > 0) {
lastRelease = data.releases[0].tag_name;
var splited = lastRelease.split(".");
var major = parseInt(splited[0]);
var minor = parseInt(splited[1]);
var patch = parseInt(splited[2]);
var regMessageMajor = new RegExp("major", "gi");
var regMessageMinor = new RegExp("minor", "gi");
if (data.titleCommit.match(regMessageMajor) != null) {
major++;
minor = 0;
patch = 0;
} else if (data.titleCommit.match(regMessageMinor) != null) {
minor++;
patch = 0;
} else {
patch++;
}
newRelease.tag = major+"."+minor+"."+patch;
newRelease.name = "Release "+major+"."+minor+"."+patch;
} else {
newRelease.tag = "0.0.1";
newRelease.name = "Release 0.0.1";
}
// Request for create new release
options.method = "POST";
options.url = apiUrl+"/repos/"+data.json.full_name+"/releases";
options.json = {
"tag_name": newRelease.tag,
"target_commitish": "master",
"name": newRelease.name,
"body": data.description,
"draft": false,
"prerelease": false
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 201) {
console.log(newRelease.name+" for "+data.json.full_name+" is created!");
} else {
console.log("Creating "+newRelease.name+" for "+data.json.full_name+" is failed!");
}
});
options.method = "GET";
});