0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 00:02:34 +01:00

modules/web_hook: Add post-push handler for reacting to push events.

This commit is contained in:
Jason Volk 2023-04-27 22:33:46 -07:00
parent e5ae70c521
commit 0624b69246

View file

@ -174,6 +174,10 @@ static bool
github_handle__push(std::ostream &, github_handle__push(std::ostream &,
const json::object &content); const json::object &content);
static bool
github_post_handle__push(const m::event::id &,
const json::object &content);
static bool static bool
github_handle__pull_request(std::ostream &, github_handle__pull_request(std::ostream &,
const json::object &content); const json::object &content);
@ -355,6 +359,9 @@ github_handle(client &client,
m::msghtml(room_id, user_id, view(out, buf[0]), view(alt, buf[1]), "m.notice") m::msghtml(room_id, user_id, view(out, buf[0]), view(alt, buf[1]), "m.notice")
}; };
if(type == "push")
github_post_handle__push(evid, request.content);
log::info log::info
{ {
"Webhook [%s] '%s' delivered to %s %s", "Webhook [%s] '%s' delivered to %s %s",
@ -1591,9 +1598,8 @@ try
json::object{relates_content}.get("body") json::object{relates_content}.get("body")
}; };
if(!startswith(relates_body, "job status table ")) if(startswith(relates_body, "job status table "))
return; {
const auto suffix const auto suffix
{ {
lstrip(relates_body, "job status table ") lstrip(relates_body, "job status table ")
@ -1626,6 +1632,44 @@ try
github_run_rerun(repopath, run_id); github_run_rerun(repopath, run_id);
break; break;
} }
}
else if(startswith(relates_body, "push by "))
{
const auto suffix
{
lstrip(relates_body, "push by ")
};
string_view token[5];
ircd::tokens(suffix, ' ', token);
const auto &[pusher, _by_, repo, _at_, commit] {token};
assert(pusher);
assert(repo);
assert(commit);
if(!repo)
return;
if(startswith(key, "▶️"))
{
const auto id
{
between(key, '(', ')')
};
const auto ref
{
// commit // hash not supported by github
"master"
};
github_run_dispatch(repo, id, ref, json::members
{
});
return;
}
}
} }
catch(const ctx::interrupted &) catch(const ctx::interrupted &)
{ {
@ -1944,6 +1988,54 @@ github_handle__push(std::ostream &out,
return true; return true;
} }
static bool
github_post_handle__push(const m::event::id &push_event_id,
const json::object &content)
{
const m::user::id::buf _webhook_user
{
string_view{webhook_user}, my_host()
};
const auto _webhook_room
{
m::room_id(string_view(webhook_room))
};
const auto repo
{
github_repopath(content)
};
github_flow_for_each(repo, [&]
(const json::object &flow)
{
const json::string name
{
flow["name"]
};
const json::string id
{
flow["id"]
};
const fmt::bsprintf<128> key
{
"▶️ %s (%s)", name, id
};
const auto annote_id
{
m::annotate(_webhook_room, _webhook_user, push_event_id, string_view(key))
};
return true;
});
return true;
}
bool bool
github_handle__pull_request(std::ostream &out, github_handle__pull_request(std::ostream &out,
const json::object &content) const json::object &content)