From abd6ede8444a89b6c56c20d06110cb3923b05bbe Mon Sep 17 00:00:00 2001 From: Francois Kritzinger Date: Tue, 6 Feb 2024 16:35:59 +0200 Subject: Get installation access token (IAT) and restructure code --- mod/mod-ci-github.hxx | 116 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 4 deletions(-) (limited to 'mod/mod-ci-github.hxx') diff --git a/mod/mod-ci-github.hxx b/mod/mod-ci-github.hxx index a869878..9731881 100644 --- a/mod/mod-ci-github.hxx +++ b/mod/mod-ci-github.hxx @@ -4,16 +4,111 @@ #ifndef MOD_MOD_CI_GITHUB_HXX #define MOD_MOD_CI_GITHUB_HXX -#include - #include #include #include #include +namespace butl +{ + namespace json + { + class parser; + } +} + namespace brep { + // GitHub request/response types. + // + // Note that having this types directly in brep causes clashes (e.g., for + // the repository name). + // + namespace gh + { + namespace json = butl::json; + + // The "check_suite" object within a check_suite webhook event request. + // + struct check_suite + { + uint64_t id; + string head_branch; + string head_sha; + string before; + string after; + + explicit + check_suite (json::parser&); + + check_suite () = default; + }; + + struct repository + { + string name; + string full_name; + string default_branch; + + explicit + repository (json::parser&); + + repository () = default; + }; + + struct installation + { + uint64_t id; + + explicit + installation (json::parser&); + + installation () = default; + }; + + // The check_suite webhook event request. + // + struct check_suite_event + { + string action; + gh::check_suite check_suite; + gh::repository repository; + gh::installation installation; + + explicit + check_suite_event (json::parser&); + + check_suite_event () = default; + }; + + struct installation_access_token + { + string token; + timestamp expires_at; + + explicit + installation_access_token (json::parser&); + + installation_access_token () = default; + }; + + ostream& + operator<< (ostream&, const check_suite&); + + ostream& + operator<< (ostream&, const repository&); + + ostream& + operator<< (ostream&, const installation&); + + ostream& + operator<< (ostream&, const check_suite_event&); + + ostream& + operator<< (ostream&, const installation_access_token&); + } + class ci_github: public handler { public: @@ -29,14 +124,27 @@ namespace brep handle (request&, response&); virtual const cli::options& - cli_options () const {return options::ci::description ();} + cli_options () const {return options::ci_github::description ();} private: virtual void init (cli::scanner&); + // Handle the check_suite event `requested` and `rerequested` actions. + // + bool + handle_check_suite_request (gh::check_suite_event) const; + + string + generate_jwt () const; + + // Authenticate to GitHub as an app installation. + // + gh::installation_access_token + obtain_installation_access_token (uint64_t install_id, string jwt) const; + private: - shared_ptr options_; + shared_ptr options_; }; } -- cgit v1.1