aboutsummaryrefslogtreecommitdiffstats
path: root/src/github.rs
blob: f24d1f02a182c91605b0390881e7efbe70091aa5 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
extern crate mockito;
extern crate reqwest;

use std::collections::HashMap;
use std::error::Error;
use std::fmt;

use self::reqwest::header::{Accept, Authorization, Bearer, qitem};

use pull_request::CommitRef;

#[cfg(not(test))]
const API_URL: &'static str = "https://api.github.com";

#[cfg(test)]
const API_URL: &'static str = mockito::SERVER_URL;

pub enum CommitStatus {
    Error,
    Failure,
    Pending,
    Success,
}

impl fmt::Display for CommitStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            CommitStatus::Error => write!(f, "error"),
            CommitStatus::Failure => write!(f, "failure"),
            CommitStatus::Pending => write!(f, "pending"),
            CommitStatus::Success => write!(f, "success"),
        }
    }
}

pub fn update_commit_status(
    commit_ref: &CommitRef,
    state: &CommitStatus,
    target_url: String,
    description: Option<String>,
    context: String,
) -> Result<(), Box<Error>> {
    let client = reqwest::Client::new();

    let mut params = HashMap::new();
    params.insert("state", state.to_string());
    params.insert("target_url", target_url);
    params.insert("context", context);

    if let Some(d) = description {
        params.insert("description", d);
    }

    client.post(
            &format!(
                "{}/repos/{}/{}/statuses/{}",
                API_URL,
                commit_ref.owner,
                commit_ref.repo,
                commit_ref.sha
            )
        )
        .header(
            Accept(
                vec![qitem("application/vnd.github.v3+json".parse()?)]
            )
        )
        .header(
            Authorization(
                Bearer {
                    token: "token".to_owned()
                }
            )
        )
        .json(&params)
        .send()?;

    Ok(())
}


#[cfg(test)]
mod tests {
    use self::mockito::mock;

    use super::*;

    #[test]
    fn update_commit_status_makes_a_request_to_github() {
        let mock = mock("POST", "/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e")
            .with_status(201)
            .create();

        let commit_ref = CommitRef {
            owner: "octocat".to_string(),
            repo: "Hello-World".to_string(),
            sha: "6dcb09b5b57875f334f61aebed695e2e4193db5e".to_string(),
            branch: "not-used".to_string(),
        };

        update_commit_status(
            &commit_ref,
            &CommitStatus::Success,
            "https://jenkins.example.com/job/octocat/3".to_string(),
            None,
            "continuous-integration/jenkins".to_string()
        );

        mock.assert();
    }
}