-
Notifications
You must be signed in to change notification settings - Fork 94
Normalize labels before addition or removal #2128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3edd210
Normalize labels before addition or removal
kailan ca04ebf
Return an error for ambiguous label requests
kailan 2cc8b9a
Improve formatting of AmbiguousLabelMatch message
kailan 3cefbd9
Apply suggestions from code review
kailan 97736f5
Move label matching logic to new module with tests
kailan 01409ef
Relocate `labels` module inside of `github`
kailan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
use std::{fmt, sync::LazyLock}; | ||
|
||
use itertools::Itertools; | ||
use regex::Regex; | ||
|
||
static EMOJI_REGEX: LazyLock<Regex> = | ||
LazyLock::new(|| Regex::new(r"[\p{Emoji}\p{Emoji_Presentation}]").unwrap()); | ||
|
||
pub(crate) fn normalize_and_match_labels( | ||
available_labels: &[&str], | ||
requested_labels: &[&str], | ||
) -> anyhow::Result<Vec<String>> { | ||
let normalize = |s: &str| EMOJI_REGEX.replace_all(s, "").trim().to_lowercase(); | ||
|
||
let mut found_labels = Vec::<String>::with_capacity(requested_labels.len()); | ||
let mut unknown_labels = Vec::new(); | ||
|
||
for requested_label in requested_labels { | ||
// First look for an exact match | ||
if let Some(found) = available_labels.iter().find(|l| **l == *requested_label) { | ||
found_labels.push((*found).into()); | ||
continue; | ||
} | ||
|
||
// Try normalizing requested label (remove emoji, case insensitive, trim whitespace) | ||
let normalized_requested: String = normalize(requested_label); | ||
|
||
// Find matching labels by normalized name | ||
let found = available_labels | ||
.iter() | ||
.filter(|l| normalize(l) == normalized_requested) | ||
.collect::<Vec<_>>(); | ||
|
||
match found[..] { | ||
[] => { | ||
unknown_labels.push(requested_label); | ||
} | ||
[label] => { | ||
found_labels.push((*label).into()); | ||
} | ||
[..] => { | ||
return Err(AmbiguousLabelMatch { | ||
requested_label: requested_label.to_string(), | ||
labels: found.into_iter().map(|l| (*l).into()).collect(), | ||
} | ||
.into()); | ||
} | ||
}; | ||
} | ||
|
||
if !unknown_labels.is_empty() { | ||
return Err(UnknownLabels { | ||
labels: unknown_labels.iter().map(|s| s.to_string()).collect(), | ||
} | ||
.into()); | ||
} | ||
|
||
Ok(found_labels) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct UnknownLabels { | ||
labels: Vec<String>, | ||
} | ||
|
||
// NOTE: This is used to post the Github comment; make sure it's valid markdown. | ||
impl fmt::Display for UnknownLabels { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "Unknown labels: {}", &self.labels.join(", ")) | ||
} | ||
} | ||
|
||
impl std::error::Error for UnknownLabels {} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct AmbiguousLabelMatch { | ||
pub requested_label: String, | ||
pub labels: Vec<String>, | ||
} | ||
|
||
// NOTE: This is used to post the Github comment; make sure it's valid markdown. | ||
impl fmt::Display for AmbiguousLabelMatch { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!( | ||
f, | ||
"Unsure which label to use for `{}` - could be one of: {}", | ||
self.requested_label, | ||
self.labels.iter().map(|l| format!("`{}`", l)).join(", ") | ||
) | ||
} | ||
} | ||
|
||
impl std::error::Error for AmbiguousLabelMatch {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn display_unknown_labels_error() { | ||
let x = UnknownLabels { | ||
labels: vec!["A-bootstrap".into(), "xxx".into()], | ||
}; | ||
assert_eq!(x.to_string(), "Unknown labels: A-bootstrap, xxx"); | ||
} | ||
|
||
#[test] | ||
fn display_ambiguous_label_error() { | ||
let x = AmbiguousLabelMatch { | ||
requested_label: "A-bootstrap".into(), | ||
labels: vec!["A-bootstrap".into(), "A-bootstrap-2".into()], | ||
}; | ||
assert_eq!( | ||
x.to_string(), | ||
"Unsure which label to use for `A-bootstrap` - could be one of: `A-bootstrap`, `A-bootstrap-2`" | ||
); | ||
} | ||
|
||
#[test] | ||
fn normalize_and_match_labels_happy_path() { | ||
let available_labels = vec!["A-bootstrap 😺", "B-foo 👾", "C-bar", "C-bar 😦"]; | ||
let requested_labels = vec!["A-bootstrap", "B-foo", "C-bar"]; | ||
|
||
let result = normalize_and_match_labels(&available_labels, &requested_labels); | ||
|
||
assert!(result.is_ok()); | ||
let found_labels = result.unwrap(); | ||
assert_eq!(found_labels.len(), 3); | ||
assert_eq!(found_labels[0], "A-bootstrap 😺"); | ||
assert_eq!(found_labels[1], "B-foo 👾"); | ||
assert_eq!(found_labels[2], "C-bar"); | ||
} | ||
|
||
#[test] | ||
fn normalize_and_match_labels_no_match() { | ||
let available_labels = vec!["A-bootstrap", "B-foo"]; | ||
let requested_labels = vec!["A-bootstrap", "C-bar"]; | ||
|
||
let result = normalize_and_match_labels(&available_labels, &requested_labels); | ||
|
||
assert!(result.is_err()); | ||
let err = result.unwrap_err(); | ||
assert!(err.is::<UnknownLabels>()); | ||
let unknown = err.downcast::<UnknownLabels>().unwrap(); | ||
assert_eq!(unknown.labels, vec!["C-bar"]); | ||
} | ||
|
||
#[test] | ||
fn normalize_and_match_labels_ambiguous_match() { | ||
let available_labels = vec!["A-bootstrap 😺", "A-bootstrap 👾"]; | ||
let requested_labels = vec!["A-bootstrap"]; | ||
|
||
let result = normalize_and_match_labels(&available_labels, &requested_labels); | ||
|
||
assert!(result.is_err()); | ||
let err = result.unwrap_err(); | ||
assert!(err.is::<AmbiguousLabelMatch>()); | ||
let ambiguous = err.downcast::<AmbiguousLabelMatch>().unwrap(); | ||
assert_eq!(ambiguous.requested_label, "A-bootstrap"); | ||
assert_eq!(ambiguous.labels, vec!["A-bootstrap 😺", "A-bootstrap 👾"]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.