From 67d9bff870aa9273eef9bc798b766feed41bd101 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 21 Jun 2022 20:15:57 -0700 Subject: [PATCH] modules/web_hook: Add dockerhub shot handler. --- modules/web_hook.cc | 143 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/modules/web_hook.cc b/modules/web_hook.cc index 4e6692ace..85588d923 100644 --- a/modules/web_hook.cc +++ b/modules/web_hook.cc @@ -76,16 +76,28 @@ static void appveyor_handle(client &, const resource::request &); +static void +dockerhub_handle(client &, + const resource::request &); + resource::response post__webhook(client &client, const resource::request &request) { - if(has(http::headers(request.head.headers), "X-GitHub-Event"_sv)) + const http::headers &headers + { + request.head.headers + }; + + if(http::has(headers, "X-GitHub-Event")) github_handle(client, request); - else if(has(http::headers(request.head.headers), "X-Appveyor-Secret"_sv)) + else if(http::has(headers, "X-Appveyor-Secret")) appveyor_handle(client, request); + else if(startswith(request.head.content_type, "application/json")) + dockerhub_handle(client, request); + return resource::response { client, http::OK @@ -1779,6 +1791,11 @@ catch(const crh::error &e) }; } +/////////////////////////////////////////////////////////////////////////////// +// +// appveyor +// + void appveyor_handle(client &client, const resource::request &request) @@ -1788,3 +1805,125 @@ appveyor_handle(client &client, request.head.headers }; } + +/////////////////////////////////////////////////////////////////////////////// +// +// dockerhub +// + +static void +dockerhub_handle_push(std::ostream &out, + client &client, + const resource::request &request); + +void +dockerhub_handle(client &client, + const resource::request &request) +try +{ + if(!string_view(webhook_room)) + return; + + if(!string_view(webhook_user)) + return; + + const auto callback_url + { + request["callback_url"] + }; + + const unique_buffer buf + { + 48_KiB + }; + + std::stringstream out; + pubsetbuf(out, buf); + + if(request.has("push_data")) + dockerhub_handle_push(out, client, request); + + const auto room_id + { + m::room_id(string_view(webhook_room)) + }; + + const m::user::id::buf user_id + { + string_view(webhook_user), my_host() + }; + + const auto msg + { + view(out, buf) + }; + + const auto evid + { + m::msghtml(room_id, user_id, msg, "dockerhub shot", "m.notice") + }; + + log::info + { + "Webhook '%s' delivered to %s %s", + "push"_sv, + string_view{room_id}, + string_view{evid}, + }; +} +catch(const std::exception &e) +{ + log::error + { + "dockerhub webhook :%s", + e.what(), + }; + + throw; +} + +void +dockerhub_handle_push(std::ostream &out, + client &client, + const resource::request &request) +{ + const json::object push_data + { + request["push_data"] + }; + + const json::object repository + { + request["repository"] + }; + + const json::string pusher + { + push_data["pusher"] + }; + + const string_view pushed_at + { + push_data["pushed_at"] + }; + + const json::string tag + { + push_data["tag"] + }; + + const json::array images + { + push_data["images"] + }; + + out + << "" + << json::string(repository["repo_name"]) + << " push by " + << pusher + << " to " + << tag + << "" + ; +}