Przeglądaj źródła

Implement close command

kellda 4 lat temu
rodzic
commit
05896e27ab
2 zmienionych plików z 26 dodań i 4 usunięć
  1. 17 0
      src/github.rs
  2. 9 4
      src/handlers/close.rs

+ 17 - 0
src/github.rs

@@ -593,6 +593,23 @@ impl Issue {
             .context("failed to set milestone")?;
         Ok(())
     }
+
+    pub async fn close(&self, client: &GithubClient) -> anyhow::Result<()> {
+        let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
+        #[derive(serde::Serialize)]
+        struct CloseIssue<'a> {
+            state: &'a str,
+        }
+        client
+            ._send_req(
+                client
+                    .patch(&edit_url)
+                    .json(&CloseIssue { state: "closed" }),
+            )
+            .await
+            .context("failed to close issue")?;
+        Ok(())
+    }
 }
 
 #[derive(serde::Serialize)]

+ 9 - 4
src/handlers/close.rs

@@ -1,6 +1,6 @@
 //! Allows to close an issue or a PR
 
-use crate::{config::CloseConfig, github::Event, handlers::Context};
+use crate::{config::CloseConfig, github::Event, handlers::Context, interactions::ErrorComment};
 use parser::command::close::CloseCommand;
 
 pub(super) async fn handle_command(
@@ -9,12 +9,17 @@ pub(super) async fn handle_command(
     event: &Event,
     _cmd: CloseCommand,
 ) -> anyhow::Result<()> {
+    let issue = event.issue().unwrap();
     let is_team_member = event
         .user()
         .is_team_member(&ctx.github)
         .await
         .unwrap_or(false);
-    let issue = event.issue().unwrap();
-
-    unimplemented!();
+    if !is_team_member {
+        let cmnt = ErrorComment::new(&issue, "Only team members can close issues.");
+        cmnt.post(&ctx.github).await?;
+        return Ok(());
+    }
+    issue.close(&ctx.github).await?;
+    Ok(())
 }