comparison mod_pubsub_googlecode/mod_pubsub_googlecode.lua @ 946:2c5430ff1c11

mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
author Matthew Wild <mwild1@gmail.com>
date Wed, 03 Apr 2013 15:14:16 +0100
parents
children 79b4a1db7a57
comparison
equal deleted inserted replaced
945:dbcbcec37d24 946:2c5430ff1c11
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 local hmac = require "util.hmac";
7 local st = require "util.stanza";
8 local json = require "util.json";
9 local datetime = require "util.datetime".datetime;
10
11
12 local pubsub_service = module:depends("pubsub").service;
13
14 local node = module:get_option_string("googlecode_node", "googlecode");
15 local auth_key = module:get_option_string("googlecode_auth_key");
16
17 if not auth_key then
18 module:log("warn", "Specify googlecode_auth_key to prevent commit spoofing!");
19 end
20
21 function handle_POST(event)
22 local request = event.request;
23 local body = request.body;
24
25 if auth_key then
26 local digest_header = request.headers["google-code-project-hosting-hook-hmac"];
27 local digest = hmac.md5(auth_key, body, true);
28 if digest ~= digest_header then
29 module:log("warn", "Commit POST failed authentication check, sender gave %s, we got %s, body was:\n%s", tostring(digest_header), tostring(digest), tostring(body));
30 return "No thanks.";
31 end
32 end
33
34 local data = json.decode(body);
35
36 local project = data.project_name or "somewhere";
37 for _, rev in ipairs(data.revisions) do
38 if rev.url:match("^http://wiki.") then
39 local what;
40 for _, page in ipairs(rev.added) do
41 what = page:match("^/(.-)%.wiki");
42 if what then break; end
43 end
44 if not what then
45 for _, page in ipairs(rev.modified) do
46 what = page:match("^/(.-)%.wiki");
47 if what then break; end
48 end
49 end
50 rev.message = "wiki ("..(what or "unknown page").."): "..rev.message;
51 end
52
53 local ok, err = pubsub_service:publish(node, true, project,
54 st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "project" })
55 :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" })
56 :tag("id"):text(tostring(rev.revision)):up()
57 :tag("title"):text(rev.message):up()
58 :tag("link", { rel = "alternate", href = rev.url }):up()
59 :tag("published"):text(datetime(rev.timestamp)):up()
60 :tag("author")
61 :tag("name"):text(rev.author):up()
62 :up()
63 );
64 end
65 module:log("debug", "Handled POST: \n%s\n", tostring(body));
66 return "Thank you Google!";
67 end
68
69 module:provides("http", {
70 route = {
71 POST = handle_POST;
72 };
73 });
74
75 function module.load()
76 if not pubsub_service.nodes[node] then
77 local ok, err = pubsub_service:create(node, true);
78 if not ok then
79 module:log("error", "Error creating node: %s", err);
80 else
81 module:log("debug", "Node %q created", node);
82 end
83 end
84 end