From afe029ca9a29fc73935ae3441aaad9e68f6ed34b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 10 Nov 2017 02:10:49 +0100 Subject: main(): Make endpoint to handle GitHub webhook Convert our dummy test route to a real one that will handle webhooks coming from GitHub. It will parse the POST body data and create a `CommitRef` from it. That `CommitRef` then gets passed to `find_and_track_build_and_update_status()` to update the pull request status based on Jenkins' build results. A 202 response seemed apt here. Quoting https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html: > 10.2.3 202 Accepted > > The request has been accepted for processing, but the processing has > not been completed. The request might or might not eventually be acted > upon, as it might be disallowed when processing actually takes place. > There is no facility for re-sending a status code from an asynchronous > operation such as this. --- src/lib.rs | 5 +++-- src/main.rs | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index add79dd..a4cfffb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ -mod pull_request; +pub mod jenkins; +pub mod pull_request; + mod github; -mod jenkins; mod af83; diff --git a/src/main.rs b/src/main.rs index e9c50e4..e5f7235 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,33 @@ #[macro_use] extern crate rouille; +extern crate kipper; + +use std::io::Read; + +use kipper::jenkins; +use kipper::pull_request::CommitRef; fn main() { rouille::start_server("localhost:8000", move |request| { router!(request, - (GET) (/) => { - rouille::Response::text("hello world") + (POST) (/github/pull_request_event) => { + let mut body = String::new(); + + match request.data() { + None => rouille::Response::text("400 Bad Request") + .with_status_code(400), + Some(mut data) => { + try_or_400!(data.read_to_string(&mut body)); + + let commit_ref = CommitRef::new(body.as_ref()); + + jenkins::find_and_track_build_and_update_status(commit_ref); + + rouille::Response::text("202 Accepted") + .with_status_code(202) + } + } }, _ => rouille::Response::text("404 Not Found") -- cgit v1.2.3