Skip to content

Commit 87d1c44

Browse files
committed
Move InvalidAssignee to UserError
1 parent a10c9c8 commit 87d1c44

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

src/errors.rs

Lines changed: 29 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
}
@@ -76,6 +79,32 @@ where
7679
}
7780
}
7881

82+
/// Represent an error when trying to assign someone
83+
#[derive(Debug)]
84+
pub enum AssignmentError {
85+
InvalidAssignee,
86+
Other(anyhow::Error),
87+
}
88+
89+
// NOTE: This is used to post the Github comment; make sure it's valid markdown.
90+
impl fmt::Display for AssignmentError {
91+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92+
match self {
93+
AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
94+
AssignmentError::Other(err) => write!(f, "{err}"),
95+
}
96+
}
97+
}
98+
99+
impl From<AssignmentError> for anyhow::Error {
100+
fn from(a: AssignmentError) -> Self {
101+
match a {
102+
AssignmentError::InvalidAssignee => UserError::InvalidAssignee.into(),
103+
AssignmentError::Other(err) => err.context("assignment error"),
104+
}
105+
}
106+
}
107+
79108
#[cfg(test)]
80109
mod tests {
81110
use super::*;

src/github.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::errors::UserError;
1+
use crate::errors::{AssignmentError, UserError};
22
use crate::team_data::TeamClient;
33
use anyhow::Context;
44
use async_trait::async_trait;
@@ -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,
@@ -916,12 +899,14 @@ 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+
.map_err(AssignmentError::Other)?;
909+
925910
Ok(())
926911
}
927912

@@ -945,7 +930,8 @@ impl Issue {
945930
let result: Issue = client
946931
.json(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
947932
.await
948-
.map_err(AssignmentError::Http)?;
933+
.map_err(AssignmentError::Other)?;
934+
949935
// Invalid assignees are silently ignored. We can just check if the user is now
950936
// contained in the assignees list.
951937
let success = result

src/handlers/assign.rs

Lines changed: 3 additions & 6 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, AssignmentError, user_error};
2626
use crate::github::UserId;
2727
use crate::handlers::pr_tracking::ReviewerWorkqueue;
2828
use crate::{
@@ -656,11 +656,8 @@ 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) => {
660-
issue
661-
.set_assignee(&ctx.github, &ctx.username)
662-
.await
663-
.context("self-assignment failed")?;
659+
Err(AssignmentError::InvalidAssignee) => {
660+
issue.set_assignee(&ctx.github, &ctx.username).await?;
664661
let cmt_body = format!(
665662
"This issue has been assigned to @{to_assign} via [this comment]({}).",
666663
event.html_url().unwrap()

0 commit comments

Comments
 (0)