Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disallowed labels to notify_zulip config #1382

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub(crate) struct NotifyZulipLabelConfig {
pub(crate) message_on_remove: Option<String>,
#[serde(default)]
pub(crate) required_labels: Vec<String>,
#[serde(default)]
pub(crate) disallowed_labels: Vec<String>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
Expand Down
39 changes: 23 additions & 16 deletions src/handlers/notify_zulip.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
config::NotifyZulipConfig,
github::{IssuesAction, IssuesEvent},
github::{IssuesAction, IssuesEvent, Label},
handlers::Context,
};

Expand All @@ -21,23 +21,30 @@ pub(super) fn parse_input(
if let IssuesAction::Labeled | IssuesAction::Unlabeled = event.action {
let applied_label = &event.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;
let contains_label = |haystack: &[Label], needles: &[String]| {
for needle in needles {
let pattern = match glob::Pattern::new(needle) {
Ok(pattern) => pattern,
Err(err) => {
log::error!("Invalid glob pattern: {}", err);
continue;
}
};
if haystack.iter().any(|l| pattern.matches(&l.name)) {
return true;
}
};
if !event
.issue
.labels()
.iter()
.any(|l| pattern.matches(&l.name))
{
// Issue misses a required label, ignore this event
return Ok(None);
}
false
};

if !contains_label(event.issue.labels(), &config.required_labels) {
// Issue misses a required label, ignore this event
Mark-Simulacrum marked this conversation as resolved.
Show resolved Hide resolved
Mark-Simulacrum marked this conversation as resolved.
Show resolved Hide resolved
return Ok(None);
}

if contains_label(event.issue.labels(), &config.disallowed_labels) {
// Issue contains a disallowed label, ignore this event
return Ok(None);
}

if event.action == IssuesAction::Labeled && config.message_on_add.is_some() {
Expand Down