浏览代码

Reject assignment on a closed PR.

Eric Huss 2 年之前
父节点
当前提交
210178d511
共有 3 个文件被更改,包括 20 次插入0 次删除
  1. 13 0
      src/github.rs
  2. 6 0
      src/handlers/assign.rs
  3. 1 0
      src/handlers/assign/tests/tests_candidates.rs

+ 13 - 0
src/github.rs

@@ -297,6 +297,15 @@ pub struct Issue {
     /// The head commit for a PR (the branch from the source repo).
     #[serde(default)]
     pub head: Option<CommitBase>,
+    /// Whether it is open or closed.
+    pub state: IssueState,
+}
+
+#[derive(Debug, serde::Deserialize, Eq, PartialEq)]
+#[serde(rename_all = "snake_case")]
+pub enum IssueState {
+    Open,
+    Closed,
 }
 
 /// Contains only the parts of `Issue` that are needed for turning the issue title into a Zulip
@@ -467,6 +476,10 @@ impl Issue {
         self.pull_request.is_some()
     }
 
+    pub fn is_open(&self) -> bool {
+        self.state == IssueState::Open
+    }
+
     pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
         let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
         let comment = client.json(client.get(&comment_url)).await?;

+ 6 - 0
src/handlers/assign.rs

@@ -414,6 +414,12 @@ pub(super) async fn handle_command(
 
     let issue = event.issue().unwrap();
     if issue.is_pr() {
+        if !issue.is_open() {
+            issue
+                .post_comment(&ctx.github, "Assignment is not allowed on a closed PR.")
+                .await?;
+            return Ok(());
+        }
         let username = match cmd {
             AssignCommand::Own => event.user().login.clone(),
             AssignCommand::User { username } => username,

+ 1 - 0
src/handlers/assign/tests/tests_candidates.rs

@@ -65,6 +65,7 @@ fn generic_issue(author: &str, repo: &str) -> serde_json::Value {
         "labels": [],
         "assignees": [],
         "comments_url": format!("https://api.github.com/repos/{repo}/pull/1234/comments"),
+        "state": "open",
     })
 }