aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 58829092ef1ba0211bb5fdc691db6d050177de67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#[macro_use]
extern crate rouille;

extern crate kipper;

use std::io::Read;

use kipper::jenkins;
use kipper::pull_request::CommitRef;

fn internal_server_error() -> rouille::Response {
    rouille::Response::text("500 Internal Server Error")
        .with_status_code(500)
}

fn main() {
    rouille::start_server("localhost:8000", move |request| {
        router!(request,
            (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 = match CommitRef::new(body.as_ref()) {
                            Ok(cr) => cr,
                            Err(_) => return internal_server_error(),
                        };

                        match jenkins::find_and_track_build_and_update_status(commit_ref) {
                            Ok(_) => {},
                            Err(_) => return internal_server_error(),
                        };

                        rouille::Response::text("202 Accepted")
                            .with_status_code(202)
                    }
                }
            },

            _ => rouille::Response::text("404 Not Found")
                    .with_status_code(404)
        )
    });
}