Skip to content

Commit 87c9dfd

Browse files
authored
Merge pull request #352 from Kobzol/bors-try-special-case
Parse `@bors try` commands to replace homu for try builds
2 parents c6eb66a + 1577d17 commit 87c9dfd

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/bors/command/parser.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,22 @@ enum CommandPart<'a> {
2828

2929
pub struct CommandParser {
3030
prefix: CommandPrefix,
31+
parsers: Vec<ParserFn>,
3132
}
3233

3334
impl CommandParser {
3435
pub fn new(prefix: CommandPrefix) -> Self {
35-
Self { prefix }
36+
Self {
37+
prefix,
38+
parsers: DEFAULT_PARSERS.to_vec(),
39+
}
40+
}
41+
42+
pub fn new_try_only(prefix: CommandPrefix) -> Self {
43+
Self {
44+
prefix,
45+
parsers: ONLY_TRY_PARSERS.to_vec(),
46+
}
3647
}
3748

3849
/// Prefix of the bot, used to invoke commands from PR comments.
@@ -52,7 +63,7 @@ impl CommandParser {
5263
.filter_map(|line| match line.find(self.prefix.as_ref()) {
5364
Some(index) => {
5465
let input = &line[index + self.prefix.as_ref().len()..];
55-
parse_command(input)
66+
parse_command(input, &self.parsers)
5667
}
5768
None => None,
5869
})
@@ -116,9 +127,10 @@ fn extract_text_from_markdown(text: &str) -> String {
116127
}
117128

118129
type ParseResult<T = BorsCommand> = Option<Result<T, CommandParseError>>;
130+
type ParserFn = fn(&CommandPart<'_>, &[CommandPart<'_>]) -> ParseResult;
119131

120132
// The order of the parsers in the vector is important
121-
const PARSERS: &[fn(&CommandPart<'_>, &[CommandPart<'_>]) -> ParseResult] = &[
133+
const DEFAULT_PARSERS: &[ParserFn] = &[
122134
parser_approval,
123135
parser_unapprove,
124136
parser_rollup,
@@ -133,12 +145,14 @@ const PARSERS: &[fn(&CommandPart<'_>, &[CommandPart<'_>]) -> ParseResult] = &[
133145
parser_tree_ops,
134146
];
135147

136-
fn parse_command(input: &str) -> ParseResult {
148+
const ONLY_TRY_PARSERS: &[ParserFn] = &[parser_try_cancel, parser_try];
149+
150+
fn parse_command(input: &str, parsers: &[ParserFn]) -> ParseResult {
137151
match parse_parts(input) {
138152
Ok(parts) => match parts.as_slice() {
139153
[] => Some(Err(CommandParseError::MissingCommand)),
140154
[command, arguments @ ..] => {
141-
for parser in PARSERS {
155+
for parser in parsers {
142156
if let Some(result) = parser(command, arguments) {
143157
return Some(result);
144158
}

src/bors/handlers/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use crate::bors::handlers::review::{
1818
use crate::bors::handlers::trybuild::{TRY_BRANCH_NAME, command_try_build, command_try_cancel};
1919
use crate::bors::handlers::workflow::{handle_workflow_completed, handle_workflow_started};
2020
use crate::bors::merge_queue::{AUTO_BRANCH_NAME, MergeQueueSender};
21-
use crate::bors::{BorsContext, Comment, RepositoryState};
21+
use crate::bors::{BorsContext, CommandPrefix, Comment, RepositoryState};
2222
use crate::database::{DelegatedPermission, PullRequestModel};
2323
use crate::github::{GithubUser, PullRequest, PullRequestNumber};
2424
use crate::permissions::PermissionType;
25-
use crate::{PgDbClient, TeamApiClient, load_repositories};
25+
use crate::{CommandParser, PgDbClient, TeamApiClient, load_repositories};
2626
use anyhow::Context;
2727
use octocrab::Octocrab;
2828
use pr_events::{
@@ -337,7 +337,23 @@ async fn handle_comment(
337337
use std::fmt::Write;
338338

339339
let pr_number = comment.pr_number;
340-
let commands = ctx.parser.parse_commands(&comment.text);
340+
let mut commands = ctx.parser.parse_commands(&comment.text);
341+
342+
// Temporary special case for migration from homu on rust-lang/rust.
343+
// Try to parse `@bors try` commands with a hardcoded prefix normally assigned to homu.
344+
if repo.repository().owner() == "rust-lang" && repo.repository().name() == "rust" {
345+
let homu_commands = CommandParser::new_try_only(CommandPrefix::from("@bors".to_string()))
346+
.parse_commands(&comment.text)
347+
.into_iter()
348+
.filter(|res| match res {
349+
// Let homu handle unknown and missing commands
350+
Err(CommandParseError::UnknownCommand(_) | CommandParseError::MissingCommand) => {
351+
false
352+
}
353+
_ => true,
354+
});
355+
commands.extend(homu_commands);
356+
}
341357

342358
// Bail if no commands
343359
if commands.is_empty() {

0 commit comments

Comments
 (0)