Skip to content

Commit dc13335

Browse files
committed
Also add @bot ready reminder for review submitted
1 parent bbc836c commit dc13335

File tree

4 files changed

+75
-39
lines changed

4 files changed

+75
-39
lines changed

src/handlers.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mod relabel;
3737
mod relnotes;
3838
mod rendered_link;
3939
mod review_changes_since;
40+
mod review_reminder;
4041
mod review_requested;
4142
mod review_submitted;
4243
pub mod rustc_commits;
@@ -144,9 +145,14 @@ pub async fn handle(ctx: &Context, host: &str, event: &Event) -> Vec<HandlerErro
144145
.ok()
145146
.and_then(|c| c.review_submitted.as_ref())
146147
{
147-
review_submitted::handle(ctx, event, review_submitted_config)
148-
.await
149-
.map_err(|e| HandlerError::Other(e.context("review_submitted handler failed")))
148+
review_submitted::handle(
149+
ctx,
150+
event,
151+
review_submitted_config,
152+
config.as_ref().ok().and_then(|c| c.shortcut.as_ref()),
153+
)
154+
.await
155+
.map_err(|e| HandlerError::Other(e.context("review_submitted handler failed")))
150156
} else {
151157
Ok(())
152158
}

src/handlers/review_reminder.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use octocrab::models::AuthorAssociation;
2+
3+
use crate::{config::ShortcutConfig, db::issue_data::IssueData, github::Issue, handlers::Context};
4+
5+
/// Key for the state in the database
6+
const AUTHOR_REMINDER_KEY: &str = "author-reminder";
7+
8+
/// State stored in the database for a PR.
9+
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
10+
struct AuthorReminderState {
11+
/// ID of the reminder comment.
12+
reminder_comment: Option<String>,
13+
}
14+
15+
pub(crate) async fn remind_author_of_bot_ready(
16+
ctx: &Context,
17+
issue: &Issue,
18+
config: Option<&ShortcutConfig>,
19+
) -> anyhow::Result<()> {
20+
let Some(_config) = config else {
21+
// Ignore repository that don't have `[shortcut]` enabled
22+
return Ok(());
23+
};
24+
25+
if matches!(
26+
issue.author_association,
27+
AuthorAssociation::Member | AuthorAssociation::Owner
28+
) {
29+
// Don't post the reminder for org members (public-only unfortunately) and owner of the org.
30+
return Ok(());
31+
}
32+
33+
// Get the state of the author reminder for this PR
34+
let mut db = ctx.db.get().await;
35+
let mut state: IssueData<'_, AuthorReminderState> =
36+
IssueData::load(&mut db, issue, AUTHOR_REMINDER_KEY).await?;
37+
38+
// If no comment already posted, let's post it
39+
if state.data.reminder_comment.is_none() {
40+
let comment_body = format!(
41+
"Reminder, once the PR becomes ready for a review, use `@{bot} ready`.",
42+
bot = &ctx.username,
43+
);
44+
let comment = issue
45+
.post_comment(&ctx.github, comment_body.as_str())
46+
.await?;
47+
48+
state.data.reminder_comment = Some(comment.node_id);
49+
state.save().await?;
50+
}
51+
52+
Ok(())
53+
}

src/handlers/review_submitted.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use anyhow::Context as _;
22

3+
use crate::config::ShortcutConfig;
34
use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label, PullRequestReviewState};
45
use crate::{config::ReviewSubmittedConfig, github::Event, handlers::Context};
56

67
pub(crate) async fn handle(
78
ctx: &Context,
89
event: &Event,
910
config: &ReviewSubmittedConfig,
11+
shortcut_config: Option<&ShortcutConfig>,
1012
) -> anyhow::Result<()> {
1113
if let Event::IssueComment(
1214
event @ IssueCommentEvent {
@@ -61,6 +63,11 @@ pub(crate) async fn handle(
6163
}],
6264
)
6365
.await?;
66+
67+
// Add reminder about `@bot review`
68+
super::review_reminder::remind_author_of_bot_ready(ctx, &event.issue, shortcut_config)
69+
.await
70+
.context("failed to remind author of @bot review")?;
6471
}
6572
}
6673

src/handlers/shortcut.rs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@
44
55
use crate::{
66
config::ShortcutConfig,
7-
db::issue_data::IssueData,
87
errors::user_error,
98
github::{Event, Label},
109
handlers::Context,
1110
};
12-
use octocrab::models::AuthorAssociation;
11+
use anyhow::Context as _;
1312
use parser::command::shortcut::ShortcutCommand;
1413

15-
/// Key for the state in the database
16-
const AUTHOR_REMINDER_KEY: &str = "author-reminder";
17-
18-
/// State stored in the database for a PR.
19-
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
20-
struct AuthorReminderState {
21-
/// ID of the reminder comment.
22-
reminder_comment: Option<String>,
23-
}
24-
2514
pub(super) async fn handle_command(
2615
ctx: &Context,
27-
_config: &ShortcutConfig,
16+
config: &ShortcutConfig,
2817
event: &Event,
2918
input: ShortcutCommand,
3019
) -> anyhow::Result<()> {
@@ -75,29 +64,10 @@ pub(super) async fn handle_command(
7564
// Except if the author is a member (or the owner) of the repository, as
7665
// the author should already know about the `ready` command and already
7766
// have the required permissions to update the labels manually anyway.
78-
if matches!(input, ShortcutCommand::Author)
79-
&& !matches!(
80-
issue.author_association,
81-
AuthorAssociation::Member | AuthorAssociation::Owner
82-
)
83-
{
84-
// Get the state of the author reminder for this PR
85-
let mut db = ctx.db.get().await;
86-
let mut state: IssueData<'_, AuthorReminderState> =
87-
IssueData::load(&mut db, issue, AUTHOR_REMINDER_KEY).await?;
88-
89-
if state.data.reminder_comment.is_none() {
90-
let comment_body = format!(
91-
"Reminder, once the PR becomes ready for a review, use `@{bot} ready`.",
92-
bot = &ctx.username,
93-
);
94-
let comment = issue
95-
.post_comment(&ctx.github, comment_body.as_str())
96-
.await?;
97-
98-
state.data.reminder_comment = Some(comment.node_id);
99-
state.save().await?;
100-
}
67+
if matches!(input, ShortcutCommand::Author) {
68+
super::review_reminder::remind_author_of_bot_ready(ctx, issue, Some(config))
69+
.await
70+
.context("failed to send @bot review reminder")?;
10171
}
10272

10373
Ok(())

0 commit comments

Comments
 (0)