Comparison

mod_pubsub_github/mod_pubsub_github.lua @ 3517:ea1edd7cfb01

mod_pubsub_github: Add support for publishing to multiple node based on repository
author Kim Alvefur <zash@zash.se>
date Sun, 31 Mar 2019 18:10:12 +0200
parent 3516:d94875c3ddda
child 3518:95c1c3e057cf
comparison
equal deleted inserted replaced
3516:d94875c3ddda 3517:ea1edd7cfb01
3 local st = require "util.stanza"; 3 local st = require "util.stanza";
4 local json = require "util.json"; 4 local json = require "util.json";
5 local hmac_sha1 = require "util.hashes".hmac_sha1; 5 local hmac_sha1 = require "util.hashes".hmac_sha1;
6 6
7 local pubsub_service = module:depends("pubsub").service; 7 local pubsub_service = module:depends("pubsub").service;
8 local node = module:get_option("github_node", "github"); 8 local default_node = module:get_option("github_node", "github");
9 local node_prefix = module:get_option_string("github_node_prefix", "github/");
10 local node_mapping = module:get_option_string("github_node_mapping");
9 local github_actor = module:get_option_string("github_actor") or true; 11 local github_actor = module:get_option_string("github_actor") or true;
10 local secret = module:get_option("github_secret"); 12 local secret = module:get_option("github_secret");
11 13
12 assert(secret, "Please set 'github_secret'"); 14 assert(secret, "Please set 'github_secret'");
13 15
35 elseif github_event then 37 elseif github_event then
36 module:log("debug", "Unsupported Github event %q", github_event); 38 module:log("debug", "Unsupported Github event %q", github_event);
37 return 501; 39 return 501;
38 end -- else .. is this even github? 40 end -- else .. is this even github?
39 41
42 local node = default_node;
43 if node_mapping then
44 node = node_prefix .. data.repository[node_mapping];
45 end
46
40 for _, commit in ipairs(data.commits) do 47 for _, commit in ipairs(data.commits) do
41 local ok, err = pubsub_service:publish(node, github_actor, commit.id, 48 local ok, err = pubsub_service:publish(node, github_actor, commit.id,
42 st.stanza("item", { id = commit.id, xmlns = "http://jabber.org/protocol/pubsub" }) 49 st.stanza("item", { id = commit.id, xmlns = "http://jabber.org/protocol/pubsub" })
43 :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" }) 50 :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" })
44 :tag("id"):text(commit.id):up() 51 :tag("id"):text(commit.id):up()
63 route = { 70 route = {
64 POST = handle_POST; 71 POST = handle_POST;
65 }; 72 };
66 }); 73 });
67 74
68 function module.load() 75 if not node_mapping then
69 if not pubsub_service.nodes[node] then 76 function module.load()
70 local ok, err = pubsub_service:create(node, true); 77 if not pubsub_service.nodes[default_node] then
71 if not ok then 78 local ok, err = pubsub_service:create(default_node, true);
72 module:log("error", "Error creating node: %s", err); 79 if not ok then
73 else 80 module:log("error", "Error creating node: %s", err);
74 module:log("debug", "Node %q created", node); 81 else
82 module:log("debug", "Node %q created", default_node);
83 end
75 end 84 end
76 end 85 end
77 end 86 end