소스 검색

Post error comment when command parsing fails

Mark Rousskov 6 년 전
부모
커밋
e5f3623c43
4개의 변경된 파일63개의 추가작업 그리고 4개의 파일을 삭제
  1. 16 1
      src/github.rs
  2. 15 3
      src/handlers/label.rs
  3. 31 0
      src/interactions.rs
  4. 1 0
      src/main.rs

+ 16 - 1
src/github.rs

@@ -28,22 +28,37 @@ impl Label {
 
 #[derive(Debug, serde::Deserialize)]
 pub struct Issue {
-    number: u64,
+    pub number: u64,
     title: String,
     user: User,
     labels: Vec<Label>,
     assignees: Vec<User>,
     // API URL
     repository_url: String,
+    comments_url: String,
 }
 
 #[derive(Debug, serde::Deserialize)]
 pub struct Comment {
     pub body: String,
+    pub html_url: String,
     pub user: User,
 }
 
 impl Issue {
+    pub fn post_comment(&self, client: &GithubClient, body: &str) -> Result<(), Error> {
+        #[derive(serde::Serialize)]
+        struct PostComment<'a> {
+            body: &'a str,
+        }
+        client
+            .post(&self.comments_url)
+            .json(&PostComment { body: body })
+            .send_req()
+            .context("failed to post comment")?;
+        Ok(())
+    }
+
     pub fn set_labels(&self, client: &GithubClient, mut labels: Vec<Label>) -> Result<(), Error> {
         // PUT /repos/:owner/:repo/issues/:number/labels
         // repo_url = https://api.github.com/repos/Codertocat/Hello-World

+ 15 - 3
src/handlers/label.rs

@@ -10,6 +10,7 @@
 
 use crate::{
     github::{self, GithubClient},
+    interactions::ErrorComment,
     registry::{Event, Handler},
 };
 use failure::Error;
@@ -35,9 +36,20 @@ impl Handler for LabelHandler {
         let mut input = Input::new(&event.comment.body, self.client.username());
         let deltas = match input.parse_command() {
             Command::Label(Ok(LabelCommand(deltas))) => deltas,
-            Command::Label(Err(_)) => {
-                // XXX: inform user of error
-                return Ok(());
+            Command::Label(Err(err)) => {
+                ErrorComment::new(
+                    &event.issue,
+                    format!(
+                        "Parsing label command in [comment]({}) failed: {}",
+                        event.comment.html_url, err
+                    ),
+                )
+                .post(&self.client)?;
+                failure::bail!(
+                    "label parsing failed for issue #{}, error: {:?}",
+                    event.issue.number,
+                    err
+                );
             }
             _ => return Ok(()),
         };

+ 31 - 0
src/interactions.rs

@@ -0,0 +1,31 @@
+use crate::github::{GithubClient, Issue};
+use failure::Error;
+use std::fmt::Write;
+
+pub struct ErrorComment<'a> {
+    issue: &'a Issue,
+    message: String,
+}
+
+impl<'a> ErrorComment<'a> {
+    pub fn new<T>(issue: &'a Issue, message: T) -> ErrorComment<'a>
+    where
+        T: Into<String>,
+    {
+        ErrorComment {
+            issue,
+            message: message.into(),
+        }
+    }
+
+    pub fn post(&self, client: &GithubClient) -> Result<(), Error> {
+        let mut body = String::new();
+        writeln!(body, "**Error**: {}", self.message)?;
+        writeln!(body, "")?;
+        writeln!(
+            body,
+            "Please let **`@rust-lang/release`** know if you're having trouble with this bot."
+        )?;
+        self.issue.post_comment(client, &body)
+    }
+}

+ 1 - 0
src/main.rs

@@ -14,6 +14,7 @@ mod handlers;
 mod registry;
 
 mod github;
+mod interactions;
 mod payload;
 mod team;