Skip to content

Commit

Permalink
Add a handler for requested reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Oct 15, 2023
1 parent 559e1d3 commit 7a0a772
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) struct Config {
pub(crate) notify_zulip: Option<NotifyZulipConfig>,
pub(crate) github_releases: Option<GitHubReleasesConfig>,
pub(crate) review_submitted: Option<ReviewSubmittedConfig>,
pub(crate) review_requested: Option<ReviewRequestedConfig>,
pub(crate) shortcut: Option<ShortcutConfig>,
pub(crate) note: Option<NoteConfig>,
pub(crate) mentions: Option<MentionsConfig>,
Expand Down Expand Up @@ -253,6 +254,12 @@ pub(crate) struct ReviewSubmittedConfig {
pub(crate) reviewed_label: String,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
pub(crate) struct ReviewRequestedConfig {
pub(crate) remove_labels: Vec<String>,
pub(crate) add_labels: Vec<String>,
}

pub(crate) async fn get(
gh: &GithubClient,
repo: &Repository,
Expand Down Expand Up @@ -421,6 +428,7 @@ mod tests {
notify_zulip: None,
github_releases: None,
review_submitted: None,
review_requested: None,
mentions: None,
no_merges: None,
}
Expand Down
15 changes: 15 additions & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mod notify_zulip;
mod ping;
mod prioritize;
mod relabel;
mod review_requested;
mod review_submitted;
mod rfc_helper;
pub mod rustc_commits;
Expand Down Expand Up @@ -107,6 +108,20 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
}
}

if let Some(config) = config
.as_ref()
.ok()
.and_then(|c| c.review_requested.as_ref())
{
if let Err(e) = review_requested::handle(ctx, event, config).await {
log::error!(
"failed to process event {:?} with review_requested handler: {:?}",
event,
e
)
}
}

if let Some(ghr_config) = config
.as_ref()
.ok()
Expand Down
40 changes: 40 additions & 0 deletions src/handlers/review_requested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::config::ReviewRequestedConfig;
use crate::github::{Event, IssuesAction, IssuesEvent, Label};
use crate::handlers::Context;

pub(crate) async fn handle(
ctx: &Context,
event: &Event,
config: &ReviewRequestedConfig,
) -> anyhow::Result<()> {
// PR author requests a review from one of the assignees
if let Event::Issue(IssuesEvent {
action,
issue,
sender,
..
}) = event
{
if let IssuesAction::ReviewRequested { requested_reviewer } = action {
if *sender == issue.user && issue.assignees.contains(requested_reviewer) {
issue
.add_labels(
&ctx.github,
config
.add_labels
.iter()
.cloned()
.map(|name| Label { name })
.collect(),
)
.await?;

for label in &config.remove_labels {
issue.remove_label(&ctx.github, label).await?;
}
}
}
}

Ok(())
}

0 comments on commit 7a0a772

Please sign in to comment.