Comparison

mod_pubsub_github/mod_pubsub_github.lua @ 860:1c886affb375

mod_pubsub_github: Receive Github web hooks (generated on pushes to a repository) and forward to a local pubsub node
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Nov 2012 00:02:28 -0500
child 1343:7dbde05b48a9
comparison
equal deleted inserted replaced
859:9922e8cdf4a4 860:1c886affb375
1 module:depends("http");
2
3 local st = require "util.stanza";
4 local json = require "util.json";
5 local formdecode = require "net.http".formdecode;
6
7 local pubsub_service = module:depends("pubsub").service;
8 local node = module:get_option("github_node", "github");
9
10 function handle_POST(event)
11 local data = json.decode(formdecode(event.request.body).payload);
12 if not data then
13 return "Invalid JSON. From you of all people...";
14 end
15
16 for _, commit in ipairs(data.commits) do
17 local ok, err = pubsub_service:publish(node, true, data.repository.name,
18 st.stanza("item", { id = data.repository.name, xmlns = "http://jabber.org/protocol/pubsub" })
19 :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" })
20 :tag("id"):text(commit.id):up()
21 :tag("title"):text(commit.message):up()
22 :tag("link", { rel = "alternate", href = commit.url }):up()
23 :tag("published"):text(commit.timestamp):up()
24 :tag("author")
25 :tag("name"):text(commit.author.name):up()
26 :tag("email"):text(commit.author.email):up()
27 :up()
28 );
29 end
30
31 module:log("debug", "Handled POST: \n%s\n", tostring(event.request.body));
32 return "Thank you Github!";
33 end
34
35 module:provides("http", {
36 route = {
37 POST = handle_POST;
38 };
39 });
40
41 function module.load()
42 if not pubsub_service.nodes[node] then
43 local ok, err = pubsub_service:create(node, true);
44 if not ok then
45 module:log("error", "Error creating node: %s", err);
46 else
47 module:log("debug", "Node %q created", node);
48 end
49 end
50 end