|
1 local url = require"socket.url"; |
|
2 local json = require"util.json"; |
|
3 local http = require"net.http"; |
|
4 |
|
5 function riddim.plugins.github(bot) |
|
6 local conf = bot.config.github; |
|
7 local base_url = url.parse("https://api.github.com/repos/x/y/issues/123"); |
|
8 local base_path = url.parse_path(base_url.path); |
|
9 |
|
10 local ex = { |
|
11 headers = { |
|
12 Accept = "application/vnd.github.v3+json"; |
|
13 }; |
|
14 }; |
|
15 |
|
16 local function get_issue_url(conf, number) |
|
17 base_path[2] = conf.user; |
|
18 base_path[3] = conf.repo or conf.project; |
|
19 base_path[5] = number; |
|
20 base_url.path = url.build_path(base_path); |
|
21 local url = url.build(base_url); |
|
22 return url; |
|
23 end |
|
24 |
|
25 bot:hook("commands/issue", function (command) |
|
26 local issue_id = tonumber(command.param); |
|
27 if not issue_id then return; end |
|
28 local current_conf = conf[command.room and command.room.jid] or conf; |
|
29 if not current_conf.user then return end |
|
30 assert(http.request(get_issue_url(current_conf, issue_id), ex, function (issue, code) |
|
31 if code > 400 then |
|
32 return command:reply("HTTP Error "..code.." :("); |
|
33 end |
|
34 issue = issue and json.decode(issue); |
|
35 if not issue then |
|
36 return command:reply("Got invalid JSON back :("); |
|
37 end |
|
38 command:reply(("%s #%d\n%s"):format(issue.title, issue.number, issue.html_url)); |
|
39 end)); |
|
40 return true; |
|
41 end); |
|
42 |
|
43 local function check_for_issue_id(message) |
|
44 local issue_id = message.body and message.body:match"#(%d+)"; |
|
45 if issue_id then |
|
46 return bot:event("commands/issue", { param = issue_id, reply = message.reply, }); |
|
47 end |
|
48 end |
|
49 |
|
50 bot:hook("groupchat/joining", function (room) |
|
51 room:hook("message", check_for_issue_id); |
|
52 end); |
|
53 end |