Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
11 changes: 6 additions & 5 deletions src/handlers/autolabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
config::AutolabelConfig,
github::{IssuesAction, IssuesEvent, Label},
handlers::Context,
utils::ModifiedPathMatcher,
};
use anyhow::Context as _;
use tracing as log;
Expand Down Expand Up @@ -87,11 +88,11 @@ pub(super) async fn parse_input(
// This a PR with modified files.

// Add the matching labels for the modified files paths
if cfg.trigger_files.iter().any(|f| {
files
.iter()
.any(|file_diff| file_diff.filename.starts_with(f))
}) {
let matcher = ModifiedPathMatcher::new(&cfg.trigger_files);
if files
.iter()
.any(|file_diff| matcher.is_match(&file_diff.filename))
{
autolabels.push(Label {
name: label.to_owned(),
});
Expand Down
90 changes: 60 additions & 30 deletions src/handlers/check_commits/validate_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//! changes are a valid configuration file.

use crate::{
config::{CONFIG_FILE_NAME, MentionsEntryConfig, MentionsEntryType},
config::{CONFIG_FILE_NAME, Config, MentionsEntryConfig, MentionsEntryType},
github::FileDiff,
handlers::{Context, IssuesEvent},
utils::{ModifiedPathMatcher, PathMatcherError},
};
use anyhow::{Context as _, bail};

Expand Down Expand Up @@ -55,43 +56,72 @@ pub(super) async fn validate_config(
`````",
)))
}
Ok(config) => {
// Error if `[assign.owners]` is not empty (ie auto-assign) and the custom welcome message for assignee isn't set.
if let Some(assign) = config.assign
&& !assign.owners.is_empty()
&& let Some(custom_messages) = &assign.custom_messages
&& custom_messages.auto_assign_someone.is_none()
{
return Ok(Some(
Ok(config) => match validate_parsed_config(&config) {
Ok(()) => Ok(None),
Err(err) => Ok(Some(err)),
},
}
}

fn validate_parsed_config(config: &Config) -> Result<(), String> {
// Error if `[assign.owners]` is not empty (ie auto-assign) and the custom welcome message for assignee isn't set.
if let Some(assign) = &config.assign
&& !assign.owners.is_empty()
&& let Some(custom_messages) = &assign.custom_messages
&& custom_messages.auto_assign_someone.is_none()
{
return Err("Invalid `triagebot.toml`:\n\
`[assign.owners]` is populated but `[assign.custom_messages.auto-assign-someone]` \
is not set!"
.to_string());
}

// Validate patterns everywhere we allow glob matching
let validate_pattern =
|pat: &str, location: &str| match ModifiedPathMatcher::validate_entry(&pat) {
Ok(()) => Ok(()),
Err(PathMatcherError::Glob(err)) => {
return Err(format!(
"Invalid `triagebot.toml`:\n\
`[assign.owners]` is populated but `[assign.custom_messages.auto-assign-someone]` is not set!".to_string()
{location} has an invalid glob syntax: {err}"
));
}
Err(PathMatcherError::NonRelativePath) => {
return Err(format!(
"Invalid `triagebot.toml`:\n\
{location} has an invalid pattern: path must be \
relative (remove the `/` at the start)"
));
}
};

// Error if one the mentions entry is not a valid glob.
if let Some(mentions) = config.mentions {
for (entry, MentionsEntryConfig { type_, .. }) in mentions.entries {
if type_ == MentionsEntryType::Filename {
if let Err(err) = globset::Glob::new(&entry) {
return Ok(Some(format!(
"Invalid `triagebot.toml`:\n\
`[mentions.\"{entry}\"]` has an invalid glob syntax: {err}"
)));
}

if entry.starts_with('/') {
return Ok(Some(format!(
"Invalid `triagebot.toml`:\n\
`[mentions.\"{entry}\"]` has an invalid pattern: path must be relative (remove the `/` at the start)"
)));
}
}
}
if let Some(mentions) = &config.mentions {
for (entry, MentionsEntryConfig { type_, .. }) in &mentions.entries {
if type_ == &MentionsEntryType::Filename {
validate_pattern(entry, &format!("`[mentions.\"{entry}\"]`"))?;
}
}
}

Ok(None)
if let Some(autolabel) = &config.autolabel {
for (_label, cfg) in &autolabel.labels {
for pat in &cfg.trigger_files {
validate_pattern(pat, &format!("`autolabel.trigger_files` item `{pat}`"))?;
}
}
}

if let Some(rendered_link) = &config.rendered_link {
for pat in &rendered_link.trigger_files {
validate_pattern(pat, &format!("`rendered_link.trigger_files` item `{pat}`"))?;
}

for pat in &rendered_link.exclude_files {
validate_pattern(pat, &format!("`rendered_link.exclude_files` item `{pat}`"))?;
}
}

Ok(())
}

/// Helper to translate a toml span to a `(line_no, col_no)` (1-based).
Expand Down
Loading