浏览代码

Support `new_issue` in autolabel config

Just like `new_pr`, it adds a label to every new issue that is opened.

See https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/make.20untriaged.20issues.20explicit
for the motivation.
Nilstrieb 1 年之前
父节点
当前提交
e3da5232a7
共有 2 个文件被更改,包括 45 次插入31 次删除
  1. 2 0
      src/config.rs
  2. 43 31
      src/handlers/autolabel.rs

+ 2 - 0
src/config.rs

@@ -163,6 +163,8 @@ pub(crate) struct AutolabelLabelConfig {
     pub(crate) trigger_files: Vec<String>,
     #[serde(default)]
     pub(crate) new_pr: bool,
+    #[serde(default)]
+    pub(crate) new_issue: bool,
 }
 
 #[derive(PartialEq, Eq, Debug, serde::Deserialize)]

+ 43 - 31
src/handlers/autolabel.rs

@@ -27,43 +27,45 @@ pub(super) async fn parse_input(
     // remove. Not much can be done about that currently; the before/after on
     // synchronize may be straddling a rebase, which will break diff generation.
     if event.action == IssuesAction::Opened || event.action == IssuesAction::Synchronize {
-        if let Some(diff) = event
+        let diff = event
             .issue
             .diff(&ctx.github)
             .await
             .map_err(|e| {
                 log::error!("failed to fetch diff: {:?}", e);
             })
-            .unwrap_or_default()
-        {
-            let files = files_changed(&diff);
-            let mut autolabels = Vec::new();
-            'outer: for (label, cfg) in config.labels.iter() {
+            .unwrap_or_default();
+        let files = diff.as_deref().map(files_changed);
+        let mut autolabels = Vec::new();
+
+        'outer: for (label, cfg) in config.labels.iter() {
+            let exclude_patterns: Vec<glob::Pattern> = cfg
+                .exclude_labels
+                .iter()
+                .filter_map(|label| match glob::Pattern::new(label) {
+                    Ok(exclude_glob) => Some(exclude_glob),
+                    Err(error) => {
+                        log::error!("Invalid glob pattern: {}", error);
+                        None
+                    }
+                })
+                .collect();
+
+            for label in event.issue.labels() {
+                for pat in &exclude_patterns {
+                    if pat.matches(&label.name) {
+                        // If we hit an excluded label, ignore this autolabel and check the next
+                        continue 'outer;
+                    }
+                }
+            }
+
+            if let Some(files) = &files {
                 if cfg
                     .trigger_files
                     .iter()
                     .any(|f| files.iter().any(|diff_file| diff_file.starts_with(f)))
                 {
-                    let exclude_patterns: Vec<glob::Pattern> = cfg
-                        .exclude_labels
-                        .iter()
-                        .filter_map(|label| match glob::Pattern::new(label) {
-                            Ok(exclude_glob) => Some(exclude_glob),
-                            Err(error) => {
-                                log::error!("Invalid glob pattern: {}", error);
-                                None
-                            }
-                        })
-                        .collect();
-                    for label in event.issue.labels() {
-                        for pat in &exclude_patterns {
-                            if pat.matches(&label.name) {
-                                // If we hit an excluded label, ignore this autolabel and check the next
-                                continue 'outer;
-                            }
-                        }
-                    }
-
                     autolabels.push(Label {
                         name: label.to_owned(),
                     });
@@ -74,13 +76,23 @@ pub(super) async fn parse_input(
                     });
                 }
             }
-            if !autolabels.is_empty() {
-                return Ok(Some(AutolabelInput {
-                    add: autolabels,
-                    remove: vec![],
-                }));
+
+            if event.issue.pull_request.is_none()
+                && cfg.new_issue
+                && event.action == IssuesAction::Opened
+            {
+                autolabels.push(Label {
+                    name: label.to_owned(),
+                });
             }
         }
+
+        if !autolabels.is_empty() {
+            return Ok(Some(AutolabelInput {
+                add: autolabels,
+                remove: vec![],
+            }));
+        }
     }
 
     if event.action == IssuesAction::Labeled {