From e0dca75f9b2de9023ecb978a0f9c8a11b905bcd1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 8 Nov 2017 04:21:52 +0100 Subject: Add rough code for `update_commit_status` This new function will make a request to the GitHub REST API to update the status of a commit using the given arguments. * Create a new `github` module for this to live in. * Uses `mockito` to check that the request was made. * The `API_URL` is necessary in order to set up 'mockito' to work properly. That pattern is lifted from an example in the crate's docs: http://lipanski.github.io/mockito/generated/mockito/index.html#example * Sort of make a POST request to the GitHub API to update the status. This doesn't actually work, though, of course, as it's incomplete. For one thing, we haven't even included the 'reqwest' library, and for another we need a way to get the GitHub owner name to build the API URL. --- src/github.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 68 insertions(+) create mode 100644 src/github.rs (limited to 'src') diff --git a/src/github.rs b/src/github.rs new file mode 100644 index 0000000..b297886 --- /dev/null +++ b/src/github.rs @@ -0,0 +1,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, + 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(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 204a45b..add79dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ mod pull_request; +mod github; mod jenkins; mod af83; -- cgit v1.2.3