From 25bb5af1aebd3e1f6f8a6a18805a94b8e23288bf Mon Sep 17 00:00:00 2001 From: Francois Kritzinger Date: Thu, 23 May 2024 10:12:42 +0200 Subject: Handle pull_request events --- mod/mod-ci-github.cxx | 76 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 12 deletions(-) (limited to 'mod/mod-ci-github.cxx') diff --git a/mod/mod-ci-github.cxx b/mod/mod-ci-github.cxx index b9a8271..7205e6e 100644 --- a/mod/mod-ci-github.cxx +++ b/mod/mod-ci-github.cxx @@ -338,10 +338,27 @@ namespace brep return true; } } - // else if (event == "pull_request") - // { - // return handle_pull_request (); - // } + else if (event == "pull_request") + { + gh_pull_request_event pr; + try + { + json::parser p (body.data (), body.size (), "pull_request event"); + + pr = gh_pull_request_event (p); + } + catch (const json::invalid_json_input& e) + { + string m ("malformed JSON in " + e.name + " request body"); + + error << m << ", line: " << e.line << ", column: " << e.column + << ", byte offset: " << e.position << ", error: " << e; + + throw invalid_request (400, move (m)); + } + + return handle_pull_request (move (pr), warning_success); + } else { // Log to investigate. @@ -512,14 +529,49 @@ namespace brep // Note: when a PR is merged we will get a pull_request(closed) and then a // check_suite for the base branch. // - // bool ci_github:: - // handle_pull_request (gh_pull_request_event pr) - // { - // // Fetch pull request in order to start job creating test merge - // // commit. Discard result. - // // - // // Start unloaded CI job. - // } + bool ci_github:: + handle_pull_request (gh_pull_request_event pr, bool warning_success) + { + HANDLER_DIAG; + + l3 ([&]{trace << "pull_request event { " << pr << " }";}); + + optional jwt (generate_jwt (trace, error)); + if (!jwt) + throw server_error (); + + optional iat ( + obtain_installation_access_token (pr.installation.id, + move (*jwt), + error)); + + if (!iat) + throw server_error (); + + l3 ([&]{trace << "installation_access_token { " << *iat << " }";}); + + string sd (service_data (warning_success, + move (iat->token), + iat->expires_at, + pr.installation.id, + move (pr.repository.node_id), + pr.pull_request.head_sha) + .json ()); + + optional tid ( + create (error, warn, &trace, + *build_db_, + tenant_service (move (pr.pull_request.node_id), + "ci-github", + move (sd)), + chrono::seconds (30), + chrono::seconds (10))); + + if (!tid) + fail << "unable to create unloaded CI request"; + + return true; + } // Build state change notifications (see tenant-services.hxx for // background). Mapping our state transitions to GitHub pose multiple -- cgit v1.1