Skip to content
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
12 changes: 9 additions & 3 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod relabel;
mod relnotes;
mod rendered_link;
mod review_changes_since;
mod review_reminder;
mod review_requested;
mod review_submitted;
pub mod rustc_commits;
Expand Down Expand Up @@ -144,9 +145,14 @@ pub async fn handle(ctx: &Context, host: &str, event: &Event) -> Vec<HandlerErro
.ok()
.and_then(|c| c.review_submitted.as_ref())
{
review_submitted::handle(ctx, event, review_submitted_config)
.await
.map_err(|e| HandlerError::Other(e.context("review_submitted handler failed")))
review_submitted::handle(
ctx,
event,
review_submitted_config,
config.as_ref().ok().and_then(|c| c.shortcut.as_ref()),
)
.await
.map_err(|e| HandlerError::Other(e.context("review_submitted handler failed")))
} else {
Ok(())
}
Expand Down
53 changes: 53 additions & 0 deletions src/handlers/review_reminder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use octocrab::models::AuthorAssociation;

use crate::{config::ShortcutConfig, db::issue_data::IssueData, github::Issue, handlers::Context};

/// Key for the state in the database
const AUTHOR_REMINDER_KEY: &str = "author-reminder";

/// State stored in the database for a PR.
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
struct AuthorReminderState {
/// ID of the reminder comment.
reminder_comment: Option<String>,
}

pub(crate) async fn remind_author_of_bot_ready(
ctx: &Context,
issue: &Issue,
config: Option<&ShortcutConfig>,
) -> anyhow::Result<()> {
let Some(_config) = config else {
// Ignore repository that don't have `[shortcut]` enabled
return Ok(());
};

if matches!(
issue.author_association,
AuthorAssociation::Member | AuthorAssociation::Owner
) {
// Don't post the reminder for org members (public-only unfortunately) and owner of the org.
return Ok(());
}

// Get the state of the author reminder for this PR
let mut db = ctx.db.get().await;
let mut state: IssueData<'_, AuthorReminderState> =
IssueData::load(&mut db, issue, AUTHOR_REMINDER_KEY).await?;

// If no comment already posted, let's post it
if state.data.reminder_comment.is_none() {
let comment_body = format!(
"Reminder, once the PR becomes ready for a review, use `@{bot} ready`.",
bot = &ctx.username,
);
let comment = issue
.post_comment(&ctx.github, comment_body.as_str())
.await?;

state.data.reminder_comment = Some(comment.node_id);
state.save().await?;
}

Ok(())
}
7 changes: 7 additions & 0 deletions src/handlers/review_submitted.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use anyhow::Context as _;

use crate::config::ShortcutConfig;
use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label, PullRequestReviewState};
use crate::{config::ReviewSubmittedConfig, github::Event, handlers::Context};

pub(crate) async fn handle(
ctx: &Context,
event: &Event,
config: &ReviewSubmittedConfig,
shortcut_config: Option<&ShortcutConfig>,
) -> anyhow::Result<()> {
if let Event::IssueComment(
event @ IssueCommentEvent {
Expand Down Expand Up @@ -61,6 +63,11 @@ pub(crate) async fn handle(
}],
)
.await?;

// Add reminder about `@bot review`
super::review_reminder::remind_author_of_bot_ready(ctx, &event.issue, shortcut_config)
.await
.context("failed to remind author of @bot review")?;
}
}

Expand Down
42 changes: 6 additions & 36 deletions src/handlers/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@
use crate::{
config::ShortcutConfig,
db::issue_data::IssueData,
errors::user_error,
github::{Event, Label},
handlers::Context,
};
use octocrab::models::AuthorAssociation;
use anyhow::Context as _;
use parser::command::shortcut::ShortcutCommand;

/// Key for the state in the database
const AUTHOR_REMINDER_KEY: &str = "author-reminder";

/// State stored in the database for a PR.
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
struct AuthorReminderState {
/// ID of the reminder comment.
reminder_comment: Option<String>,
}

pub(super) async fn handle_command(
ctx: &Context,
_config: &ShortcutConfig,
config: &ShortcutConfig,
event: &Event,
input: ShortcutCommand,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -75,29 +64,10 @@ pub(super) async fn handle_command(
// Except if the author is a member (or the owner) of the repository, as
// the author should already know about the `ready` command and already
// have the required permissions to update the labels manually anyway.
if matches!(input, ShortcutCommand::Author)
&& !matches!(
issue.author_association,
AuthorAssociation::Member | AuthorAssociation::Owner
)
{
// Get the state of the author reminder for this PR
let mut db = ctx.db.get().await;
let mut state: IssueData<'_, AuthorReminderState> =
IssueData::load(&mut db, issue, AUTHOR_REMINDER_KEY).await?;

if state.data.reminder_comment.is_none() {
let comment_body = format!(
"Reminder, once the PR becomes ready for a review, use `@{bot} ready`.",
bot = &ctx.username,
);
let comment = issue
.post_comment(&ctx.github, comment_body.as_str())
.await?;

state.data.reminder_comment = Some(comment.node_id);
state.save().await?;
}
if matches!(input, ShortcutCommand::Author) {
super::review_reminder::remind_author_of_bot_ready(ctx, issue, Some(config))
.await
.context("failed to send @bot review reminder")?;
}

Ok(())
Expand Down