Skip to content

Commit 8acec5e

Browse files
committed
@bors info command
1 parent 3b95f4d commit 8acec5e

File tree

7 files changed

+105
-1
lines changed

7 files changed

+105
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
target/
2+
.env

src/bors/command/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ pub enum BorsCommand {
4949
TryCancel,
5050
/// Set the priority of a commit.
5151
SetPriority(Priority),
52+
/// Get information about the current PR.
53+
Info,
5254
}

src/bors/command/parser.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl CommandParser {
5151
parser_ping,
5252
parser_try_cancel,
5353
parser_try,
54+
parser_info,
5455
];
5556

5657
text.lines()
@@ -130,7 +131,6 @@ fn parse_parts(input: &str) -> Result<Vec<CommandPart>, CommandParseError> {
130131
}
131132

132133
/// Parsers
133-
134134
/// Parses "@bors r+ <p=priority>"
135135
fn parse_self_approve<'a>(command: &'a str, parts: &[CommandPart<'a>]) -> ParseResult<'a> {
136136
if command != "r+" {
@@ -321,6 +321,15 @@ fn parse_priority_arg<'a>(parts: &[CommandPart<'a>]) -> Result<Option<u32>, Comm
321321
Ok(priority)
322322
}
323323

324+
/// Parses "@bors info"
325+
fn parser_info<'a>(command: &'a str, _parts: &[CommandPart<'a>]) -> ParseResult<'a> {
326+
if command == "info" {
327+
Some(Ok(BorsCommand::Info))
328+
} else {
329+
None
330+
}
331+
}
332+
324333
#[cfg(test)]
325334
mod tests {
326335
use crate::bors::command::parser::{CommandParseError, CommandParser};
@@ -814,6 +823,20 @@ line two
814823
"###)
815824
}
816825

826+
#[test]
827+
fn parse_info() {
828+
let cmds = parse_commands("@bors info");
829+
assert_eq!(cmds.len(), 1);
830+
assert!(matches!(cmds[0], Ok(BorsCommand::Info)));
831+
}
832+
833+
#[test]
834+
fn parse_info_unknown_arg() {
835+
let cmds = parse_commands("@bors info a");
836+
assert_eq!(cmds.len(), 1);
837+
assert!(matches!(cmds[0], Ok(BorsCommand::Info)));
838+
}
839+
817840
#[test]
818841
fn parse_try_cancel() {
819842
let cmds = parse_commands("@bors try cancel");

src/bors/handlers/help.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(super) async fn command_help(
2626
BorsCommand::TryCancel,
2727
BorsCommand::Ping,
2828
BorsCommand::Help,
29+
BorsCommand::Info,
2930
]
3031
.into_iter()
3132
.map(|help| format!("- {}", get_command_help(help)))
@@ -65,6 +66,9 @@ fn get_command_help(command: BorsCommand) -> String {
6566
BorsCommand::TryCancel => {
6667
"`try cancel`: Cancel a running try build"
6768
}
69+
BorsCommand::Info => {
70+
"`info`: Get information about the current PR including delegation, priority, merge status, and try build status"
71+
}
6872
};
6973
help.to_string()
7074
}
@@ -86,6 +90,7 @@ mod tests {
8690
- `try cancel`: Cancel a running try build
8791
- `ping`: Check if the bot is alive
8892
- `help`: Print this help message
93+
- `info`: Get information about the current PR including delegation, priority, merge status, and try build status
8994
");
9095
Ok(tester)
9196
})

src/bors/handlers/info.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use crate::bors::Comment;
2+
use crate::bors::RepositoryState;
3+
use crate::database::PgDbClient;
4+
use crate::github::PullRequest;
5+
use std::sync::Arc;
6+
7+
pub(super) async fn command_info(
8+
repo: Arc<RepositoryState>,
9+
pr: &PullRequest,
10+
db: Arc<PgDbClient>,
11+
) -> anyhow::Result<()> {
12+
// Geting PR info from database
13+
let pr_model = db.get_pull_request(repo.repository(), pr.number).await?;
14+
15+
// Building the info message
16+
let mut info_lines = Vec::new();
17+
18+
// Approval info
19+
if let Some(pr) = pr_model {
20+
if let Some(approved_by) = pr.approved_by {
21+
info_lines.push(format!("- **Approved by:** @{}", approved_by));
22+
} else {
23+
info_lines.push("- **Approved by:** No one".to_string());
24+
}
25+
26+
// Priority info
27+
if let Some(priority) = pr.priority {
28+
info_lines.push(format!("- **Priority:** {}", priority));
29+
} else {
30+
info_lines.push("- **Priority:** None".to_string());
31+
}
32+
33+
// Build status
34+
if let Some(try_build) = pr.try_build {
35+
info_lines.push(format!("- **Try build branch:** {}", try_build.branch));
36+
} else {
37+
info_lines.push("- **Try build:** None".to_string());
38+
}
39+
} else {
40+
info_lines.push("- **PR not found in database**".to_string());
41+
}
42+
43+
// Merge status (placeholder for future)
44+
info_lines.push("- **Merge status:** Not implemented yet".to_string());
45+
46+
// Queue link (placeholder for future)
47+
info_lines.push("- **Queue link:** Not implemented yet".to_string());
48+
49+
// Joining all lines
50+
let info = info_lines.join("\n");
51+
52+
// Post the comment
53+
repo.client
54+
.post_comment(pr.number, Comment::new(info))
55+
.await?;
56+
57+
Ok(())
58+
}

src/bors/handlers/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use tracing::Instrument;
88
use crate::bors::command::{BorsCommand, CommandParseError};
99
use crate::bors::event::{BorsGlobalEvent, BorsRepositoryEvent, PullRequestComment};
1010
use crate::bors::handlers::help::command_help;
11+
use crate::bors::handlers::info::command_info;
1112
use crate::bors::handlers::ping::command_ping;
1213
use crate::bors::handlers::refresh::refresh_repository;
1314
use crate::bors::handlers::review::{
@@ -24,6 +25,7 @@ use crate::{load_repositories, PgDbClient, TeamApiClient};
2425
use crate::tests::util::TestSyncMarker;
2526

2627
mod help;
28+
mod info;
2729
mod labels;
2830
mod ping;
2931
mod refresh;
@@ -264,6 +266,12 @@ async fn handle_comment(
264266
.instrument(span)
265267
.await
266268
}
269+
BorsCommand::Info => {
270+
let span = tracing::info_span!("Info");
271+
command_info(repo, &pull_request, database)
272+
.instrument(span)
273+
.await
274+
}
267275
};
268276
if result.is_err() {
269277
return result.context("Cannot execute Bors command");

src/database/client.rs

+7
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,11 @@ impl PgDbClient {
168168
.collect::<Vec<_>>();
169169
Ok(workflows)
170170
}
171+
pub async fn get_pull_request(
172+
&self,
173+
repo: &GithubRepoName,
174+
pr_number: PullRequestNumber,
175+
) -> anyhow::Result<Option<PullRequestModel>> {
176+
get_pull_request(&self.pool, repo, pr_number).await
177+
}
171178
}

0 commit comments

Comments
 (0)