Skip to content

Commit dd2a7d8

Browse files
committed
chore(handlers/assign): replace const + replace patterns with typed functions
1 parent 9463f08 commit dd2a7d8

File tree

3 files changed

+120
-108
lines changed

3 files changed

+120
-108
lines changed

src/handlers/assign.rs

Lines changed: 21 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -43,61 +43,14 @@ use tokio::sync::RwLock;
4343
use tokio_postgres::Client as DbClient;
4444
use tracing as log;
4545

46+
mod messages;
47+
4648
#[cfg(test)]
4749
mod tests {
4850
mod tests_candidates;
4951
mod tests_from_diff;
5052
}
5153

52-
const NEW_USER_WELCOME_MESSAGE: &str = "Thanks for the pull request, and welcome! \
53-
The Rust team is excited to review your changes, and you should hear from {who} \
54-
some time within the next two weeks.";
55-
56-
const CONTRIBUTION_MESSAGE: &str = "Please see [the contribution \
57-
instructions]({contributing_url}) for more information. Namely, in order to ensure the \
58-
minimum review times lag, PR authors and assigned reviewers should ensure that the review \
59-
label (`S-waiting-on-review` and `S-waiting-on-author`) stays updated, invoking these commands \
60-
when appropriate:
61-
62-
- `@{bot} author`: the review is finished, PR author should check the comments and take action accordingly
63-
- `@{bot} review`: the author is ready for a review, this PR will be queued again in the reviewer's queue";
64-
65-
const WELCOME_WITH_REVIEWER: &str = "@{assignee} (or someone else)";
66-
67-
const WELCOME_WITHOUT_REVIEWER: &str = "@Mark-Simulacrum (NB. this repo may be misconfigured)";
68-
69-
const RETURNING_USER_WELCOME_MESSAGE: &str = "r? @{assignee}
70-
71-
{bot} has assigned @{assignee}.
72-
They will have a look at your PR within the next two weeks and either review your PR or \
73-
reassign to another reviewer.
74-
75-
Use `r?` to explicitly pick a reviewer";
76-
77-
const RETURNING_USER_WELCOME_MESSAGE_NO_REVIEWER: &str =
78-
"@{author}: no appropriate reviewer found, use `r?` to override";
79-
80-
fn reviewer_off_rotation_message(username: &str) -> String {
81-
format!(
82-
r"`{username}` is not available for reviewing at the moment.
83-
84-
Please choose another assignee."
85-
)
86-
}
87-
88-
const REVIEWER_IS_PR_AUTHOR: &str = "Pull request author cannot be assigned as reviewer.
89-
90-
Please choose another assignee.";
91-
92-
const REVIEWER_ALREADY_ASSIGNED: &str =
93-
"Requested reviewer is already assigned to this pull request.
94-
95-
Please choose another assignee.";
96-
97-
const REVIEWER_ASSIGNED_BEFORE: &str = "Requested reviewer @{username} was already assigned before.
98-
99-
Please choose another assignee by using `r? @reviewer`.";
100-
10154
// Special account that we use to prevent assignment.
10255
const GHOST_ACCOUNT: &str = "ghost";
10356

@@ -211,11 +164,7 @@ pub(super) async fn handle_input(
211164
AuthorAssociation::FirstTimer | AuthorAssociation::FirstTimeContributor
212165
) {
213166
welcome.push_str("\n\n");
214-
welcome.push_str(
215-
&CONTRIBUTION_MESSAGE
216-
.replace("{contributing_url}", contrib)
217-
.replace("{bot}", &ctx.username),
218-
);
167+
welcome.push_str(&messages::contribution_message(contrib, &ctx.username));
219168
}
220169
}
221170
Some(welcome)
@@ -227,35 +176,29 @@ pub(super) async fn handle_input(
227176
event.issue.author_association,
228177
AuthorAssociation::FirstTimer | AuthorAssociation::FirstTimeContributor
229178
) {
230-
let who_text = match &assignee {
231-
Some(assignee) => WELCOME_WITH_REVIEWER.replace("{assignee}", &assignee.name),
232-
None => WELCOME_WITHOUT_REVIEWER.to_string(),
179+
let assignee_text = match &assignee {
180+
Some(assignee) => messages::welcome_with_reviewer(&assignee.name),
181+
None => messages::WELCOME_WITHOUT_REVIEWER.to_string(),
233182
};
234-
let mut welcome = NEW_USER_WELCOME_MESSAGE.replace("{who}", &who_text);
183+
let mut welcome = messages::new_user_welcome_message(&assignee_text);
235184
if let Some(contrib) = &config.contributing_url {
236185
welcome.push_str("\n\n");
237-
welcome.push_str(
238-
&CONTRIBUTION_MESSAGE
239-
.replace("{contributing_url}", contrib)
240-
.replace("{bot}", &ctx.username),
241-
);
186+
welcome.push_str(&messages::contribution_message(contrib, &ctx.username));
242187
}
243188
Some(welcome)
244189
} else if !from_comment {
245190
match &assignee {
246-
Some(assignee) => Some(
247-
RETURNING_USER_WELCOME_MESSAGE
248-
.replace("{assignee}", &assignee.name)
249-
.replace("{bot}", &ctx.username),
250-
),
191+
Some(assignee) => Some(messages::returning_user_welcome_message(
192+
&assignee.name,
193+
&ctx.username,
194+
)),
251195
None => {
252196
// If the assign fallback group is empty, then we don't expect any automatic
253197
// assignment, and this message would just be spam.
254198
if config.fallback_review_group().is_some() {
255-
Some(
256-
RETURNING_USER_WELCOME_MESSAGE_NO_REVIEWER
257-
.replace("{author}", &event.issue.user.login),
258-
)
199+
Some(messages::returning_user_welcome_message_no_reviewer(
200+
&event.issue.user.login,
201+
))
259202
} else {
260203
None
261204
}
@@ -807,28 +750,16 @@ impl fmt::Display for FindReviewerError {
807750
)
808751
}
809752
FindReviewerError::ReviewerOffRotation { username } => {
810-
write!(f, "{}", reviewer_off_rotation_message(username))
753+
write!(f, "{}", messages::reviewer_off_rotation_message(username))
811754
}
812-
FindReviewerError::ReviewerIsPrAuthor { username } => {
813-
write!(
814-
f,
815-
"{}",
816-
REVIEWER_IS_PR_AUTHOR.replace("{username}", username)
817-
)
755+
FindReviewerError::ReviewerIsPrAuthor { .. } => {
756+
write!(f, "{}", messages::REVIEWER_IS_PR_AUTHOR)
818757
}
819-
FindReviewerError::ReviewerAlreadyAssigned { username } => {
820-
write!(
821-
f,
822-
"{}",
823-
REVIEWER_ALREADY_ASSIGNED.replace("{username}", username)
824-
)
758+
FindReviewerError::ReviewerAlreadyAssigned { .. } => {
759+
write!(f, "{}", messages::REVIEWER_ALREADY_ASSIGNED)
825760
}
826761
FindReviewerError::ReviewerPreviouslyAssigned { username } => {
827-
write!(
828-
f,
829-
"{}",
830-
REVIEWER_ASSIGNED_BEFORE.replace("{username}", username)
831-
)
762+
write!(f, "{}", messages::reviewer_assigned_before(username))
832763
}
833764
FindReviewerError::DatabaseError(error) => {
834765
write!(f, "Database error: {error}")

src/handlers/assign/messages.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//! Assignment messages functions and constants.
2+
//!
3+
//! This module contains the different constants and functions related
4+
//! to assignment messages.
5+
6+
pub fn new_user_welcome_message(reviewer: &str) -> String {
7+
format!(
8+
"Thanks for the pull request, and welcome! \
9+
The Rust team is excited to review your changes, and you should hear from {reviewer} \
10+
some time within the next two weeks."
11+
)
12+
}
13+
14+
pub fn contribution_message(contributing_url: &str, bot: &str) -> String {
15+
format!(
16+
"Please see [the contribution \
17+
instructions]({contributing_url}) for more information. Namely, in order to ensure the \
18+
minimum review times lag, PR authors and assigned reviewers should ensure that the review \
19+
label (`S-waiting-on-review` and `S-waiting-on-author`) stays updated, invoking these commands \
20+
when appropriate:
21+
22+
- `@{bot} author`: the review is finished, PR author should check the comments and take action accordingly
23+
- `@{bot} review`: the author is ready for a review, this PR will be queued again in the reviewer's queue"
24+
)
25+
}
26+
27+
pub fn welcome_with_reviewer(assignee: &str) -> String {
28+
format!("@{assignee} (or someone else)")
29+
}
30+
31+
pub fn returning_user_welcome_message(assignee: &str, bot: &str) -> String {
32+
format!(
33+
"r? @{assignee}
34+
35+
{bot} has assigned @{assignee}.
36+
They will have a look at your PR within the next two weeks and either review your PR or \
37+
reassign to another reviewer.
38+
39+
Use `r?` to explicitly pick a reviewer"
40+
)
41+
}
42+
43+
pub fn returning_user_welcome_message_no_reviewer(pr_author: &str) -> String {
44+
format!("@{pr_author}: no appropriate reviewer found, use `r?` to override")
45+
}
46+
47+
pub fn reviewer_off_rotation_message(username: &str) -> String {
48+
format!(
49+
r"`{username}` is not available for reviewing at the moment.
50+
51+
Please choose another assignee."
52+
)
53+
}
54+
55+
pub fn reviewer_assigned_before(username: &str) -> String {
56+
format!(
57+
"Requested reviewer @{username} was already assigned before.
58+
59+
Please choose another assignee by using `r? @reviewer`."
60+
)
61+
}
62+
63+
pub const WELCOME_WITHOUT_REVIEWER: &str = "@Mark-Simulacrum (NB. this repo may be misconfigured)";
64+
65+
pub const REVIEWER_IS_PR_AUTHOR: &str = "Pull request author cannot be assigned as reviewer.
66+
67+
68+
Please choose another assignee.";
69+
70+
pub const REVIEWER_ALREADY_ASSIGNED: &str =
71+
"Requested reviewer is already assigned to this pull request.
72+
73+
Please choose another assignee.";

src/handlers/project_goals.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,29 @@ const RUST_PROJECT_GOALS_REPO: &'static str = "rust-lang/rust-project-goals";
1717
const GOALS_STREAM: u64 = 435869; // #project-goals
1818
const C_TRACKING_ISSUE: &str = "C-tracking-issue";
1919

20-
const MESSAGE: &str = r#"
21-
Dear $OWNERS, it's been $DAYS days since the last update to your goal *$GOAL*.
20+
fn message(
21+
zulip_owners: &str,
22+
days: &str,
23+
issue_number: u64,
24+
issue_title: &str,
25+
next_update: &str,
26+
) -> String {
27+
format!(
28+
r#"
29+
Dear {zulip_owners}, it's been {days} days since the last update to your goal *{issue_title}*.
2230
23-
We will begin drafting the next blog post collecting goal updates $NEXT_UPDATE.
31+
We will begin drafting the next blog post collecting goal updates {next_update}.
2432
25-
Please comment on the github tracking issue goals#$GOALNUM before then. Thanks! <3
33+
Please comment on the github tracking issue goals#{issue_number} before then. Thanks! <3
2634
2735
Here is a suggested template for updates (feel free to drop the items that don't apply):
2836
2937
* **Key developments:** *What has happened since the last time. It's perfectly ok to list "nothing" if that's the truth, we know people get busy.*
3038
* **Blockers:** *List any Rust teams you are waiting on and what you are waiting for.*
3139
* **Help wanted:** *Are there places where you are looking for contribution or feedback from the broader community?*
32-
"#;
40+
"#
41+
)
42+
}
3343

3444
pub struct ProjectGoalsUpdateJob;
3545

@@ -146,19 +156,17 @@ pub async fn ping_project_goals_owners(
146156
continue;
147157
};
148158

149-
let message = MESSAGE
150-
.replace("$OWNERS", &zulip_owners)
151-
.replace(
152-
"$DAYS",
153-
&if comments <= 1 {
154-
"∞".to_string()
155-
} else {
156-
days_since_last_comment.to_string()
157-
},
158-
)
159-
.replace("$GOALNUM", &issue.number.to_string())
160-
.replace("$GOAL", &issue.title)
161-
.replace("$NEXT_UPDATE", next_update);
159+
let message = message(
160+
&zulip_owners,
161+
&if comments <= 1 {
162+
"∞".to_string()
163+
} else {
164+
days_since_last_comment.to_string()
165+
},
166+
issue.number,
167+
&issue.title,
168+
next_update,
169+
);
162170

163171
let zulip_req = crate::zulip::MessageApiRequest {
164172
recipient: Recipient::Stream {

0 commit comments

Comments
 (0)