Skip to content

Commit ced76f0

Browse files
committed
Simplify issue handling in relabel and add comments
1 parent 18dabcf commit ced76f0

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/handlers/relabel.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ pub(super) async fn handle_command(
2727
event: &Event,
2828
input: RelabelCommand,
2929
) -> anyhow::Result<()> {
30+
let Some(issue) = event.issue() else {
31+
anyhow::bail!("event is not an issue");
32+
};
33+
34+
// Check label authorization for the current user
3035
for delta in &input.0 {
3136
let name = delta.label().as_str();
3237
let err = match check_filter(name, config, is_member(&event.user(), &ctx.team).await) {
@@ -43,48 +48,37 @@ pub(super) async fn handle_command(
4348
Err(err) => Some(err),
4449
};
4550
if let Some(msg) = err {
46-
let cmnt = ErrorComment::new(&event.issue().unwrap(), msg);
51+
let cmnt = ErrorComment::new(issue, msg);
4752
cmnt.post(&ctx.github).await?;
4853
return Ok(());
4954
}
5055
}
5156

57+
// Compute the labels to add and remove
5258
let (to_add, to_remove) = compute_label_deltas(&input.0);
5359

54-
if let Err(e) = event
55-
.issue()
56-
.unwrap()
57-
.add_labels(&ctx.github, to_add.clone())
58-
.await
59-
{
60+
// Add labels
61+
if let Err(e) = issue.add_labels(&ctx.github, to_add.clone()).await {
6062
tracing::error!(
6163
"failed to add {:?} from issue {}: {:?}",
6264
to_add,
63-
event.issue().unwrap().global_id(),
65+
issue.global_id(),
6466
e
6567
);
6668
if let Some(err @ UnknownLabels { .. }) = e.downcast_ref() {
67-
event
68-
.issue()
69-
.unwrap()
70-
.post_comment(&ctx.github, &err.to_string())
71-
.await?;
69+
issue.post_comment(&ctx.github, &err.to_string()).await?;
7270
}
7371

7472
return Err(e);
7573
}
7674

75+
// Remove labels
7776
for label in to_remove {
78-
if let Err(e) = event
79-
.issue()
80-
.unwrap()
81-
.remove_label(&ctx.github, &label.name)
82-
.await
83-
{
77+
if let Err(e) = issue.remove_label(&ctx.github, &label.name).await {
8478
tracing::error!(
8579
"failed to remove {:?} from issue {}: {:?}",
8680
label,
87-
event.issue().unwrap().global_id(),
81+
issue.global_id(),
8882
e
8983
);
9084
return Err(e);

0 commit comments

Comments
 (0)