Skip to content

Commit ed28689

Browse files
committed
Generate help dynamically
Before, it was a static string and it was easy to forget to add new commands to it.
1 parent ea91f11 commit ed28689

File tree

1 file changed

+57
-12
lines changed

1 file changed

+57
-12
lines changed

src/bors/handlers/help.rs

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,81 @@
1-
use std::sync::Arc;
2-
1+
use crate::bors::command::{Approver, BorsCommand};
32
use crate::bors::Comment;
43
use crate::bors::RepositoryState;
54
use crate::github::PullRequest;
6-
7-
const HELP_MESSAGE: &str = r#"
8-
- try: Execute the `try` CI workflow on this PR (without approving it for a merge).
9-
- try cancel: Stop try builds on current PR.
10-
- ping: pong
11-
- help: Print this help message
12-
"#;
5+
use std::sync::Arc;
136

147
pub(super) async fn command_help(
158
repo: Arc<RepositoryState>,
169
pr: &PullRequest,
1710
) -> anyhow::Result<()> {
11+
let help = [
12+
BorsCommand::Approve(Approver::Myself),
13+
BorsCommand::Approve(Approver::Specified("".to_string())),
14+
BorsCommand::Unapprove,
15+
BorsCommand::Try {
16+
parent: None,
17+
jobs: vec![],
18+
},
19+
BorsCommand::TryCancel,
20+
BorsCommand::Ping,
21+
BorsCommand::Help,
22+
]
23+
.into_iter()
24+
.map(|help| format!("- {}", get_command_help(help)))
25+
.collect::<Vec<_>>()
26+
.join("\n");
27+
1828
repo.client
19-
.post_comment(pr.number, Comment::new(HELP_MESSAGE.to_string()))
29+
.post_comment(pr.number, Comment::new(help))
2030
.await?;
2131
Ok(())
2232
}
2333

34+
fn get_command_help(command: BorsCommand) -> String {
35+
// !!! When modifying this match, also update the command list above (in [`command_help`]) !!!
36+
let help = match command {
37+
BorsCommand::Approve(Approver::Myself) => {
38+
"`r+`: Approve this PR"
39+
}
40+
BorsCommand::Approve(Approver::Specified(_)) => {
41+
"`r=<user>`: Approve this PR on behalf of `<user>`"
42+
}
43+
BorsCommand::Unapprove => {
44+
"`r-`: Unapprove this PR"
45+
}
46+
BorsCommand::Help => {
47+
"`help`: Print this help message"
48+
}
49+
BorsCommand::Ping => {
50+
"`ping`: Check if the bot is alive"
51+
}
52+
BorsCommand::Try { .. } => {
53+
"`try [parent=<parent>] [jobs=<jobs>]`: Start a try build. Optionally, you can specify a `<parent>` SHA or a list of `<jobs>` to run"
54+
}
55+
BorsCommand::TryCancel => {
56+
"`try cancel`: Cancel a running try build"
57+
}
58+
};
59+
help.to_string()
60+
}
61+
2462
#[cfg(test)]
2563
mod tests {
26-
use crate::bors::handlers::help::HELP_MESSAGE;
2764
use crate::tests::mocks::run_test;
2865

2966
#[sqlx::test]
3067
async fn help_command(pool: sqlx::PgPool) {
3168
run_test(pool, |mut tester| async {
3269
tester.post_comment("@bors help").await?;
33-
assert_eq!(tester.get_comment().await?, HELP_MESSAGE);
70+
insta::assert_snapshot!(tester.get_comment().await?, @r"
71+
- `r+`: Approve this PR
72+
- `r=<user>`: Approve this PR on behalf of `<user>`
73+
- `r-`: Unapprove this PR
74+
- `try [parent=<parent>] [jobs=<jobs>]`: Start a try build. Optionally, you can specify a `<parent>` SHA or a list of `<jobs>` to run
75+
- `try cancel`: Cancel a running try build
76+
- `ping`: Check if the bot is alive
77+
- `help`: Print this help message
78+
");
3479
Ok(tester)
3580
})
3681
.await;

0 commit comments

Comments
 (0)