From e5ae70c5216ff36207412d76b5d9df3b293c0337 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 27 Apr 2023 22:32:03 -0700 Subject: [PATCH] modules/web_hook: Add requests for workflows and dispatching. --- modules/web_hook.cc | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/modules/web_hook.cc b/modules/web_hook.cc index b6c2141bb..ca3ba81e8 100644 --- a/modules/web_hook.cc +++ b/modules/web_hook.cc @@ -933,6 +933,33 @@ github_hook_shot_retry(const string_view &repo, ); } +static bool +github_hook_shot_for_each_fail(const string_view &repo, + const string_view &hook, + const function_bool &closure) +{ + bool ret {true}; + github_hook_shot_for_each(repo, hook, true, [&ret, &closure] + (const json::object &object) + { + if(object.at("redelivery")) + return true; + + const json::string status + { + object["status"] + }; + + if(status == "OK") + return false; + + ret = closure(object); + return ret; + }); + + return ret; +} + static bool github_run_for_each_jobs(const string_view &repo, const string_view &run_id, @@ -992,6 +1019,59 @@ github_run_rerun_failed(const string_view &repo, github_request(buf, "POST", repo, "actions/runs/%s/rerun-failed-jobs", run_id); } +static void +github_run_dispatch(const string_view &repo, + const string_view &name, + const string_view &ref = "master", + const json::members inputs = {}) +{ + const json::strung content{json::members + { + { "ref", ref }, + { "inputs", inputs }, + }}; + + unique_const_buffer buf; + github_request(content, buf, "POST", repo, "actions/workflows/%s/dispatches", name); +} + +static bool +github_flow_for_each(const string_view &repo, + const function_bool &closure, + const bool active_only = true) +{ + unique_const_buffer buf; + const json::object response + { + github_request + ( + //TODO: pagination token + buf, "GET", repo, "actions/workflows?per_page=100&page=1" + ) + }; + + const json::array workflows + { + response["workflows"] + }; + + for(const json::object flow : workflows) + { + const json::string state + { + flow["state"] + }; + + if(active_only && state != "active") + continue; + + if(!closure(flow)) + return false; + } + + return true; +} + bool github_handle__workflow_run(std::ostream &out, std::ostream &alt,