Browse Source

Add a `required_labels` option for Zulip notifications

LeSeulArtichaut 4 years ago
parent
commit
c639c16398
2 changed files with 16 additions and 0 deletions
  1. 2 0
      src/config.rs
  2. 14 0
      src/handlers/notify_zulip.rs

+ 2 - 0
src/config.rs

@@ -120,6 +120,8 @@ pub(crate) struct NotifyZulipLabelConfig {
     pub(crate) topic: String,
     pub(crate) message_on_add: Option<String>,
     pub(crate) message_on_remove: Option<String>,
+    #[serde(default)]
+    pub(crate) required_labels: Vec<String>,
 }
 
 #[derive(PartialEq, Eq, Debug, serde::Deserialize)]

+ 14 - 0
src/handlers/notify_zulip.rs

@@ -30,6 +30,20 @@ impl Handler for NotifyZulipHandler {
             if let github::IssuesAction::Labeled | github::IssuesAction::Unlabeled = e.action {
                 let applied_label = &e.label.as_ref().expect("label").name;
                 if let Some(config) = config.and_then(|c| c.labels.get(applied_label)) {
+                    for label in &config.required_labels {
+                        let pattern =  match glob::Pattern::new(label) {
+                            Ok(pattern) => pattern,
+                            Err(err) => {
+                                log::error!("Invalid glob pattern: {}", err);
+                                continue;
+                            }
+                        };
+                        if !e.issue.labels().iter().any(|l| pattern.matches(&l.name)) {
+                            // Issue misses a required label, ignore this event
+                            return Ok(None);
+                        }
+                    }
+
                     if e.action == github::IssuesAction::Labeled && config.message_on_add.is_some() {
                         return Ok(Some(NotifyZulipInput {
                             notification_type: NotificationType::Labeled,