aboutsummaryrefslogtreecommitdiffstats
path: root/src/github.rs
blob: b297886b6c30d80ff2f57fa58ed0669fc8df5841 (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
extern crate mockito;

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;

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

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

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

    let response = client.post(
        format!("{}/repos/{}/{}/statuses/{}", API_URL, commit_ref.repo, commit_ref.sha)
    );
}


#[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 {
            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",
            None,
            "continuous-integration/jenkins"
        );

        mock.assert();
    }
}