Skip to content

Commit d4484b6

Browse files
committed
Comment on issues when @rustbot label is given an invalid label
Previously, the label was just silently ignored, along with all other labels in the same command. This tells the user what went wrong, and also adds all valid labels
1 parent daaf943 commit d4484b6

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/github.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,18 @@ impl IssueRepository {
366366
)
367367
}
368368

369-
async fn has_label(&self, client: &GithubClient, label: &str) -> bool {
369+
async fn has_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<bool> {
370370
#[allow(clippy::redundant_pattern_matching)]
371371
let url = format!("{}/labels/{}", self.url(), label);
372-
match client.send_req(client.get(&url)).await {
373-
Ok(_) => true,
374-
// XXX: Error handling if the request failed for reasons beyond 'label didn't exist'
375-
Err(_) => false,
372+
match client._send_req(client.get(&url)).await {
373+
Ok((_, _)) => Ok(true),
374+
Err(e) => {
375+
if e.downcast_ref::<reqwest::Error>().map_or(false, |e| e.status() == Some(StatusCode::NOT_FOUND)) {
376+
Ok(false)
377+
} else {
378+
Err(e)
379+
}
380+
}
376381
}
377382
}
378383
}
@@ -519,20 +524,32 @@ impl Issue {
519524
return Ok(());
520525
}
521526

522-
for label in &labels {
523-
if !self.repository().has_label(client, &label).await {
524-
anyhow::bail!("Label {} does not exist in {}", label, self.global_id());
527+
let mut unknown_labels = vec![];
528+
let mut known_labels = vec![];
529+
for label in labels {
530+
if !self.repository().has_label(client, &label).await? {
531+
unknown_labels.push(label);
532+
} else {
533+
known_labels.push(label);
525534
}
526535
}
527536

528537
#[derive(serde::Serialize)]
529538
struct LabelsReq {
530539
labels: Vec<String>,
531540
}
532-
client
533-
._send_req(client.post(&url).json(&LabelsReq { labels }))
534-
.await
535-
.context("failed to add labels")?;
541+
542+
if !known_labels.is_empty() {
543+
client
544+
._send_req(client.post(&url).json(&LabelsReq { labels: known_labels }))
545+
.await
546+
.context("failed to add labels")?;
547+
}
548+
549+
if !unknown_labels.is_empty() {
550+
let comment = String::from("Unknown labels:\n- ") + &unknown_labels.join("\n- ");
551+
self.post_comment(client, &comment).await.context("failed to post missing label comment")?;
552+
}
536553

537554
Ok(())
538555
}

src/interactions.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::github::{GithubClient, Issue};
22
use std::fmt::Write;
3-
use tracing as log;
43

54
pub struct ErrorComment<'a> {
65
issue: &'a Issue,

0 commit comments

Comments
 (0)