Skip to content

Commit 89cd553

Browse files
Merge pull request #14 from spastorino/allow-removing-my-assignments
Allow to unassign myself from issues
2 parents 3dd3b80 + fcfc436 commit 89cd553

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

src/github.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use reqwest::{Client, Error as HttpError, RequestBuilder, Response, StatusCode};
44
use std::fmt;
55
use std::io::Read;
66

7-
#[derive(Debug, serde::Deserialize)]
7+
#[derive(Debug, PartialEq, Eq, serde::Deserialize)]
88
pub struct User {
99
pub login: String,
1010
}
@@ -76,6 +76,11 @@ pub enum AssignmentError {
7676
Http(HttpError),
7777
}
7878

79+
pub enum Selection<'a, T> {
80+
All,
81+
One(&'a T),
82+
}
83+
7984
impl fmt::Display for AssignmentError {
8085
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8186
match self {
@@ -180,25 +185,38 @@ impl Issue {
180185
&self.labels
181186
}
182187

183-
pub fn remove_assignees(&self, client: &GithubClient) -> Result<(), AssignmentError> {
188+
pub fn contain_assignee(&self, user: &User) -> bool {
189+
self.assignees.contains(user)
190+
}
191+
192+
pub fn remove_assignees(
193+
&self,
194+
client: &GithubClient,
195+
selection: Selection<User>,
196+
) -> Result<(), AssignmentError> {
184197
let url = format!(
185198
"{repo_url}/issues/{number}/assignees",
186199
repo_url = self.repository_url,
187200
number = self.number
188201
);
189202

203+
let assignees = match selection {
204+
Selection::All => self
205+
.assignees
206+
.iter()
207+
.map(|u| u.login.as_str())
208+
.collect::<Vec<_>>(),
209+
Selection::One(user) => vec![user.login.as_str()],
210+
};
211+
190212
#[derive(serde::Serialize)]
191213
struct AssigneeReq<'a> {
192214
assignees: &'a [&'a str],
193215
}
194216
client
195217
.delete(&url)
196218
.json(&AssigneeReq {
197-
assignees: &self
198-
.assignees
199-
.iter()
200-
.map(|u| u.login.as_str())
201-
.collect::<Vec<_>>()[..],
219+
assignees: &assignees[..],
202220
})
203221
.send_req()
204222
.map_err(AssignmentError::Http)?;
@@ -229,7 +247,7 @@ impl Issue {
229247
Err(e) => return Err(AssignmentError::Http(e)),
230248
}
231249

232-
self.remove_assignees(client)?;
250+
self.remove_assignees(client, Selection::All)?;
233251

234252
#[derive(serde::Serialize)]
235253
struct AssigneeReq<'a> {

src/handlers/assign.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
use crate::{
1515
config::AssignConfig,
16-
github::{self, Event},
16+
github::{self, Event, Selection},
1717
handlers::{Context, Handler},
1818
interactions::EditIssueBody,
1919
};
@@ -88,18 +88,29 @@ impl Handler for AssignmentHandler {
8888
username.clone()
8989
}
9090
AssignCommand::Release => {
91-
let current = if let Some(AssignData { user: Some(user) }) = e.current_data() {
92-
user
91+
if let Some(AssignData {
92+
user: Some(current),
93+
}) = e.current_data()
94+
{
95+
if current == event.comment.user.login || is_team_member {
96+
event.issue.remove_assignees(&ctx.github, Selection::All)?;
97+
e.apply(&ctx.github, String::new(), AssignData { user: None })?;
98+
return Ok(());
99+
} else {
100+
failure::bail!("Cannot release another user's assignment");
101+
}
93102
} else {
94-
failure::bail!("Cannot release unassigned issue");
103+
let current = &event.comment.user;
104+
if event.issue.contain_assignee(current) {
105+
event
106+
.issue
107+
.remove_assignees(&ctx.github, Selection::One(&current))?;
108+
e.apply(&ctx.github, String::new(), AssignData { user: None })?;
109+
return Ok(());
110+
} else {
111+
failure::bail!("Cannot release unassigned issue");
112+
}
95113
};
96-
if current == event.comment.user.login || is_team_member {
97-
event.issue.remove_assignees(&ctx.github)?;
98-
e.apply(&ctx.github, String::new(), AssignData { user: None })?;
99-
return Ok(());
100-
} else {
101-
failure::bail!("Cannot release another user's assignment");
102-
}
103114
}
104115
};
105116
let data = AssignData {

0 commit comments

Comments
 (0)