-
Notifications
You must be signed in to change notification settings - Fork 3
/
jig.js
182 lines (165 loc) · 5.69 KB
/
jig.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
170
171
172
173
174
175
176
177
178
179
180
181
182
var http = require('http'),
https = require('https'),
qs = require('querystring'),
irc = require('irc'),
options = require('nomnom').opts({
config: {
string: "-c CONFIG, --config=CONFIG",
default: "config.ini",
help: "What config file to use. (Default: config.ini)"
}
}).parseArgs(),
daemon = require('daemon'),
IniReader = require('inireader').IniReader,
GitHubApi = require('github'),
github = new GitHubApi({version: "3.0.0"}),
PULLREQRE = /pull\s+(?:req\s+)?#?(\d+)/i,
TAGRE = /\/tags\/(.+)$/i;
inireader = new IniReader();
inireader.load(options.config);
var CONFIG = inireader.getBlock();
var CHANNELS = CONFIG.irc.channels.split(','),
BRANCHES = {},
JENKINS = CONFIG.http.jenkins,
IRCHOST = CONFIG.irc.server,
IRCNICK = CONFIG.irc.nick;
CHANNELS.forEach(function(c, i) {
CHANNELS[i] = c.trim();
});
for (var br in CONFIG.branches) {
var ref = CONFIG.branches[br];
BRANCHES[ref] = CONFIG['branch:' + br];
BRANCHES[ref].name = br;
}
function interpolate(fmt, obj, named) {
if (named) {
return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
} else {
return fmt.replace(/%s/g, function(match){return String(obj.shift())});
}
}
var client = new irc.Client(IRCHOST, IRCNICK, {
channels: CHANNELS
}).on('error', function(err) {
if (err.rawCommand != '421') {
console.log(err);
if (err.hasOwnProperty('stack')) {
console.log(err.stack);
}
}
});
var response = '\00307#%(number)s\003 - \00303%(user)s\003 - %(title)s - %(url)s';
client.on('message', function(from, to, msg) {
if (to.indexOf('#') != 0) return;
msg = msg.toLowerCase();
if (msg.indexOf(IRCNICK) == 0 && /botsnack\s*$/i.test(msg)) {
client.say(to, 'YUMMY');
} else if (msg.indexOf(IRCNICK) == 0 && /thanks/i.test(msg)) {
client.say(to, 'no problem!');
} else if (PULLREQRE.test(msg)) {
var m = PULLREQRE.exec(msg);
github.pullRequests.get({user: CONFIG.github.user, repo: CONFIG.github.repo, number: m[1]}, function(err, pull) {
if (err || !pull) {
console.log(err);
return;
}
client.say(
to,
interpolate(response, {
'number': pull.number,
'title': pull.title,
'url': pull.html_url,
'user': pull.user.name
}, true));
});
}
});
var server = http.createServer(function(req, res) {
if (req.method != 'POST') {
res.statusCode = 405;
res.end();
return;
}
var payload = '';
req.on('data', function(d) {
payload += d;
});
req.on('end', function() {
res.statusCode = 200;
res.end();
try {
var post, data;
post = qs.parse(payload);
data = JSON.parse(post.payload);
} catch(e) {
console.log(e);
return;
}
console.log('ref is: ' + data.ref);
var push = '\00303%(pusher)s\003 \00307%(branch)s\003: %(compare)s',
commit = '\00303%(author)s\003 \002%(sha)s\002: %(msg)s';
if (data.ref in BRANCHES) {
console.log('push to ' + data.ref);
var ref = BRANCHES[data.ref];
var pushMsg = interpolate(push, {
'pusher': data.pusher.name,
'branch': ref.name,
'compare': data.compare
}, true),
commitMsgs = [];
if (ref.commits && ref.commits.toLowerCase() == 'yes') {
data.commits.forEach(function(c) {
commitMsgs.push(interpolate(commit, {
'author': c.author.username,
'sha': c.id.substring(0, 8),
'msg': c.message
}, true));
});
}
CHANNELS.forEach(function(c) {
client.say(c, pushMsg);
commitMsgs.forEach(function(m) {
client.say(c, m);
});
});
var q = {
token: ref.token,
cause: 'Push by ' + data.pusher.name
};
var web = http,
headers = {};
if (CONFIG.http.ssl == 'yes') {
web = https;
}
if (CONFIG.http.auth) {
headers['Authorization'] = CONFIG.http.auth;
}
// We wrap this in a setTimeout to give downstream repos
// time to sync with github.
setTimeout(function() {
web.get({
host: JENKINS,
path: ref.path + '?' + qs.stringify(q),
headers: headers
}, function(res) {
console.log(res.statusCode + ': ' + ref.path + '?' + qs.stringify(q));
}).on('error', function(err) {
console.log(err);
});
}, 5000);
} else if (TAGRE.test(data.ref)) {
var m = TAGRE.exec(data.ref),
tag = '\002New Tag:\002 \00312%(tag)s\003 pushed by \00303%(pusher)s\003',
tagMsg = interpolate(tag, {'tag': m[1], 'pusher': data.pusher.name}, true);
CHANNELS.forEach(function(c) {
client.say(c, tagMsg);
});
}
});
});
server.listen(CONFIG.http.port, CONFIG.http.host);
if (CONFIG.process.daemon == 'yes') {
daemon.daemonize(CONFIG.process.logfile, CONFIG.process.pidfile, function(err, pid) {
console.log('Daemonizing');
});
}