Skip to content

Commit ca04ebf

Browse files
committed
Return an error for ambiguous label requests
1 parent 3edd210 commit ca04ebf

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/github.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,26 @@ impl fmt::Display for UnknownLabels {
588588

589589
impl std::error::Error for UnknownLabels {}
590590

591+
#[derive(Debug)]
592+
pub(crate) struct AmbiguousLabelMatch {
593+
pub requested_label: String,
594+
pub labels: Vec<String>,
595+
}
596+
597+
// NOTE: This is used to post the Github comment; make sure it's valid markdown.
598+
impl fmt::Display for AmbiguousLabelMatch {
599+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
600+
write!(
601+
f,
602+
"Unsure which label to use for `{}` - could be one of: {}",
603+
self.requested_label,
604+
self.labels.join(", ")
605+
)
606+
}
607+
}
608+
609+
impl std::error::Error for AmbiguousLabelMatch {}
610+
591611
impl Issue {
592612
pub fn to_zulip_github_reference(&self) -> ZulipGitHubReference {
593613
ZulipGitHubReference {
@@ -742,14 +762,24 @@ impl Issue {
742762
}
743763

744764
// Try normalizing requested label (remove emoji, case insensitive, trim whitespace)
745-
let normalized_requested = normalize(requested_label);
746-
if let Some(found) = available_labels
765+
let normalized_requested: String = normalize(requested_label);
766+
767+
// Find matching labels by normalized name
768+
let found = available_labels
747769
.iter()
748-
.find(|l| normalize(&l.name) == normalized_requested)
749-
{
750-
found_labels.push(&found.name);
751-
} else {
770+
.filter(|l| normalize(&l.name) == normalized_requested)
771+
.collect::<Vec<_>>();
772+
773+
if found.is_empty() {
752774
unknown_labels.push(requested_label.as_str());
775+
} else if found.len() > 1 {
776+
return Err(AmbiguousLabelMatch {
777+
requested_label: requested_label.clone(),
778+
labels: found.into_iter().map(|l| l.name.clone()).collect(),
779+
}
780+
.into());
781+
} else {
782+
found_labels.push(&found.first().unwrap().name);
753783
}
754784
}
755785

0 commit comments

Comments
 (0)