Skip to content

Commit

Permalink
Un-ad-hoc review_requested handler
Browse files Browse the repository at this point in the history
Use the `issue_handlers!` instead
  • Loading branch information
WaffleLapkin committed Oct 15, 2023
1 parent 7a0a772 commit 9563aff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
15 changes: 1 addition & 14 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,6 @@ 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 Expand Up @@ -179,6 +165,7 @@ issue_handlers! {
mentions,
no_merges,
notify_zulip,
review_requested,
}

macro_rules! command_handlers {
Expand Down
73 changes: 43 additions & 30 deletions src/handlers/review_requested.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
use crate::config::ReviewRequestedConfig;
use crate::github::{Event, IssuesAction, IssuesEvent, Label};
use crate::github::{IssuesAction, IssuesEvent, Label};
use crate::handlers::Context;

pub(crate) async fn handle(
pub(crate) struct ReviewRequestedInput {}

pub(crate) async fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
_config: Option<&ReviewRequestedConfig>,
) -> Result<Option<ReviewRequestedInput>, String> {
// PR author requests a review from one of the assignees

let IssuesAction::ReviewRequested { requested_reviewer } = &event.action else {
return Ok(None);
};

if event.sender != event.issue.user {
return Ok(None);
}

if !event.issue.assignees.contains(requested_reviewer) {
return Ok(None);
}

Ok(Some(ReviewRequestedInput {}))
}

pub(crate) async fn handle_input(
ctx: &Context,
event: &Event,
config: &ReviewRequestedConfig,
event: &IssuesEvent,
ReviewRequestedInput {}: ReviewRequestedInput,
) -> 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?;
}
}
}
event
.issue
.add_labels(
&ctx.github,
config
.add_labels
.iter()
.cloned()
.map(|name| Label { name })
.collect(),
)
.await?;

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

Ok(())
Expand Down

0 comments on commit 9563aff

Please sign in to comment.