comparison mod_pubsub_github/mod_pubsub_github.lua @ 3508:a98a3922bc01

mod_pubsub_github: Send sensible status codes
author Kim Alvefur <zash@zash.se>
date Sun, 31 Mar 2019 16:05:24 +0200
parents d4207ab8ccc1
children 94414cadfcaa
comparison
equal deleted inserted replaced
3507:5c37d759b1e2 3508:a98a3922bc01
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 node = module:get_option("github_node", "github");
9 local secret = module:get_option("github_secret"); 9 local secret = module:get_option("github_secret");
10 10
11 function handle_POST(event) 11 function handle_POST(event)
12 local request = event.request; 12 local request, response = event.request, event.response;
13 if secret and ("sha1=" .. hmac_sha1(secret, request.body, true)) ~= request.headers.x_hub_signature then 13 if secret and ("sha1=" .. hmac_sha1(secret, request.body, true)) ~= request.headers.x_hub_signature then
14 return 401; 14 return 401;
15 end 15 end
16 local data = json.decode(request.body); 16 local data = json.decode(request.body);
17 if not data then 17 if not data then
18 response.status_code = 400;
18 return "Invalid JSON. From you of all people..."; 19 return "Invalid JSON. From you of all people...";
19 end 20 end
20 21
21 for _, commit in ipairs(data.commits) do 22 for _, commit in ipairs(data.commits) do
22 local ok, err = pubsub_service:publish(node, true, data.repository.name, 23 local ok, err = pubsub_service:publish(node, true, data.repository.name,
32 :up() 33 :up()
33 ); 34 );
34 end 35 end
35 36
36 module:log("debug", "Handled POST: \n%s\n", tostring(request.body)); 37 module:log("debug", "Handled POST: \n%s\n", tostring(request.body));
38 response.status_code = 202;
37 return "Thank you Github!"; 39 return "Thank you Github!";
38 end 40 end
39 41
40 module:provides("http", { 42 module:provides("http", {
41 route = { 43 route = {