Skip to content

Commit

Permalink
modules/web_hook: Add post-push handler for reacting to push events.
Browse files Browse the repository at this point in the history
  • Loading branch information
jevolk committed May 1, 2023
1 parent e5ae70c commit 0624b69
Showing 1 changed file with 119 additions and 27 deletions.
146 changes: 119 additions & 27 deletions modules/web_hook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ static bool
github_handle__push(std::ostream &,
const json::object &content);

static bool
github_post_handle__push(const m::event::id &,
const json::object &content);

static bool
github_handle__pull_request(std::ostream &,
const json::object &content);
Expand Down Expand Up @@ -355,6 +359,9 @@ github_handle(client &client,
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
{
"Webhook [%s] '%s' delivered to %s %s",
Expand Down Expand Up @@ -1591,40 +1598,77 @@ try
json::object{relates_content}.get("body")
};

if(!startswith(relates_body, "job status table "))
return;

const auto suffix
if(startswith(relates_body, "job status table "))
{
lstrip(relates_body, "job status table ")
};
const auto suffix
{
lstrip(relates_body, "job status table ")
};

string_view token[3];
ircd::tokens(suffix, ' ', token);
const auto &[repopath, run_id, attempt] {token};
assert(repopath);
assert(run_id);
if(!repopath || !run_id)
return;
string_view token[3];
ircd::tokens(suffix, ' ', token);
const auto &[repopath, run_id, attempt] {token};
assert(repopath);
assert(run_id);
if(!repopath || !run_id)
return;

switch(hash(key))
switch(hash(key))
{
case hash("🚮"_sv):
github_run_delete(repopath, run_id);
m::redact(room, user_id, relates_event_id, "deleted");
break;

case hash(""_sv):
github_run_cancel(repopath, run_id);
break;

case hash("🔄"_sv):
github_run_rerun_failed(repopath, run_id);
break;

case hash("↪️"_sv):
github_run_rerun(repopath, run_id);
break;
}
}
else if(startswith(relates_body, "push by "))
{
case hash("🚮"_sv):
github_run_delete(repopath, run_id);
m::redact(room, user_id, relates_event_id, "deleted");
break;
const auto suffix
{
lstrip(relates_body, "push by ")
};

case hash(""_sv):
github_run_cancel(repopath, run_id);
break;
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;

case hash("🔄"_sv):
github_run_rerun_failed(repopath, run_id);
break;
if(startswith(key, "▶️"))
{
const auto id
{
between(key, '(', ')')
};

case hash("↪️"_sv):
github_run_rerun(repopath, run_id);
break;
const auto ref
{
// commit // hash not supported by github
"master"
};

github_run_dispatch(repo, id, ref, json::members
{

});

return;
}
}
}
catch(const ctx::interrupted &)
Expand Down Expand Up @@ -1944,6 +1988,54 @@ github_handle__push(std::ostream &out,
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
github_handle__pull_request(std::ostream &out,
const json::object &content)
Expand Down

0 comments on commit 0624b69

Please sign in to comment.