فهرست منبع

Implement assignment

Mark Rousskov 6 سال پیش
والد
کامیت
9e9f2e9fd8
3فایلهای تغییر یافته به همراه28 افزوده شده و 15 حذف شده
  1. 21 3
      src/github.rs
  2. 3 8
      src/handlers/assign.rs
  3. 4 4
      src/handlers/label.rs

+ 21 - 3
src/github.rs

@@ -47,7 +47,6 @@ impl Issue {
     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
-        // Might need `Accept: application/vnd.github.symmetra-preview+json` for emoji and descriptions
         let url = format!(
             "{repo_url}/issues/{number}/labels",
             repo_url = self.repository_url,
@@ -75,9 +74,24 @@ impl Issue {
         &self.labels
     }
 
-    #[allow(unused)]
     pub fn add_assignee(&self, client: &GithubClient, user: &str) -> Result<(), Error> {
-        unimplemented!()
+        let url = format!(
+            "{repo_url}/issues/{number}/assignees",
+            repo_url = self.repository_url,
+            number = self.number
+        );
+
+        #[derive(serde::Serialize)]
+        struct AssigneeReq<'a> {
+            assignees: &'a [&'a str],
+        }
+        client
+            .post(&url)
+            .json(&AssigneeReq { assignees: &[user] })
+            .send_req()
+            .context("failed to add assignee")?;
+
+        Ok(())
     }
 }
 
@@ -116,6 +130,10 @@ impl GithubClient {
         self.client.get(url).configure(self)
     }
 
+    fn post(&self, url: &str) -> RequestBuilder {
+        self.client.post(url).configure(self)
+    }
+
     fn put(&self, url: &str) -> RequestBuilder {
         self.client.put(url).configure(self)
     }

+ 3 - 8
src/handlers/assign.rs

@@ -13,7 +13,7 @@
 //! been given for the past 2 weeks, the bot will de-assign the user. They can once more claim
 //! the issue if necessary.
 //!
-//! Assign users with `assign: @gh-user` or `@bot claim` (self-claim).
+//! Assign users with `/assign @gh-user` or `/claim` (self-claim).
 
 use crate::{
     github::GithubClient,
@@ -38,14 +38,10 @@ impl Handler for AssignmentHandler {
         };
 
         lazy_static! {
-            static ref RE_ASSIGN: Regex = Regex::new(r"\bassign: @(\S+)").unwrap();
-            static ref RE_CLAIM: Regex =
-                Regex::new(&format!(r"\b@{} claim\b", crate::BOT_USER_NAME)).unwrap();
+            static ref RE_ASSIGN: Regex = Regex::new(r"\b/assign @(\S+)").unwrap();
+            static ref RE_CLAIM: Regex = Regex::new(r"\b/claim\b").unwrap();
         }
 
-        // XXX: Handle updates to the comment specially to avoid double-queueing or double-assigning
-        // and other edge cases.
-
         if RE_CLAIM.is_match(&event.comment.body) {
             event
                 .issue
@@ -59,7 +55,6 @@ impl Handler for AssignmentHandler {
         // TODO: Enqueue a check-in in two weeks.
         // TODO: Post a comment documenting the biweekly check-in? Maybe just give them two weeks
         //       without any commentary from us.
-        // TODO: How do we handle `claim`/`assign:` if someone's already assigned? Error out?
 
         Ok(())
     }

+ 4 - 4
src/handlers/label.rs

@@ -1,10 +1,10 @@
 //! Purpose: Allow any user to modify issue labels on GitHub via comments.
 //!
 //! The current syntax allows adding labels (+labelname or just labelname) following the
-//! `label:` prefix. Users can also remove labels with -labelname.
+//! `/label` prefix. Users can also remove labels with -labelname.
 //!
-//! No verification is currently attempted of the added labels (only currently present labels
-//! can be removed). XXX: How does this affect users workflow?
+//! Labels are checked against the labels in the project; the bot does not support creating new
+//! labels.
 //!
 //! There will be no feedback beyond the label change to reduce notification noise.
 
@@ -31,7 +31,7 @@ impl Handler for LabelHandler {
         };
 
         lazy_static! {
-            static ref LABEL_RE: Regex = Regex::new(r#"\blabel: (\S+\s*)+"#).unwrap();
+            static ref LABEL_RE: Regex = Regex::new(r#"\b/label (\S+\s*)+"#).unwrap();
         }
 
         let mut issue_labels = event.issue.labels().to_owned();