Changeset

3530:552d4944d1ca

mod_pubsub_github: Rearrange code to make it easier to handle other event types
author Kim Alvefur <zash@zash.se>
date Sun, 31 Mar 2019 19:08:43 +0200
parents 3529:6ac98c4dbbd3
children 3531:3bece2db869c
files mod_pubsub_github/mod_pubsub_github.lua
diffstat 1 files changed, 23 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/mod_pubsub_github/mod_pubsub_github.lua	Sun Mar 31 19:07:22 2019 +0200
+++ b/mod_pubsub_github/mod_pubsub_github.lua	Sun Mar 31 19:08:43 2019 +0200
@@ -59,31 +59,33 @@
 	end
 
 	local github_event = request.headers.x_github_event or data.object_kind;
+	module:log("debug", "Handling '%s' event: \n%s\n", github_event, tostring(request.body));
+
 	if github_event == "push" then
-		module:log("debug", "Handling 'push' event: \n%s\n", tostring(request.body));
+
+		for _, commit in ipairs(data.commits) do
+			local ok, err = pubsub_service:publish(node, github_actor, commit.id,
+				st.stanza("item", { id = commit.id, xmlns = "http://jabber.org/protocol/pubsub" })
+				:tag("entry", { xmlns = "http://www.w3.org/2005/Atom" })
+					:tag("id"):text(commit.id):up()
+					:tag("title"):text(commit.message:match("^[^\r\n]*")):up()
+					:tag("summary"):text(("Commit to %s by %s: %s"):format(data.repository.name, commit.author.name, commit.message:match("^[^\r\n]*"))):up()
+					:tag("content"):text(commit.message):up()
+					:tag("link", { rel = "alternate", href = commit.url }):up()
+					:tag("published"):text(commit.author.date):up()
+					:tag("author")
+						:tag("name"):text(commit.author.name):up()
+						:tag("email"):text(commit.author.email):up()
+						:up()
+			);
+			if not ok then
+				return error_mapping[err] or 500;
+			end
+		end
+
 	elseif github_event then
 		module:log("debug", "Unsupported Github event %q", github_event);
 		return 501;
-	end -- else .. is this even github?
-
-	for _, commit in ipairs(data.commits) do
-		local ok, err = pubsub_service:publish(node, github_actor, commit.id,
-			st.stanza("item", { id = commit.id, xmlns = "http://jabber.org/protocol/pubsub" })
-			:tag("entry", { xmlns = "http://www.w3.org/2005/Atom" })
-				:tag("id"):text(commit.id):up()
-				:tag("title"):text(commit.message:match("^[^\r\n]*")):up()
-				:tag("summary"):text(("Commit to %s by %s: %s"):format(data.repository.name, commit.author.name, commit.message:match("^[^\r\n]*"))):up()
-				:tag("content"):text(commit.message):up()
-				:tag("link", { rel = "alternate", href = commit.url }):up()
-				:tag("published"):text(commit.author.date):up()
-				:tag("author")
-					:tag("name"):text(commit.author.name):up()
-					:tag("email"):text(commit.author.email):up()
-					:up()
-		);
-		if not ok then
-			return error_mapping[err] or 500;
-		end
 	end
 
 	response.status_code = 202;