From daf3da39dea7a0558b963c81ce92e9a3781b7e5b Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:48:00 +0200 Subject: [PATCH 1/3] rustfmt --- src/actions.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index ec518b0e9..d2735f74c 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -57,8 +57,7 @@ impl<'a> Action for Step<'a> { let mut context = Context::new(); let mut results = HashMap::new(); - for Query { repos, queries} in &self.actions { - + for Query { repos, queries } in &self.actions { for repo in repos { let repository = Repository { full_name: repo.to_string(), @@ -118,7 +117,6 @@ impl<'a> Action for Step<'a> { match count { Ok(count) => { - let result = if let Some(value) = context.get(*name) { value.as_u64().unwrap() + count as u64 } else { From c0b0eeb748aa9883981a034010a2ef563a146ced Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:50:12 +0200 Subject: [PATCH 2/3] Add review_submitted handler which changes labels after a review When an assigned reviewer submits a review to a PR, automatically remove waiting-on-review and add waiting-on-author label --- src/config.rs | 8 ++++++ src/handlers.rs | 19 +++++++++++++-- src/handlers/review_submitted.rs | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/handlers/review_submitted.rs diff --git a/src/config.rs b/src/config.rs index a10b943cd..248ac546c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,6 +28,7 @@ pub(crate) struct Config { pub(crate) autolabel: Option, pub(crate) notify_zulip: Option, pub(crate) github_releases: Option, + pub(crate) review_submitted: Option, } #[derive(PartialEq, Eq, Debug, serde::Deserialize)] @@ -142,6 +143,12 @@ pub(crate) struct GlacierConfig {} #[derive(PartialEq, Eq, Debug, serde::Deserialize)] pub(crate) struct CloseConfig {} +#[derive(PartialEq, Eq, Debug, serde::Deserialize)] +pub(crate) struct ReviewSubmittedConfig { + pub(crate) review_labels: Vec, + pub(crate) reviewed_label: String, +} + pub(crate) async fn get(gh: &GithubClient, repo: &str) -> Result, ConfigurationError> { if let Some(config) = get_cached_config(repo) { log::trace!("returning config for {} from cache", repo); @@ -290,6 +297,7 @@ mod tests { autolabel: None, notify_zulip: None, github_releases: None, + review_submitted: None, } ); } diff --git a/src/handlers.rs b/src/handlers.rs index 40639fff0..e1100fcd2 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -36,6 +36,7 @@ mod notify_zulip; mod ping; mod prioritize; mod relabel; +mod review_submitted; mod rustc_commits; pub async fn handle(ctx: &Context, event: &Event) -> Vec { @@ -74,6 +75,20 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec { ); } + if let Some(config) = config + .as_ref() + .ok() + .and_then(|c| c.review_submitted.as_ref()) + { + if let Err(e) = review_submitted::handle(ctx, event, config).await { + log::error!( + "failed to process event {:?} with review_submitted handler: {:?}", + event, + e + ) + } + } + if let Some(ghr_config) = config .as_ref() .ok() @@ -120,9 +135,9 @@ macro_rules! issue_handlers { } } -// Handle events that happend on issues +// Handle events that happened on issues // -// This is for events that happends only on issues (e.g. label changes). +// This is for events that happen only on issues (e.g. label changes). // Each module in the list must contain the functions `parse_input` and `handle_input`. issue_handlers! { autolabel, diff --git a/src/handlers/review_submitted.rs b/src/handlers/review_submitted.rs new file mode 100644 index 000000000..00f924367 --- /dev/null +++ b/src/handlers/review_submitted.rs @@ -0,0 +1,42 @@ +use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label}; +use crate::{config::ReviewSubmittedConfig, github::Event, handlers::Context}; + +pub(crate) async fn handle( + ctx: &Context, + event: &Event, + config: &ReviewSubmittedConfig, +) -> anyhow::Result<()> { + if let Event::IssueComment( + event + @ + IssueCommentEvent { + action: IssueCommentAction::Created, + issue: Issue { + pull_request: Some(_), + .. + }, + .. + }, + ) = event + { + if event.issue.assignees.contains(&event.comment.user) { + let labels = event + .issue + .labels() + .iter() + // Remove review related labels + .filter(|label| !config.review_labels.contains(&label.name)) + .cloned() + // Add waiting on author label + .chain(std::iter::once(Label { + name: config.reviewed_label.clone(), + })); + event + .issue + .set_labels(&ctx.github, labels.collect()) + .await?; + } + } + + Ok(()) +} From 5490a59beaba47080467d9db1160c0faabd05088 Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Sun, 6 Jun 2021 19:55:05 +0200 Subject: [PATCH 3/3] Only add waiting on author label if the reviewer requested changes --- src/github.rs | 12 ++++++++++++ src/handlers/review_submitted.rs | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/github.rs b/src/github.rs index 95f7ee622..3f67cb24c 100644 --- a/src/github.rs +++ b/src/github.rs @@ -292,6 +292,18 @@ pub struct Comment { pub user: User, #[serde(alias = "submitted_at")] // for pull request reviews pub updated_at: chrono::DateTime, + #[serde(rename = "state")] + pub pr_review_state: PullRequestReviewState, +} + +#[derive(Debug, serde::Deserialize, Eq, PartialEq)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum PullRequestReviewState { + Approved, + ChangesRequested, + Commented, + Dismissed, + Pending, } fn opt_string<'de, D>(deserializer: D) -> Result diff --git a/src/handlers/review_submitted.rs b/src/handlers/review_submitted.rs index 00f924367..b144ae5cf 100644 --- a/src/handlers/review_submitted.rs +++ b/src/handlers/review_submitted.rs @@ -1,4 +1,4 @@ -use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label}; +use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label, PullRequestReviewState}; use crate::{config::ReviewSubmittedConfig, github::Event, handlers::Context}; pub(crate) async fn handle( @@ -19,6 +19,10 @@ pub(crate) async fn handle( }, ) = event { + if event.comment.pr_review_state != PullRequestReviewState::ChangesRequested { + return Ok(()); + } + if event.issue.assignees.contains(&event.comment.user) { let labels = event .issue