瀏覽代碼

add support for the push and create events

Pietro Albini 4 年之前
父節點
當前提交
7c7260dd78
共有 4 個文件被更改,包括 65 次插入0 次删除
  1. 36 0
      src/github.rs
  2. 4 0
      src/handlers.rs
  3. 1 0
      src/handlers/notification.rs
  4. 24 0
      src/lib.rs

+ 36 - 0
src/github.rs

@@ -847,61 +847,97 @@ pub enum QueryKind {
     Count,
 }
 
+#[derive(Debug, serde::Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum CreateKind {
+    Branch,
+    Tag,
+}
+
+#[derive(Debug, serde::Deserialize)]
+pub struct CreateEvent {
+    pub ref_type: CreateKind,
+    repository: Repository,
+    sender: User,
+}
+
+#[derive(Debug, serde::Deserialize)]
+pub struct PushEvent {
+    repository: Repository,
+    sender: User,
+}
+
 #[derive(Debug)]
 pub enum Event {
+    Create(CreateEvent),
     IssueComment(IssueCommentEvent),
     Issue(IssuesEvent),
+    Push(PushEvent),
 }
 
 impl Event {
     pub fn repo_name(&self) -> &str {
         match self {
+            Event::Create(event) => &event.repository.full_name,
             Event::IssueComment(event) => &event.repository.full_name,
             Event::Issue(event) => &event.repository.full_name,
+            Event::Push(event) => &event.repository.full_name,
         }
     }
 
     pub fn issue(&self) -> Option<&Issue> {
         match self {
+            Event::Create(_) => None,
             Event::IssueComment(event) => Some(&event.issue),
             Event::Issue(event) => Some(&event.issue),
+            Event::Push(_) => None,
         }
     }
 
     /// This will both extract from IssueComment events but also Issue events
     pub fn comment_body(&self) -> Option<&str> {
         match self {
+            Event::Create(_) => None,
             Event::Issue(e) => Some(&e.issue.body),
             Event::IssueComment(e) => Some(&e.comment.body),
+            Event::Push(_) => None,
         }
     }
 
     /// This will both extract from IssueComment events but also Issue events
     pub fn comment_from(&self) -> Option<&str> {
         match self {
+            Event::Create(_) => None,
             Event::Issue(e) => Some(&e.changes.as_ref()?.body.from),
             Event::IssueComment(e) => Some(&e.changes.as_ref()?.body.from),
+            Event::Push(_) => None,
         }
     }
 
     pub fn html_url(&self) -> Option<&str> {
         match self {
+            Event::Create(_) => None,
             Event::Issue(e) => Some(&e.issue.html_url),
             Event::IssueComment(e) => Some(&e.comment.html_url),
+            Event::Push(_) => None,
         }
     }
 
     pub fn user(&self) -> &User {
         match self {
+            Event::Create(e) => &e.sender,
             Event::Issue(e) => &e.issue.user,
             Event::IssueComment(e) => &e.comment.user,
+            Event::Push(e) => &e.sender,
         }
     }
 
     pub fn time(&self) -> chrono::DateTime<FixedOffset> {
         match self {
+            Event::Create(_) => todo!(),
             Event::Issue(e) => e.issue.created_at.into(),
             Event::IssueComment(e) => e.comment.updated_at.into(),
+            Event::Push(_) => todo!(),
         }
     }
 }

+ 4 - 0
src/handlers.rs

@@ -134,6 +134,10 @@ macro_rules! command_handlers {
                     log::debug!("skipping event, comment was {:?}", e.action);
                     return;
                 }
+                Event::Push(_) | Event::Create(_) => {
+                    log::debug!("skipping unsupported event");
+                    return;
+                }
             }
 
             let input = Input::new(&body, &ctx.username);

+ 1 - 0
src/handlers/notification.rs

@@ -33,6 +33,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
     let short_description = match event {
         Event::Issue(e) => e.issue.title.clone(),
         Event::IssueComment(e) => format!("Comment on {}", e.issue.title),
+        Event::Push(_) | Event::Create(_) => return Ok(()),
     };
 
     let mut caps = parser::get_mentions(body)

+ 24 - 0
src/lib.rs

@@ -30,6 +30,8 @@ pub enum EventName {
     PullRequestReviewComment,
     IssueComment,
     Issue,
+    Push,
+    Create,
     Other,
 }
 
@@ -42,6 +44,8 @@ impl std::str::FromStr for EventName {
             "issue_comment" => EventName::IssueComment,
             "pull_request" => EventName::PullRequest,
             "issues" => EventName::Issue,
+            "push" => EventName::Push,
+            "create" => EventName::Create,
             _ => EventName::Other,
         })
     }
@@ -58,6 +62,8 @@ impl fmt::Display for EventName {
                 EventName::IssueComment => "issue_comment",
                 EventName::Issue => "issues",
                 EventName::PullRequest => "pull_request",
+                EventName::Push => "push",
+                EventName::Create => "create",
                 EventName::Other => "other",
             }
         )
@@ -151,6 +157,24 @@ pub async fn webhook(
 
             github::Event::Issue(payload)
         }
+        EventName::Push => {
+            let payload = deserialize_payload::<github::PushEvent>(&payload)
+                .with_context(|| format!("{:?} failed to deserialize", event))
+                .map_err(anyhow::Error::from)?;
+
+            log::info!("handling push event {:?}", payload);
+
+            github::Event::Push(payload)
+        }
+        EventName::Create => {
+            let payload = deserialize_payload::<github::CreateEvent>(&payload)
+                .with_context(|| format!("{:?} failed to deserialize", event))
+                .map_err(anyhow::Error::from)?;
+
+            log::info!("handling create event {:?}", payload);
+
+            github::Event::Create(payload)
+        }
         // Other events need not be handled
         EventName::Other => {
             return Ok(false);