Software / code / prosody-modules
Comparison
mod_pubsub_forgejo/mod_pubsub_forgejo.lua @ 6203:131b8bfbefb4
mod_pubsub_forgejo: new module for forgejo webhooks
| author | nicoco <nicoco@nicoco.fr> |
|---|---|
| date | Mon, 17 Feb 2025 23:28:05 +0100 |
comparison
equal
deleted
inserted
replaced
| 6202:6d5a19bdd718 | 6203:131b8bfbefb4 |
|---|---|
| 1 module:depends("http") | |
| 2 local pubsub_service = module:depends("pubsub").service | |
| 3 | |
| 4 local st = require "util.stanza" | |
| 5 local json = require "util.json" | |
| 6 local hashes = require "util.hashes" | |
| 7 local from_hex = require"util.hex".from | |
| 8 local hmacs = { | |
| 9 sha1 = hashes.hmac_sha1, | |
| 10 sha256 = hashes.hmac_sha256, | |
| 11 sha384 = hashes.hmac_sha384, | |
| 12 sha512 = hashes.hmac_sha512 | |
| 13 } | |
| 14 | |
| 15 local format = module:require "format" | |
| 16 local default_templates = module:require "templates" | |
| 17 | |
| 18 -- configuration | |
| 19 local forgejo_secret = module:get_option("forgejo_secret") | |
| 20 | |
| 21 local default_node = module:get_option("forgejo_node", "forgejo") | |
| 22 local node_prefix = module:get_option_string("forgejo_node_prefix", "forgejo/") | |
| 23 local node_mapping = module:get_option_string("forgejo_node_mapping") | |
| 24 local forgejo_actor = module:get_option_string("forgejo_actor") or true | |
| 25 | |
| 26 local skip_commitless_push = module:get_option_boolean( | |
| 27 "forgejo_skip_commitless_push", true) | |
| 28 local custom_templates = module:get_option("forgejo_templates") | |
| 29 | |
| 30 local forgejo_templates = default_templates | |
| 31 | |
| 32 if custom_templates ~= nil then | |
| 33 for k, v in pairs(custom_templates) do forgejo_templates[k] = v end | |
| 34 end | |
| 35 | |
| 36 -- used for develoment, should never be set in prod! | |
| 37 local insecure = module:get_option_boolean("forgejo_insecure", false) | |
| 38 -- validation | |
| 39 if not insecure then assert(forgejo_secret, "Please set 'forgejo_secret'") end | |
| 40 | |
| 41 local error_mapping = { | |
| 42 ["forbidden"] = 403, | |
| 43 ["item-not-found"] = 404, | |
| 44 ["internal-server-error"] = 500, | |
| 45 ["conflict"] = 409 | |
| 46 } | |
| 47 | |
| 48 local function verify_signature(secret, body, signature) | |
| 49 if insecure then return true end | |
| 50 if not signature then return false end | |
| 51 local algo, digest = signature:match("^([^=]+)=(%x+)") | |
| 52 if not algo then return false end | |
| 53 local hmac = hmacs[algo] | |
| 54 if not algo then return false end | |
| 55 return hmac(secret, body) == from_hex(digest) | |
| 56 end | |
| 57 | |
| 58 function handle_POST(event) | |
| 59 local request, response = event.request, event.response | |
| 60 | |
| 61 if not verify_signature(forgejo_secret, request.body, | |
| 62 request.headers.x_hub_signature) then | |
| 63 module:log("debug", "Signature validation failed") | |
| 64 return 401 | |
| 65 end | |
| 66 | |
| 67 local data = json.decode(request.body) | |
| 68 if not data then | |
| 69 response.status_code = 400 | |
| 70 return "Invalid JSON. From you of all people..." | |
| 71 end | |
| 72 | |
| 73 local forgejo_event = request.headers.x_forgejo_event or data.object_kind | |
| 74 | |
| 75 if skip_commitless_push and forgejo_event == "push" and data.total_commits == 0 then | |
| 76 module:log("debug", "Skipping push event with 0 commits") | |
| 77 return 501 | |
| 78 end | |
| 79 | |
| 80 if forgejo_templates[forgejo_event] == nil then | |
| 81 module:log("debug", "Unsupported forgejo event %q", forgejo_event) | |
| 82 return 501 | |
| 83 end | |
| 84 | |
| 85 local item = format(data, forgejo_templates[forgejo_event]) | |
| 86 | |
| 87 if item == nil then | |
| 88 module:log("debug", "Formatter returned nil for event %q", forgejo_event) | |
| 89 return 501 | |
| 90 end | |
| 91 | |
| 92 local node = default_node | |
| 93 if node_mapping then node = node_prefix .. data.repository[node_mapping] end | |
| 94 | |
| 95 create_node(node) | |
| 96 | |
| 97 local ok, err = pubsub_service:publish(node, forgejo_actor, item.attr.id, item) | |
| 98 if not ok then return error_mapping[err] or 500 end | |
| 99 | |
| 100 response.status_code = 202 | |
| 101 return "Thank you forgejo.\n" .. tostring(item:indent(1, " ")) | |
| 102 end | |
| 103 | |
| 104 module:provides("http", {route = {POST = handle_POST}}) | |
| 105 | |
| 106 function create_node(node) | |
| 107 if not pubsub_service.nodes[node] then | |
| 108 local ok, err = pubsub_service:create(node, true) | |
| 109 if not ok then | |
| 110 module:log("error", "Error creating node: %s", err) | |
| 111 else | |
| 112 module:log("debug", "Node %q created", node) | |
| 113 end | |
| 114 end | |
| 115 end |