Skip to content

feat: add delegation commands #210

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 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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.

4 changes: 3 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ which is by default set to `@bors`.
| `try` | `try` | Start a try build based on the most recent commit from the main branch. |
| `try parent=<sha>` | `try` | Start a try build based on the specified parent commit `sha`. |
| `try cancel` | `try` | Cancel a running try build. |
| `p=<priority>` | `try` | Set the priority of a PR. |
| `p=<priority>` | `review` | Set the priority of a PR. |
| `delegate+` | `review` | Delegate approval authority to the PR author. |
| `delegate-` | `review` | Remove any previously granted delegation. |
2 changes: 2 additions & 0 deletions migrations/20250304150656_add_delegated_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 delegated;
2 changes: 2 additions & 0 deletions migrations/20250304150656_add_delegated_to_pr.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add up migration script here
ALTER TABLE pull_request ADD COLUMN delegated BOOLEAN NOT NULL DEFAULT FALSE;
4 changes: 4 additions & 0 deletions src/bors/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ pub enum BorsCommand {
TryCancel,
/// Set the priority of a commit.
SetPriority(Priority),
/// Delegate approval authority to the pull request author.
Delegate,
/// Revoke any previously granted delegation.
Undelegate,
}
42 changes: 42 additions & 0 deletions src/bors/command/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl CommandParser {
parser_ping,
parser_try_cancel,
parser_try,
parse_delegate_author,
parse_undelegate,
];

text.lines()
Expand Down Expand Up @@ -152,6 +154,7 @@ fn parse_approve_on_behalf<'a>(parts: &[CommandPart<'a>]) -> ParseResult<'a> {
if value.is_empty() {
return Some(Err(CommandParseError::MissingArgValue { arg: "r" }));
}

match parse_priority_arg(&parts[1..]) {
Ok(priority) => Some(Ok(BorsCommand::Approve {
approver: Approver::Specified(value.to_string()),
Expand Down Expand Up @@ -270,6 +273,24 @@ fn parser_try_cancel<'a>(command: &'a str, parts: &[CommandPart<'a>]) -> ParseRe
}
}

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

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

/// Parses "@bors p=<priority>"
fn parse_priority<'a>(parts: &[CommandPart<'a>]) -> ParseResult<'a> {
// If we have more than one part, check for duplicate priority arguments
Expand Down Expand Up @@ -821,6 +842,27 @@ line two
assert!(matches!(cmds[0], Ok(BorsCommand::TryCancel)));
}

#[test]
fn parse_delegate_author() {
let cmds = parse_commands("@bors delegate+");
assert_eq!(cmds.len(), 1);
assert!(matches!(cmds[0], Ok(BorsCommand::Delegate)));
}

#[test]
fn parse_undelegate() {
let cmds = parse_commands("@bors delegate-");
assert_eq!(cmds.len(), 1);
assert_eq!(cmds[0], Ok(BorsCommand::Undelegate));
}

#[test]
fn parse_delegate_author_unknown_arg() {
let cmds = parse_commands("@bors delegate+ a");
assert_eq!(cmds.len(), 1);
assert_eq!(cmds[0], Ok(BorsCommand::Delegate));
}

fn parse_commands(text: &str) -> Vec<Result<BorsCommand, CommandParseError>> {
CommandParser::new("@bors".to_string()).parse_commands(text)
}
Expand Down
10 changes: 10 additions & 0 deletions src/bors/handlers/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub(super) async fn command_help(
},
BorsCommand::Unapprove,
BorsCommand::SetPriority(0),
BorsCommand::Delegate,
BorsCommand::Undelegate,
BorsCommand::Try {
parent: None,
jobs: vec![],
Expand Down Expand Up @@ -53,6 +55,12 @@ fn get_command_help(command: BorsCommand) -> String {
BorsCommand::SetPriority(_) => {
"`p=<priority>`: Set the priority of this PR"
}
BorsCommand::Delegate => {
"`delegate+`: Delegate approval authority to the PR author"
}
BorsCommand::Undelegate => {
"`delegate-`: Remove any previously granted delegation"
}
BorsCommand::Help => {
"`help`: Print this help message"
}
Expand Down Expand Up @@ -82,6 +90,8 @@ mod tests {
- `r=<user> [p=<priority>]`: Approve this PR on behalf of `<user>`. Optionally, you can specify a `<priority>`.
- `r-`: Unapprove this PR
- `p=<priority>`: Set the priority of this PR
- `delegate+`: Delegate approval authority to the PR author
- `delegate-`: Remove any previously granted delegation
- `try [parent=<parent>] [jobs=<jobs>]`: Start a try build. Optionally, you can specify a `<parent>` SHA or a list of `<jobs>` to run
- `try cancel`: Cancel a running try build
- `ping`: Check if the bot is alive
Expand Down
62 changes: 61 additions & 1 deletion src/bors/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use anyhow::Context;
use octocrab::Octocrab;
use review::command_set_priority;
use review::{command_delegate, command_set_priority, command_undelegate};
use tracing::Instrument;

use crate::bors::command::{BorsCommand, CommandParseError};
Expand All @@ -18,6 +18,8 @@ use crate::bors::handlers::workflow::{
handle_check_suite_completed, handle_workflow_completed, handle_workflow_started,
};
use crate::bors::{BorsContext, Comment, RepositoryState};
use crate::github::{GithubUser, PullRequest};
use crate::permissions::PermissionType;
use crate::{load_repositories, PgDbClient, TeamApiClient};

#[cfg(test)]
Expand Down Expand Up @@ -237,6 +239,18 @@ async fn handle_comment(
.instrument(span)
.await
}
BorsCommand::Delegate => {
let span = tracing::info_span!("Delegate");
command_delegate(repo, database, &pull_request, &comment.author)
.instrument(span)
.await
}
BorsCommand::Undelegate => {
let span = tracing::info_span!("Undelegate");
command_undelegate(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 Expand Up @@ -334,6 +348,52 @@ fn is_bors_observed_branch(branch: &str) -> bool {
branch == TRY_BRANCH_NAME
}

/// Deny permission for a request.
async fn deny_request(
repo: &RepositoryState,
pr: &PullRequest,
author: &GithubUser,
permission_type: PermissionType,
) -> anyhow::Result<()> {
tracing::warn!(
"Permission denied for request command by {}",
author.username
);
repo.client
.post_comment(
pr.number,
Comment::new(format!(
"@{}: :key: Insufficient privileges: not in {} users",
author.username, permission_type
)),
)
.await
}

/// Check if a user has specified permission or has been delegated.
async fn has_permission(
repo_state: &RepositoryState,
author: &GithubUser,
pr: &PullRequest,
db: &PgDbClient,
permission: PermissionType,
) -> anyhow::Result<bool> {
if repo_state
.permissions
.load()
.has_permission(author.id, permission.clone())
{
return Ok(true);
}

let pr_model = db
.get_or_create_pull_request(repo_state.repository(), pr.number)
.await?;
let is_delegated = pr_model.delegated && author.id == pr.author.id;

Ok(is_delegated)
}

#[cfg(test)]
mod tests {
use crate::tests::mocks::{run_test, Comment, User};
Expand Down
Loading