Skip to content

Commit 7a6ffb3

Browse files
committed
Move InvalidAssignee to UserError
1 parent a10c9c8 commit 7a6ffb3

File tree

3 files changed

+22
-37
lines changed

3 files changed

+22
-37
lines changed

src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub enum UserError {
1818
Message(String),
1919
/// Unknown labels
2020
UnknownLabels { labels: Vec<String> },
21+
/// Invalid assignee
22+
InvalidAssignee,
2123
}
2224

2325
impl std::error::Error for UserError {}
@@ -30,6 +32,7 @@ impl fmt::Display for UserError {
3032
UserError::UnknownLabels { labels } => {
3133
write!(f, "Unknown labels: {}", labels.join(", "))
3234
}
35+
UserError::InvalidAssignee => write!(f, "invalid assignee"),
3336
}
3437
}
3538
}

src/github.rs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -547,30 +547,13 @@ where
547547
}
548548
}
549549

550-
#[derive(Debug)]
551-
pub enum AssignmentError {
552-
InvalidAssignee,
553-
Http(anyhow::Error),
554-
}
555-
556550
#[derive(Debug)]
557551
pub enum Selection<'a, T: ?Sized> {
558552
All,
559553
One(&'a T),
560554
Except(&'a T),
561555
}
562556

563-
impl fmt::Display for AssignmentError {
564-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
565-
match self {
566-
AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
567-
AssignmentError::Http(e) => write!(f, "cannot assign: {e}"),
568-
}
569-
}
570-
}
571-
572-
impl std::error::Error for AssignmentError {}
573-
574557
#[derive(Debug, Clone, PartialEq, Eq)]
575558
pub struct IssueRepository {
576559
pub organization: String,
@@ -889,7 +872,7 @@ impl Issue {
889872
&self,
890873
client: &GithubClient,
891874
selection: Selection<'_, str>,
892-
) -> Result<(), AssignmentError> {
875+
) -> anyhow::Result<()> {
893876
log::info!("remove {:?} assignees for {}", selection, self.global_id());
894877
let url = format!(
895878
"{repo_url}/issues/{number}/assignees",
@@ -916,20 +899,18 @@ impl Issue {
916899
struct AssigneeReq<'a> {
917900
assignees: &'a [&'a str],
918901
}
902+
919903
client
920904
.send_req(client.delete(&url).json(&AssigneeReq {
921905
assignees: &assignees[..],
922906
}))
923907
.await
924-
.map_err(AssignmentError::Http)?;
908+
.context("failed to remove assignees")?;
909+
925910
Ok(())
926911
}
927912

928-
pub async fn add_assignee(
929-
&self,
930-
client: &GithubClient,
931-
user: &str,
932-
) -> Result<(), AssignmentError> {
913+
pub async fn add_assignee(&self, client: &GithubClient, user: &str) -> anyhow::Result<()> {
933914
log::info!("add_assignee {} for {}", user, self.global_id());
934915
let url = format!(
935916
"{repo_url}/issues/{number}/assignees",
@@ -944,27 +925,23 @@ impl Issue {
944925

945926
let result: Issue = client
946927
.json(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
947-
.await
948-
.map_err(AssignmentError::Http)?;
928+
.await?;
929+
949930
// Invalid assignees are silently ignored. We can just check if the user is now
950931
// contained in the assignees list.
951932
let success = result
952933
.assignees
953934
.iter()
954935
.any(|u| u.login.as_str().to_lowercase() == user.to_lowercase());
955936

956-
if success {
957-
Ok(())
958-
} else {
959-
Err(AssignmentError::InvalidAssignee)
937+
if !success {
938+
anyhow::bail!(UserError::InvalidAssignee);
960939
}
940+
941+
Ok(())
961942
}
962943

963-
pub async fn set_assignee(
964-
&self,
965-
client: &GithubClient,
966-
user: &str,
967-
) -> Result<(), AssignmentError> {
944+
pub async fn set_assignee(&self, client: &GithubClient, user: &str) -> anyhow::Result<()> {
968945
log::info!("set_assignee for {} to {}", self.global_id(), user);
969946
self.add_assignee(client, user).await?;
970947
self.remove_assignees(client, Selection::Except(user))

src/handlers/assign.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
2323
use crate::db::issue_data::IssueData;
2424
use crate::db::review_prefs::{RotationMode, get_review_prefs_batch};
25-
use crate::errors::{self, user_error};
25+
use crate::errors::{self, UserError, user_error};
2626
use crate::github::UserId;
2727
use crate::handlers::pr_tracking::ReviewerWorkqueue;
2828
use crate::{
@@ -656,7 +656,12 @@ pub(super) async fn handle_command(
656656
e.apply(&ctx.github, String::new()).await?;
657657
return Ok(());
658658
} // we are done
659-
Err(github::AssignmentError::InvalidAssignee) => {
659+
Err(err)
660+
if matches!(
661+
err.downcast_ref::<UserError>(),
662+
Some(UserError::InvalidAssignee)
663+
) =>
664+
{
660665
issue
661666
.set_assignee(&ctx.github, &ctx.username)
662667
.await

0 commit comments

Comments
 (0)