Skip to content

Commit

Permalink
Support new_issue in autolabel config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Noratrieb committed Jul 1, 2023
1 parent 5a887e1 commit e3da523
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
74 changes: 43 additions & 31 deletions src/handlers/autolabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Expand All @@ -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 {
Expand Down

0 comments on commit e3da523

Please sign in to comment.