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