-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Parses the `@bot transfer reponame` command. | ||
use crate::error::Error; | ||
use crate::token::{Token, Tokenizer}; | ||
use std::fmt; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct TransferCommand(pub String); | ||
|
||
#[derive(Debug)] | ||
pub enum ParseError { | ||
MissingRepo, | ||
} | ||
|
||
impl std::error::Error for ParseError {} | ||
|
||
impl fmt::Display for ParseError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
ParseError::MissingRepo => write!(f, "missing repository name"), | ||
} | ||
} | ||
} | ||
|
||
impl TransferCommand { | ||
pub fn parse<'a>(input: &mut Tokenizer<'a>) -> Result<Option<Self>, Error<'a>> { | ||
if !matches!(input.peek_token()?, Some(Token::Word("transfer"))) { | ||
return Ok(None); | ||
} | ||
input.next_token()?; | ||
let repo = if let Some(Token::Word(repo)) = input.next_token()? { | ||
repo.to_owned() | ||
} else { | ||
return Err(input.error(ParseError::MissingRepo)); | ||
}; | ||
Ok(Some(TransferCommand(repo))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! Handles the `@rustbot transfer reponame` command to transfer an issue to | ||
//! another repository. | ||
use crate::{config::TransferConfig, github::Event, handlers::Context}; | ||
use parser::command::transfer::TransferCommand; | ||
|
||
pub(super) async fn handle_command( | ||
ctx: &Context, | ||
_config: &TransferConfig, | ||
event: &Event, | ||
input: TransferCommand, | ||
) -> anyhow::Result<()> { | ||
let issue = event.issue().unwrap(); | ||
if issue.is_pr() { | ||
issue | ||
.post_comment(&ctx.github, "Only issues can be transferred.") | ||
.await?; | ||
return Ok(()); | ||
} | ||
if !event | ||
.user() | ||
.is_team_member(&ctx.github) | ||
.await | ||
.ok() | ||
.unwrap_or(false) | ||
{ | ||
issue | ||
.post_comment( | ||
&ctx.github, | ||
"Only team members may use the `transfer` command.", | ||
) | ||
.await?; | ||
return Ok(()); | ||
} | ||
|
||
let repo = input.0; | ||
let repo = repo.strip_prefix("rust-lang/").unwrap_or(&repo); | ||
if repo.contains('/') { | ||
issue | ||
.post_comment(&ctx.github, "Cross-organization transfers are not allowed.") | ||
.await?; | ||
return Ok(()); | ||
} | ||
|
||
if let Err(e) = issue.transfer(&ctx.github, "rust-lang", &repo).await { | ||
issue | ||
.post_comment(&ctx.github, &format!("Failed to transfer issue:\n{e:?}")) | ||
.await?; | ||
return Ok(()); | ||
} | ||
|
||
Ok(()) | ||
} |