diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 19 | ||||
| -rw-r--r-- | src/pull_request.rs | 71 | 
2 files changed, 84 insertions, 6 deletions
| diff --git a/src/main.rs b/src/main.rs index 9ac9570..12d71eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@  // along with Kipper. If not, see <http://www.gnu.org/licenses/>.  extern crate getopts; +extern crate json;  #[macro_use]  extern crate log;  extern crate stderrlog; @@ -32,7 +33,7 @@ use std::time::Duration;  use getopts::Options;  use kipper::jenkins; -use kipper::pull_request::CommitRef; +use kipper::pull_request::{CommitRef, pull_request_opened_or_synchronized};  const DEFAULT_PORT: u16 = 8000; @@ -129,7 +130,21 @@ 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() +                            }, +                        }; + +                        if !pull_request_opened_or_synchronized(json.clone()) { +                            return rouille::Response::text("No status update needed.") +                                .with_status_code(200) +                        } + +                        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 fb06544..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<CommitRef, Box<Error>> { -        let mut github_push_event = json::parse(json_str)?; - +    pub fn new( +        mut github_push_event: json::JsonValue +    ) -> Result<CommitRef, Box<Error>> {          Ok(              CommitRef {                  owner: github_push_event["pull_request"]["head"]["repo"]["owner"]["login"].take_string().unwrap_or_default(), @@ -42,6 +42,18 @@ impl CommitRef {      }  } +pub fn pull_request_opened_or_synchronized( +    mut github_push_event: json::JsonValue +) -> bool { +    let action = github_push_event["action"].take_string().unwrap_or_default(); + +    if action == "opened" || action == "synchronize" { +        return true +    } + +    false +} +  #[cfg(test)]  mod tests { @@ -465,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"); @@ -473,4 +488,52 @@ mod tests {          assert_eq!(commit_ref.sha, "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c");          assert_eq!(commit_ref.branch, "changes");      } + +    #[test] +    fn pull_request_opened_or_synchronized_returns_true_when_opened() { +        let payload = r#"{ +            "action": "opened" +        }"#; + +        let json = json::parse(payload) +            .expect("Failed to parse payload."); + +        assert_eq!( +            pull_request_opened_or_synchronized(json), +            true +        ); +    } + +    #[test] +    fn pull_request_opened_or_synchronized_returns_true_when_synchronized() { +        let payload = r#"{ +            "action": "synchronize" +        }"#; + +        let json = json::parse(payload) +            .expect("Failed to parse payload."); + +        assert_eq!( +            pull_request_opened_or_synchronized(json), +            true +        ); +    } + +    #[test] +    fn pull_request_opened_or_synchronized_returns_false_when_not_opened_or_synchronized() { +        // "assigned", "unassigned", "review_requested", +        // "review_request_removed", "labeled", "unlabeled", "opened", +        // "edited", "closed", or "reopened" +        let payload = r#"{ +            "action": "review_requested" +        }"#; + +        let json = json::parse(payload) +            .expect("Failed to parse payload."); + +        assert_eq!( +            pull_request_opened_or_synchronized(json), +            false +        ); +    }  } | 
