plugins/github.lua

changeset 128
5b03c71d05f0
parent 127
6c977fbf6d0b
child 129
c9da10093c51
equal deleted inserted replaced
127:6c977fbf6d0b 128:5b03c71d05f0
4 4
5 function riddim.plugins.github(bot) 5 function riddim.plugins.github(bot)
6 local conf = bot.config.github; 6 local conf = bot.config.github;
7 local base_url = url.parse("https://api.github.com/repos/x/y/issues/123"); 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); 8 local base_path = url.parse_path(base_url.path);
9 base_path[2], base_path[3] = conf.user, conf.project;
10 9
11 local ex = { 10 local ex = {
12 headers = { 11 headers = {
13 Accept = "application/vnd.github.v3+json"; 12 Accept = "application/vnd.github.v3+json";
14 }; 13 };
15 }; 14 };
16 15
17 local function issue_url(number) 16 local function get_issue_url(conf, number)
17 base_path[2] = conf.user;
18 base_path[3] = conf.repo or conf.project;
18 base_path[5] = number; 19 base_path[5] = number;
19 base_url.path = url.build_path(base_path); 20 base_url.path = url.build_path(base_path);
20 local url = url.build(base_url); 21 local url = url.build(base_url);
21 return url; 22 return url;
22 end 23 end
23 24
24 bot:hook("commands/issue", function (command) 25 bot:hook("commands/issue", function (command)
25 local issue_id = tonumber(command.param); 26 local issue_id = tonumber(command.param);
26 if not issue_id then return; end 27 if not issue_id then return; end
28 local current_conf = conf[command.room and command.room.jid or "default"];
29 if not current_conf then return end
27 assert(http.request(issue_url(issue_id), ex, function (issue, code) 30 assert(http.request(issue_url(issue_id), ex, function (issue, code)
28 if code > 400 then 31 if code > 400 then
29 return command:reply("HTTP Error "..code.." :("); 32 return command:reply("HTTP Error "..code.." :(");
30 end 33 end
31 issue = issue and json.decode(issue); 34 issue = issue and json.decode(issue);

mercurial