aboutsummaryrefslogtreecommitdiffstats
path: root/src/pull_request.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/pull_request.rs')
-rw-r--r--src/pull_request.rs71
1 files changed, 67 insertions, 4 deletions
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
+ );
+ }
}