Add plugin for fetching issue info from GitHub

Sat, 27 Jun 2015 22:32:23 +0200

author
Kim Alvefur <zash@zash.se>
date
Sat, 27 Jun 2015 22:32:23 +0200
changeset 119
e09157903d93
parent 118
b3fa3397da0b
child 120
91a29c964e97

Add plugin for fetching issue info from GitHub

plugins/github.lua file | annotate | diff | comparison | revisions
squishy file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/github.lua	Sat Jun 27 22:32:23 2015 +0200
@@ -0,0 +1,54 @@
+local url = require"socket.url";
+local json = require"util.json";
+local http = require"net.http";
+
+function riddim.plugins.github(bot)
+	local conf = bot.config.github;
+	local base_url = url.parse("https://api.github.com/repos/x/y/issues/123");
+	local base_path = url.parse_path(base_url.path);
+	base_path[2], base_path[3] = conf.user, conf.project;
+
+	local ex = {
+		headers = {
+			Accept = "application/vnd.github.v3+json";
+		};
+	};
+
+	local function issue_url(number)
+		base_path[5] = number;
+		base_url.path = url.build_path(base_path);
+		local url = url.build(base_url);
+		print("url:", url);
+		return url;
+	end
+
+	bot:hook("commands/issue", function (command)
+		local issue_id = tonumber(command.param);
+		if not issue_id then return; end
+		print(issue_id);
+		assert(http.request(issue_url(issue_id), ex, function (issue, code)
+			if code > 400 then
+				return command:reply("HTTP Error "..code.." :(");
+			end
+			issue = issue and json.decode(issue);
+			if not issue then
+				return command:reply("Got invalid JSON back :(");
+			end
+			command:reply(("%s #%d\n%s"):format(issue.title, issue.number issue.html_url));
+		end));
+		return true;
+	end);
+
+	local function check_for_issue_id(message)
+		local issue_id = message.body and message.body:match"#(%d+)";
+		if issue_id then
+			return bot:event("commands/issue", { param = issue_id, reply = message.reply, });
+		end
+	end
+
+	bot:hook("message", check_for_issue_id);
+
+	bot:hook("groupchat/joining", function (room)
+		room:hook("message", check_for_issue_id);
+	end);
+end
--- a/squishy	Tue Jun 09 17:12:27 2015 +0200
+++ b/squishy	Sat Jun 27 22:32:23 2015 +0200
@@ -21,6 +21,7 @@
 	"slap",
 	"topic",
 	"trac",
+	"github",
 	"urltitle",
 	"xeps",
 	"xkcd",

mercurial