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

Support new_issue in autolabel config #1708

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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