Skip to content

feat: implement r+, r= and r- #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions migrations/20240626072340_add_approved_by_to_pr.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add down migration script here
ALTER TABLE pull_request DROP COLUMN approved_by;
3 changes: 3 additions & 0 deletions migrations/20240626072340_add_approved_by_to_pr.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add up migration script here
ALTER TABLE pull_request ADD COLUMN approved_by TEXT;

1 change: 1 addition & 0 deletions rust-bors.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ timeout = 3600
# - try_failed: Try build has failed
# (Optional)
[labels]
approve = ["+approved"]
try = ["+foo", "-bar"]
try_succeed = ["+foobar", "+foo", "+baz"]
try_failed = []
12 changes: 12 additions & 0 deletions src/bors/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@ pub enum Parent {
Last,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Approver {
/// The approver is the same as the comment author.
Myself,
/// The approver is specified by the user.
Specified(String),
}

/// Bors command specified by a user.
#[derive(Debug, PartialEq)]
pub enum BorsCommand {
/// Approve a commit.
Approve(Approver),
/// Unapprove a commit.
Unapprove,
/// Print help
Help,
/// Ping the bot.
Expand Down
85 changes: 81 additions & 4 deletions src/bors/command/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::collections::HashSet;

use crate::bors::command::{BorsCommand, Parent};
use crate::bors::command::{Approver, BorsCommand, Parent};
use crate::github::CommitSha;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -40,8 +40,14 @@ impl CommandParser {
text: &'a str,
) -> Vec<Result<BorsCommand, CommandParseError<'a>>> {
// The order of the parsers in the vector is important
let parsers: Vec<for<'b> fn(&'b str, &[CommandPart<'b>]) -> ParseResult<'b>> =
vec![parser_help, parser_ping, parser_try_cancel, parser_try];
let parsers: Vec<for<'b> fn(&'b str, &[CommandPart<'b>]) -> ParseResult<'b>> = vec![
parse_self_approve,
parse_unapprove,
parser_help,
parser_ping,
parser_try_cancel,
parser_try,
];

text.lines()
.filter_map(|line| match line.find(&self.prefix) {
Expand All @@ -62,7 +68,11 @@ impl CommandParser {
}
Some(Err(CommandParseError::UnknownCommand(command)))
}
// for `@bors r=user1`
CommandPart::KeyValue { .. } => {
if let Some(result) = parse_approve_on_behalf(&parts) {
return Some(result);
}
Some(Err(CommandParseError::MissingCommand))
}
}
Expand Down Expand Up @@ -108,6 +118,41 @@ fn parse_parts(input: &str) -> Result<Vec<CommandPart>, CommandParseError> {

/// Parsers

/// Parses "@bors r+"
fn parse_self_approve<'a>(command: &'a str, _parts: &[CommandPart<'a>]) -> ParseResult<'a> {
if command == "r+" {
Some(Ok(BorsCommand::Approve(Approver::Myself)))
} else {
None
}
}

/// Parses "@bors r=<username>".
fn parse_approve_on_behalf<'a>(parts: &[CommandPart<'a>]) -> ParseResult<'a> {
if let Some(CommandPart::KeyValue { key, value }) = parts.first() {
if *key != "r" {
Some(Err(CommandParseError::UnknownArg(key)))
} else if value.is_empty() {
return Some(Err(CommandParseError::MissingArgValue { arg: "r" }));
} else {
Some(Ok(BorsCommand::Approve(Approver::Specified(
value.to_string(),
))))
}
} else {
Some(Err(CommandParseError::MissingArgValue { arg: "r" }))
}
}

/// Parses "@bors r-"
fn parse_unapprove<'a>(command: &'a str, _parts: &[CommandPart<'a>]) -> ParseResult<'a> {
if command == "r-" {
Some(Ok(BorsCommand::Unapprove))
} else {
None
}
}

/// Parses "@bors help".
fn parser_help<'a>(command: &'a str, _parts: &[CommandPart<'a>]) -> ParseResult<'a> {
if command == "help" {
Expand Down Expand Up @@ -199,7 +244,7 @@ fn parser_try_cancel<'a>(command: &'a str, parts: &[CommandPart<'a>]) -> ParseRe
#[cfg(test)]
mod tests {
use crate::bors::command::parser::{CommandParseError, CommandParser};
use crate::bors::command::{BorsCommand, Parent};
use crate::bors::command::{Approver, BorsCommand, Parent};
use crate::github::CommitSha;

#[test]
Expand Down Expand Up @@ -242,6 +287,38 @@ mod tests {
assert!(matches!(cmds[0], Err(CommandParseError::DuplicateArg("a"))));
}

#[test]
fn parse_default_approve() {
let cmds = parse_commands("@bors r+");
assert_eq!(cmds.len(), 1);
assert!(matches!(
cmds[0],
Ok(BorsCommand::Approve(Approver::Myself))
));
}

#[test]
fn parse_approve_on_behalf() {
let cmds = parse_commands("@bors r=user1");
assert_eq!(cmds.len(), 1);
insta::assert_debug_snapshot!(cmds[0], @r###"
Ok(
Approve(
Specified(
"user1",
),
),
)
"###);
}

#[test]
fn parse_unapprove() {
let cmds = parse_commands("@bors r-");
assert_eq!(cmds.len(), 1);
assert!(matches!(cmds[0], Ok(BorsCommand::Unapprove)));
}

#[test]
fn parse_help() {
let cmds = parse_commands("@bors help");
Expand Down
14 changes: 14 additions & 0 deletions src/bors/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::bors::event::{BorsGlobalEvent, BorsRepositoryEvent, PullRequestCommen
use crate::bors::handlers::help::command_help;
use crate::bors::handlers::ping::command_ping;
use crate::bors::handlers::refresh::refresh_repository;
use crate::bors::handlers::review::{command_approve, command_unapprove};
use crate::bors::handlers::trybuild::{command_try_build, command_try_cancel, TRY_BRANCH_NAME};
use crate::bors::handlers::workflow::{
handle_check_suite_completed, handle_workflow_completed, handle_workflow_started,
Expand All @@ -22,6 +23,7 @@ mod help;
mod labels;
mod ping;
mod refresh;
mod review;
mod trybuild;
mod workflow;

Expand Down Expand Up @@ -179,6 +181,18 @@ async fn handle_comment(
let repo = Arc::clone(&repo);
let database = Arc::clone(&database);
let result = match command {
BorsCommand::Approve(approver) => {
let span = tracing::info_span!("Approve");
command_approve(repo, database, &pull_request, &comment.author, &approver)
.instrument(span)
.await
}
BorsCommand::Unapprove => {
let span = tracing::info_span!("Unapprove");
command_unapprove(repo, database, &pull_request, &comment.author)
.instrument(span)
.await
}
BorsCommand::Help => {
let span = tracing::info_span!("Help");
command_help(repo, &pull_request).instrument(span).await
Expand Down
Loading
Loading