forked from sdwilsh/tree-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
channels.js
223 lines (211 loc) · 6.66 KB
/
channels.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var format = require("./format");
var reporter = require("./reporter");
var builds = require("./builds");
var welshify = require("./welshify");
var mehdify = require("./mehdify");
var randompicker = require("./randompicker");
var committers = require("./committers");
var textutils = require("./textutils");
var TemporalCache = require("./temporalcache");
function interceptFormat(interceptor, originalfn)
{
return function () {
var args = arguments;
if (args[0] === "")
return;
interceptor(args[0], function (fmt) {
args[0] = fmt;
originalfn.apply(null, args);
});
};
}
function filterEventsByRev(rev, cb) {
return function (event) {
if (event.rev === rev)
cb(event);
}
}
var interceptors = [
{ 'chance' : 0.01, 'fn' : welshify },
{ 'chance' : 0.10, 'fn' : mehdify },
];
function chooseCallbackFunction(origfn)
{
var fn = randompicker(interceptors, 'fn');
if (fn === undefined) {
return origfn;
} else {
return interceptFormat(fn, origfn);
}
}
function fprintf(output)
{
return function () {
var text = format.apply(null, arguments);
if (text !== "")
output(text);
}
}
function Channel(name, backend) {
this.name = name;
this.trees = {};
this.watches = new TemporalCache();
if (typeof backend ==='function') {
backend = {
say: backend,
pm: backend
};
}
this.backend = backend;
this.say = fprintf(backend.say);
this.pm = function (who) {
var output = backend.pm.bind(backend, who);
return fprintf(output);
};
this.controller = new ChannelController(this);
}
Channel.prototype = {
watch: function (name) {
if (this.trees.hasOwnProperty(name))
return;
var tree = this.trees[name] = {
watcher: new builds.Watcher(name),
success: this._treeStatusCallback.bind(this, name, 'success', reporter.success),
warning: this._treeStatusCallback.bind(this, name, 'warning', reporter.warning),
failure: this._treeStatusCallback.bind(this, name, 'failure', reporter.failure),
reportStatus: new TemporalCache(),
};
tree.watcher.on("success", tree.success);
tree.watcher.on("warning", tree.warning);
tree.watcher.on("failure", tree.failure);
},
_treeStatusCallback: function (name, type, reporter, event) {
var cb = chooseCallbackFunction(this.say);
var tree = this.trees[name];
if (tree === undefined)
return;
var status = tree.reportStatus.get(event.rev);
if (status === undefined || event.result > status) {
// Remember status for 12 hours
tree.reportStatus.add(event.rev, event.result, 12);
reporter(cb, event);
}
},
watchChangeset: function (treeName, rev, who) {
var key = treeName+rev+who;
var cb = this.pm(who);
var watcher = new builds.Watcher(treeName);
// Do individual success reports matter?
//watcher.on("success", reporter.success.bind(reporter, cb));
watcher.on("warning", filterEventsByRev(rev, reporter.warning.bind(reporter, cb)));
watcher.on("failure", filterEventsByRev(rev, reporter.failure.bind(reporter, cb)));
// Watch for 12 hours - then no more
this.watches.add(key, watcher, 12);
},
unwatchChangeset: function (treeName, rev, who) {
var key = treeName+rev+who;
if (this.watches.has(key))
this.watches.remove(key);
},
unwatch: function (name) {
if (!this.trees.hasOwnProperty(name))
return;
var tree = this.trees[name];
tree.watcher.removeListener("success", tree.success);
tree.watcher.removeListener("warning", tree.warning);
tree.watcher.removeListener("failure", tree.failure);
delete this.trees[name];
},
tell: function (person) {
var self = this;
return function () {
var cb = chooseCallbackFunction(self.say);
if (person !== '')
cb = interceptFormat(textutils.prepend(person + ": "), cb);
cb.apply(self, arguments);
};
},
handleCommand: function (from, text) {
this.controller.handleCommand(from, text);
}
};
function ChannelController(channel)
{
this.channel = channel;
}
ChannelController.prototype = {
watch: function (from, tree) {
this.channel.watch(tree);
var trees = textutils.naturalJoin(Object.keys(this.channel.trees));
this.channel.tell(from)("Now watching: {0}", trees);
},
unwatch: function (from, tree) {
this.channel.unwatch(tree);
this.channel.tell(from)("No longer watching {0}", tree);
},
watchTree: function (from, rev, tree, who) {
if (who === undefined || who === 'me') {
this.channel.watchChangeset(tree, rev, from);
this.channel.tell(from)("I'll let you know if {0} burns {1}", rev, tree);
} else {
this.channel.watchChangeset(tree, rev, who);
this.channel.tell(from)("I'll let {2} know if {0} burns {1}", rev, tree, who);
}
},
unwatchTree: function (from, rev, tree, who) {
if (who === undefined || who === 'me') {
this.channel.unwatchChangeset(tree, rev, from);
this.channel.tell(from)("No longer watching if {0} burns {1}", rev, tree);
} else {
this.channel.unwatchChangeset(tree, rev, who);
this.channel.tell(from)("I'll stop letting {2} know if {0} burns {1}", rev, tree, who);
}
},
identify: function (from, email, name) {
// Identity is reflexive, canonicalize order if necessary
if (/(.+)@(.+)/.test(name)) {
var tmp = name;
name = email;
email = tmp;
}
if (name === 'me' || name === 'I')
name = from;
committers.add(email, name);
this.channel.tell(from)("thank you!");
},
help: function (from) {
this.channel.tell(from)("See https://github.com/sdwilsh/tree-bot/blob/master/README for a list of commands");
},
handleCommand: function (from, text) {
var self = this;
function tryCommand(matcher, cb) {
var match = matcher.exec(text);
if (match) {
Array.prototype.splice.call(match, 0, 1, from);
cb.apply(self, match);
}
return match != null;
}
tryCommand(/^watch ([A-Za-z-]+)$/, this.watch);
tryCommand(/^unwatch ([A-Za-z-]+)$/, this.unwatch);
tryCommand(/^(.+) is (.+)$/, this.identify);
tryCommand(/^(.+) am (.+)$/, this.identify);
tryCommand(/^watch ([A-Fa-f0-9]{12}) on ([A-Za-z-]+)(?: for (.+))?/, this.watchTree);
tryCommand(/^unwatch ([A-Fa-f0-9]{12}) on ([A-Za-z-]+)(?: for (.+))?/, this.unwatchTree);
tryCommand(/^h[ae]lp/, this.help);
}
};
var channels = {};
exports.add = function (name, output) {
var channel = channels[name] = new Channel(name, output);
reporter.greet(channel.say);
return channel;
};
exports.has = function (name) {
return channels.hasOwnProperty(name);
};
exports.get = function (name) {
if (!channels.hasOwnProperty(name))
return undefined;
return channels[name];
};