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 support for adding labels based on files changed #1321

Merged
merged 1 commit into from
Dec 12, 2021
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 @@ -117,6 +117,8 @@ pub(crate) struct AutolabelLabelConfig {
pub(crate) trigger_labels: Vec<String>,
#[serde(default)]
pub(crate) exclude_labels: Vec<String>,
#[serde(default)]
pub(crate) trigger_files: Vec<String>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
Expand Down
46 changes: 46 additions & 0 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,52 @@ pub struct IssuesEvent {
pub repository: Repository,
/// Some if action is IssuesAction::Labeled, for example
pub label: Option<Label>,

// These fields are the sha fields before/after a synchronize operation,
// used to compute the diff between these two commits.
#[serde(default)]
before: Option<String>,
#[serde(default)]
after: Option<String>,

#[serde(default)]
base: Option<CommitBase>,
#[serde(default)]
head: Option<CommitBase>,
}

#[derive(Default, Clone, Debug, serde::Deserialize)]
pub struct CommitBase {
sha: String,
}

impl IssuesEvent {
/// Returns the diff in this event, for Open and Synchronize events for now.
pub async fn diff_between(&self, client: &GithubClient) -> anyhow::Result<Option<String>> {
let (before, after) = if self.action == IssuesAction::Synchronize {
(
self.before.clone().unwrap_or_default(),
self.after.clone().unwrap_or_default(),
)
} else if self.action == IssuesAction::Opened {
(
self.base.clone().unwrap_or_default().sha,
self.head.clone().unwrap_or_default().sha,
)
} else {
return Ok(None);
};

let mut req = client.get(&format!(
"{}/compare/{}...{}",
self.issue.repository().url(),
before,
after
));
req = req.header("Accept", "application/vnd.github.v3.diff");
let diff = client.send_req(req).await?;
Ok(Some(String::from(String::from_utf8_lossy(&diff))))
}
}

#[derive(Debug, serde::Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ macro_rules! issue_handlers {
errors: &mut Vec<HandlerError>,
) {
$(
match $name::parse_input(ctx, event, config.$name.as_ref()) {
match $name::parse_input(ctx, event, config.$name.as_ref()).await {
Err(err) => errors.push(HandlerError::Message(err)),
Ok(Some(input)) => {
if let Some(config) = &config.$name {
Expand Down
48 changes: 46 additions & 2 deletions src/handlers/autolabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,55 @@ pub(super) struct AutolabelInput {
labels: Vec<Label>,
}

pub(super) fn parse_input(
_ctx: &Context,
pub(super) async fn parse_input(
ctx: &Context,
event: &IssuesEvent,
config: Option<&AutolabelConfig>,
) -> Result<Option<AutolabelInput>, String> {
if let Some(diff) = event
.diff_between(&ctx.github)
.await
.map_err(|e| {
log::error!("failed to fetch diff: {:?}", e);
})
.unwrap_or_default()
{
if let Some(config) = config {
let mut files = Vec::new();
for line in diff.lines() {
// mostly copied from highfive
if line.starts_with("diff --git ") {
let parts = line[line.find(" b/").unwrap() + " b/".len()..].split("/");
let path = parts.collect::<Vec<_>>().join("/");
if !path.is_empty() {
files.push(path);
}
}
}
let mut autolabels = Vec::new();
for trigger_file in files {
if trigger_file.is_empty() {
// TODO: when would this be true?
continue;
}
for (label, cfg) in config.labels.iter() {
if cfg
.trigger_files
.iter()
.any(|f| trigger_file.starts_with(f))
{
autolabels.push(Label {
name: label.to_owned(),
});
}
}
if !autolabels.is_empty() {
return Ok(Some(AutolabelInput { labels: autolabels }));
}
}
}
}

if event.action == IssuesAction::Labeled {
if let Some(config) = config {
let mut autolabels = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/major_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum Invocation {
Rename { prev_issue: ZulipGitHubReference },
}

pub(super) fn parse_input(
pub(super) async fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
_config: Option<&MajorChangeConfig>,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/notify_zulip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(super) enum NotificationType {
Reopened,
}

pub(super) fn parse_input(
pub(super) async fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
config: Option<&NotifyZulipConfig>,
Expand Down