From 205fc3b71bd44067685c8517e7d126e3574827e1 Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Fri, 17 Nov 2017 00:54:27 +0100
Subject: CommitRef::new(): Take a `JsonValue` instead of an `&str`
We expect to have pre-parsed the JSON as this function will get called
after `pull_request_opened_or_synchronized()`. To avoid doing the same
JSON parsing work twice, take a `JsonValue` argument in both functions.
---
src/main.rs | 12 +++++++++++-
src/pull_request.rs | 11 +++++++----
2 files changed, 18 insertions(+), 5 deletions(-)
(limited to 'src')
diff --git a/src/main.rs b/src/main.rs
index 9ac9570..3bb2dbb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,6 +16,7 @@
// along with Kipper. If not, see .
extern crate getopts;
+extern crate json;
#[macro_use]
extern crate log;
extern crate stderrlog;
@@ -129,7 +130,16 @@ fn main() {
let mut body = String::new();
try_or_400!(data.read_to_string(&mut body));
- let commit_ref = match CommitRef::new(body.as_ref()) {
+ let json = match json::parse(body.as_ref()) {
+ Ok(j) => j,
+ Err(e) => {
+ error!("{}", e.to_string());
+
+ return internal_server_error()
+ },
+ };
+
+ let commit_ref = match CommitRef::new(json) {
Ok(cr) => cr,
Err(e) => {
error!("{}", e.to_string());
diff --git a/src/pull_request.rs b/src/pull_request.rs
index c4081f4..b110ea4 100644
--- a/src/pull_request.rs
+++ b/src/pull_request.rs
@@ -28,9 +28,9 @@ pub struct CommitRef {
}
impl CommitRef {
- pub fn new(json_str: &str) -> Result> {
- let mut github_push_event = json::parse(json_str)?;
-
+ pub fn new(
+ mut github_push_event: json::JsonValue
+ ) -> Result> {
Ok(
CommitRef {
owner: github_push_event["pull_request"]["head"]["repo"]["owner"]["login"].take_string().unwrap_or_default(),
@@ -477,7 +477,10 @@ mod tests {
}
}"#;
- let commit_ref = CommitRef::new(payload)
+ let json = json::parse(payload)
+ .expect("Failed to parse payload.");
+
+ let commit_ref = CommitRef::new(json)
.expect("Failed to create CommitRef from payload");
assert_eq!(commit_ref.owner, "baxterthehacker");
--
cgit v1.2.3