-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
160 lines (142 loc) · 3.43 KB
/
app.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
var http = require('http');
var async = require('async');
var fs = require('fs')
var gitlab = require('./gitlab');
var config = require('config');
var client = null;
var lastTimePath = './lasttime.txt';
var lastTime;
var results = [];
var enableRedmine = true;
var enableGitlab = true;
function procGitlab(next) {
var env = {};
try {
env = config.get('gitlab');
} catch (e) {
}
var gitlabEnv = {};
gitlabEnv['id'] = process.env['GITLAB_ID'] || env['id'];
gitlabEnv['pw'] = process.env['GITLAB_PW'] || env['pw'];
gitlabEnv['host'] = process.env['GITLAB_HOST'] || env['host'];
gitlabEnv['project'] = process.env['GITLAB_PROJECT'] || env['project'];
gitlabEnv['group'] = process.env['GITLAB_GROUP'] || env['group'];
gitlabEnv['repo'] = process.env['GITLAB_REPO'] || env['repo'];
gitlabEnv['icon'] = process.env['GITLAB_ICON'] || env['icon'];
if (enableGitlab) {
gitlab.run(new Date(lastTime), gitlabEnv, function(msg) {
console.log('Hit Count : ' + msg.length);
results = results.concat(msg);
next();
});
} else {
next();
}
}
function dec(value) {
return (value < 10)? '0' + value : value;
}
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 MattermostOut() {}
MattermostOut.prototype.push = function(item, callback) {
var env = {};
try {
env = config.get('mattermost');
} catch (e) {
}
var host = process.env['MATTERMOST_HOST'] || env['host'];
var port = process.env['MATTERMOST_PORT'] || env['port'];
var path = process.env['MATTERMOST_PATH'] || env['path'];
var options = {
hostname: host,
path: path,
port: port,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}
var requestBbody = "payload=" + JSON.stringify({
"username": item.type,
"icon_url" : item.icon,
text : item.msg
});
var req = http.request(options, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
callback();
});
}).on('error', function(e) {
console.log('error')
console.log(e.message);
});
req.write(requestBbody);
req.end();
};
function ConsoleOut() {
}
ConsoleOut.prototype.push = function(item, callback) {
console.log(item.msg);
callback();
}
var outputModule = new MattermostOut();
function outputLog(arr) {
var list = arr.sort(function(a,b){
var aTime = new Date(a.date);
var bTime = new Date(b.date);
return (aTime.getTime() < bTime.getTime())?-1:1;
});
async.eachSeries(list, function iterator(item, callback) {
setTimeout(function() {
outputModule.push(item, function() {
callback();
});
}, 3 * 1000);
}, function(err) {
});
}
function procReadLastTime(next) {
if (fs.exists(lastTimePath, function(exists) {
if (exists) {
var buf = fs.readFileSync(lastTimePath);
if (buf) {
lastTime = buf;
}
}
console.log("Ticket Search Start from " + lastTime);
next();
}));
}
function updateLastTime() {
var dateStr = (new Date()).toString();
fs.writeFile(lastTimePath, dateStr);
}
function start(callback) {
async.waterfall([
procReadLastTime,
procGitlab
], function complete(err) {
updateLastTime();
callback(results);
});
}
function run() {
start(function(results) {
outputLog(results);
});
}
run();