diff options
| author | Teddy Wing | 2017-11-07 03:03:01 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2017-11-07 03:03:01 +0100 | 
| commit | 2f235fe9eb1e39db6d0d18818e89b63edf4e1b7b (patch) | |
| tree | c0e3a243d7ec42bb44ebb241cb61149f9a9d38d8 | |
| parent | 3958a7c917976e4e3d5e86be73f2e5289849972f (diff) | |
| download | kipper-2f235fe9eb1e39db6d0d18818e89b63edf4e1b7b.tar.bz2 | |
pull_request.rs: Parse JSON payload to create `CommitRef` struct
The test now passes, and we create our `CommitRef` struct using actual
values from the parsed JSON.
In order to get the branch name, we need the part after the last "/" in
the "ref" key.
There are a ton of `unwrap`s here, but at least it's a start and it's
working now.
| -rw-r--r-- | src/pull_request.rs | 20 | 
1 files changed, 15 insertions, 5 deletions
| diff --git a/src/pull_request.rs b/src/pull_request.rs index 75e3209..47b1eab 100644 --- a/src/pull_request.rs +++ b/src/pull_request.rs @@ -1,3 +1,6 @@ +extern crate json; + +  #[derive(Debug)]  struct CommitRef {      repo: String, @@ -6,11 +9,18 @@ struct CommitRef {  }  impl CommitRef { -    pub fn new(json: String) -> CommitRef { +    pub fn new(json_str: &str) -> CommitRef { +        let mut github_push_event = json::parse(json_str).unwrap(); + +        let commit_ref = github_push_event["ref"].take_string().unwrap(); +        let branch_parts: Vec<&str> = commit_ref +            .split('/') +            .collect(); +          CommitRef { -            repo: "test".to_string(), -            sha: "test".to_string(), -            branch: "test".to_string(), +            repo: github_push_event["repository"]["name"].take_string().unwrap(), +            sha: github_push_event["head_commit"]["id"].take_string().unwrap(), +            branch: branch_parts.last().unwrap().to_string(),          }      }  } @@ -184,7 +194,7 @@ mod tests {              "type": "User",              "site_admin": false            } -        }"#.to_string(); +        }"#;          let commit_ref = CommitRef::new(payload); | 
