Skip to content

Commit

Permalink
I AM ROBOT
Browse files Browse the repository at this point in the history
  • Loading branch information
jbalogh committed Jul 19, 2011
0 parents commit d9a0cd3
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 0 deletions.
92 changes: 92 additions & 0 deletions amobot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
var sys = require('sys'),
irc_ = require('irc'),
redis_ = require('redis'),
check = require('./check');


var amo = '#amo',
amobots = '#amo-bots',
NICK = 'gk0bes'
irc = new irc_.Client('irc.mozilla.org', NICK,
{channels: [amo, amobots]}),
jp = new irc_.Client('irc.mozilla.org', 'zalooon',
{channels: ['#flightdeck']}),
redis = redis_.createClient(6379, 'mradm02'),
repo = 'https://github.com/jbalogh/zamboni';

var channels = {
zamboni: [irc, amobots, amo],
'zamboni-lib': [irc, amobots, amo],
flightdeck: [jp, '#flightdeck', '#flightdeck'],
};


var updater = {
'master': 'https://addons.allizom.org/media/updater.output.txt',
'next': 'https://addons-next.allizom.org/media/updater.output.txt'
}


irc.on('message', function(from, to, message) {
if (message == NICK + ': yo') {
check.checkRev(function(amo, github) {
irc.say(to, from + ': preview is at ' + repo + '/commits/' + amo);
if (github.indexOf(amo) != 0) {
irc.say(to, from + ': we are behind master! ' + repo + '/compare/' + amo + '...' + github.substring(0, 8));
}
});
} else if (message == NICK + ': woo') {
irc.say(to, from + ': awww yeah');
} else if (message == NICK + ': meeting?') {
irc.say(to, from + ': hell no');
}
});
irc.on('error', function(msg) {
sys.puts('ERROR: ' + msg);
sys.puts(JSON.stringify(msg));
});
jp.on('error', function(msg) {
sys.puts('ERROR: ' + msg);
sys.puts(JSON.stringify(msg));
});

redis.on('pmessage', function(pattern, channel, message) {
sys.puts(pattern, channel);
var msg = JSON.parse(message);
if (/\.locked$/.exec(channel)) {
return;
}
if (msg instanceof Array) {
var commits = msg[3].commits,
commit = commits[commits.length - 1],
branch = msg[3].ref.split('/').pop(),
repo = msg[3].repository.name.toLowerCase();
if (!(repo in channels)) {
return sys.puts('unknown channel: ' + repo);
}
var ch = channels[repo],
bot = ch[0],
good = ch[1],
bad = ch[2];

sys.puts('returncode', msg[0]);
if (!(branch in updater)) {
return sys.puts('ignoring ' + branch);
} else if (msg[0] === 0) {
if (commit) {
bot.say(good, 'Pushed ' + msg[3].compare + ' by ' + commit.author.username);
} else {
sys.puts('wtf');
sys.puts(commits);
}
} else {
var up = updater[msg[3].ref.split('/').pop()];
bot.say(bad, 'Push failed: ' + updater[branch])
}
} else {
var commits = msg.commits,
commit = commits[commits.length - 1];
// irc.say(amo, 'Pushing ' + msg.compare + ' by ' + commit.author.username);
}
});
redis.psubscribe('update.*');
28 changes: 28 additions & 0 deletions check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var https = require('https'),
sys = require('sys');


function GET(options, cb) {
https.get(options, function(r) {
var body = '';
r.on('data', function(d){ body += d; });
r.on('end', function() {
cb(r, body);
});
});
}


var branchesURL = '/api/v2/json/repos/show/jbalogh/zamboni/branches';

exports.checkRev = function(cb) {
GET({host: 'github.com', path: branchesURL}, function(r, body) {
var ghRev = JSON.parse(body).branches.master, amoRev;
sys.puts(ghRev);
GET({host: 'addons.allizom.org', path: '/media/git-rev.txt'}, function(r, body) {
amoRev = body;
sys.puts(amoRev);
cb(amoRev, ghRev);
});
});
};
21 changes: 21 additions & 0 deletions format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Python(ish) string formatting:
* >>> format('{0}', ['zzz'])
* "zzz"
* >>> format('{0}{1}', 1, 2)
* "12"
* >>> format('{x}', {x: 1})
* "1"
*/
exports.format = (function() {
var re = /\{([^}]+)\}/g;
return function(s, args) {
if (!args) return;
if (!(args instanceof Array || args instanceof Object))
args = Array.prototype.slice.call(arguments, 1);
return s.replace(re, function(_, match){ return args[match]; });
};
})();

exports.template = function(s) {
return function(args) { return format(s, args); };
}
48 changes: 48 additions & 0 deletions pingbot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var sys = require('sys'),
irc_ = require('irc');


var pingbot = new irc_.Client('irc.mozilla.org', 'pingbot',
{channels: ['#sumodev', '#amo']});

pingbot.on('message', function(from, to, message) {
if (/pingbot\s*:\s*woo\s*$/.exec(message)) {
pingbot.say(to, from + ': aww yeah');
} else if (/pingbot\s*:\s*ping\s*$/.exec(message)) {
pingbot.say(to, from + ': PONG');
} else if (/pingbot\s*:\s*botsnack\s*$/.exec(message)) {
pingbot.say(to, 'OM NOM NOM');
} else if (/^\w+\s*:\s*can I ask you a question.\s*$/i.exec(message)) {
pingbot.say(to, from + ': please just ask your question.');
} else if (/^\w+\s*.\s*ping\s*$/i.exec(message)) {
pingbot.say(to, from + ': please just ask your question.');
} else if (/pingbot\s*:/.exec(message)) {
pingbot.say('jbalogh', '(' + to + ') ' + from + ' :: ' + message);
}
});

pingbot.on('pm', function(from, message) {
var match;
sys.puts(from + ': ' + message);
if (from == 'jbalogh') {
if (match = /^say:? ([#\w]+) (.*)$/.exec(message)) {
sys.puts(match[1] + ' => ' + match[2]);
pingbot.say(match[1], match[2]);
}
} else {
pingbot.say('jbalogh', '(' + from + ') :: ' + message);
}
});

pingbot.on('error', function(msg) {
sys.puts('ERROR: ' + msg);
sys.puts(JSON.stringify(msg));
});

pingbot.on('raw', function(msg) {
if (msg.command == 'INVITE') {
pingbot.say(msg.nick, 'joining ' + msg.args[1]);
pingbot.say('jbalogh', 'joining ' + msg.args[1]);
pingbot.join(msg.args[1]);
}
});
46 changes: 46 additions & 0 deletions pushbot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var sys = require('sys'),
irc_ = require('irc'),
redis_ = require('redis')
format = require('./format').format;


var amo = '#remora',
pushbot = new irc_.Client('irc.mozilla.org', 'pushbot',
{channels: [amo, '#amo']}),
redis = redis_.createClient(6379, 'mradm02'),
lastEvent,
lastEventTime;

pushbot.on('message', function(from, to, message) {
if (/pushbot\s*:\s*yo/.exec(message)) {
if (lastEvent) {
pushbot.say(to, from + ': ' + lastEventTime);
handle(to, lastEvent);
} else {
pushbot.say(to, from + ': all quiet over here boss');
}
}
});

function handle(channel, msg) {
if (msg.event == 'BEGIN') {
pushbot.say(channel, format('holy hell, {who} is pushing zamboni v{zamboni} ' +
'and vendor v{vendor}!', msg));
} else if (msg.event == 'PUSH') {
pushbot.say(channel, format('the push is now going to the webheads!! ' +
'(v{zamboni}/v{vendor} :{who})', msg));
} else if (msg.event == 'DONE') {
pushbot.say(channel, format('{who} pushed zamboni v{zamboni} and ' +
'vendor v{vendor}!!!', msg));
}
}


redis.on('message', function(channel, message) {
sys.puts(channel, message);
var msg = JSON.parse(message);
lastEvent = msg;
lastEventTime = new Date;
handle(amo, msg);
});
redis.subscribe('deploy.amo');

0 comments on commit d9a0cd3

Please sign in to comment.