forked from masum/pollingGitlab_postMattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.js
169 lines (156 loc) · 4.05 KB
/
gitlab.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
var http = require('http');
var async = require('async');
var querystring = require('querystring');
var _env;
var _token;
var _branches = [];
var _msg = [];
var _lastTime;
function gitlab() {
}
gitlab.run = function(time, env, callback) {
_env = env;
_lastTime = time;
async.waterfall([
procGetToken,
procGetMergeRequests,
procGetBranches,
procGetAllCommits
], function complete(err) {
callback(_msg);
});
};
function httpRequest(options, requestBody, callback) {
var req = http.request(options, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var json = JSON.parse(body);
callback(json);
});
}).on('error', function(e) {
console.log("gitlab http request error!");
console.log(e.message);
});
if (requestBody) {
req.write(requestBody);
}
req.end();
};
function procGetToken(next) {
var authParam = 'login=' + _env.id + '&password=' + _env.pw;
httpRequest({
hostname: _env.host,
path: '/api/v3/session',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, authParam, function(json) {
_token = json["private_token"];
next();
});
};
function procGetMergeRequests(next) {
var url = '/api/v3/projects/' + _env.project + '/merge_requests?state=opened&private_token=' + _token
httpRequest({
hostname: _env.host,
path: url,
method: 'GET'
}, null, function(list) {
for (var i=0;i<list.length;i++) {
var data = {}
var item = list[i];
if (_lastTime.getTime() > (new Date(item.updated_at)).getTime()) {
continue;
}
data.date = new Date(item.updated_at);
data.type = 'MergeRequest';
data.msg = '[MergeRequest](http://' + _env.host + '/' + _env.group + '/' + _env.repo + '/merge_requests/' + item.iid + ') ';
data.msg += item.title;
data.msg += '\n';
data.msg += ' UpVote(' + item.upvotes + ') DownVote(' + item.downvotes + ')';
data.msg += ' From ' + item['source_branch'] + ' into ' + item['target_branch'];
data.msg += ' (' + item.author_name + ') ' + makeDateStr(data.date);
data.msg += '\n';
data.msg += item.description;
data.icon = _env['icon'];
_msg.push(data);
}
next();
});
};
function procGetBranches(next) {
httpRequest({
hostname: _env.host,
path: '/api/v3/projects/' + _env.project + '/repository/branches?private_token=' + _token,
method: 'GET'
}, null, function(list) {
for (var i=0;i<list.length;i++) {
var date;
if (list[i].commit) {
date = list[i].commit['committed_date'];
if (_lastTime.getTime() > (new Date(date)).getTime()) {
continue;
}
}
_branches.push(list[i].name);
}
next();
});
};
function procGetAllCommits(next) {
async.eachSeries(_branches, function iterator(item, callback) {
readCommits(item, function() {
callback();
});
}, function(err) {
next();
});
}
function readCommits(branch, callback) {
var url = '/api/v3/projects/' + _env.project + '/repository/commits?private_token=' + _token;
if (branch) {
url = url + '&ref_name=' + branch;
}
httpRequest({
hostname: _env.host,
path: url,
method: 'GET'
}, null, function(list) {
for (var i=0;i<list.length;i++) {
var item = list[i];
if (_lastTime.getTime() > (new Date(item.created_at)).getTime()) {
continue;
}
var data = {}
data.date = new Date(item.created_at);
data.type = 'Commit';
data.msg = '[Commit to \'' + branch + '\'](http://' + _env.host + '/' + _env.group + '/' + _env.repo + '/commit/' + item.id +')';
data.msg += ' : ' + item.title + '(' + item.author_name + ') ' + makeDateStr(data.date);
data.msg += '\n';
data.msg += item.message;
data.icon = _env['icon'];
_msg.push(data);
}
callback();
});
}
function makeDateStr(date) {
var dateStr = [];
dateStr.push(dec(date.getMonth()+1));
dateStr.push('/');
dateStr.push(dec(date.getDate()));
dateStr.push(' ');
dateStr.push(dec(date.getHours()));
dateStr.push(':');
dateStr.push(dec(date.getMinutes()));
return dateStr.join('');
}
function dec(value) {
return (value < 10)? '0' + value : value;
}
module.exports = gitlab;