Skip to content

Commit 3cefbd9

Browse files
committed
Apply suggestions from code review
1 parent 2cc8b9a commit 3cefbd9

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

src/github.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ use async_trait::async_trait;
44
use bytes::Bytes;
55
use chrono::{DateTime, FixedOffset, Utc};
66
use futures::{FutureExt, future::BoxFuture};
7+
use itertools::Itertools;
78
use octocrab::models::{Author, AuthorAssociation};
89
use regex::Regex;
910
use reqwest::header::{AUTHORIZATION, USER_AGENT};
1011
use reqwest::{Client, Request, RequestBuilder, Response, StatusCode};
1112
use std::collections::{HashMap, HashSet};
12-
use std::sync::OnceLock;
13+
use std::sync::{LazyLock, OnceLock};
1314
use std::{
1415
fmt,
1516
time::{Duration, SystemTime},
1617
};
1718
use tracing as log;
1819

20+
static EMOJI_REGEX: LazyLock<Regex> =
21+
LazyLock::new(|| Regex::new(r"[\p{Emoji}\p{Emoji_Presentation}]").unwrap());
22+
1923
pub type UserId = u64;
2024
pub type PullRequestNumber = u64;
2125

@@ -601,11 +605,7 @@ impl fmt::Display for AmbiguousLabelMatch {
601605
f,
602606
"Unsure which label to use for `{}` - could be one of: {}",
603607
self.requested_label,
604-
self.labels
605-
.iter()
606-
.map(|l| format!("`{}`", l))
607-
.collect::<Vec<_>>()
608-
.join(", ")
608+
self.labels.iter().map(|l| format!("`{}`", l)).join(", ")
609609
)
610610
}
611611
}
@@ -748,20 +748,23 @@ impl Issue {
748748
async fn normalize_and_match_labels(
749749
&self,
750750
client: &GithubClient,
751-
requested_labels: &[String],
751+
requested_labels: &[&str],
752752
) -> anyhow::Result<Vec<String>> {
753-
let available_labels = self.repository().labels(client).await.unwrap_or_default();
753+
let available_labels = self
754+
.repository()
755+
.labels(client)
756+
.await
757+
.context("unable to retrieve the repository labels")?;
754758

755-
let emoji_regex: Regex = Regex::new(r"[\p{Emoji}\p{Emoji_Presentation}]").unwrap();
756-
let normalize = |s: &str| emoji_regex.replace_all(s, "").trim().to_lowercase();
759+
let normalize = |s: &str| EMOJI_REGEX.replace_all(s, "").trim().to_lowercase();
757760

758761
let mut found_labels = Vec::with_capacity(requested_labels.len());
759762
let mut unknown_labels = Vec::new();
760763

761764
for requested_label in requested_labels {
762765
// First look for an exact match
763766
if let Some(found) = available_labels.iter().find(|l| l.name == *requested_label) {
764-
found_labels.push(&found.name);
767+
found_labels.push(found.name.clone());
765768
continue;
766769
}
767770

@@ -774,36 +777,40 @@ impl Issue {
774777
.filter(|l| normalize(&l.name) == normalized_requested)
775778
.collect::<Vec<_>>();
776779

777-
if found.is_empty() {
778-
unknown_labels.push(requested_label.as_str());
779-
} else if found.len() > 1 {
780-
return Err(AmbiguousLabelMatch {
781-
requested_label: requested_label.clone(),
782-
labels: found.into_iter().map(|l| l.name.clone()).collect(),
780+
match found[..] {
781+
[] => {
782+
unknown_labels.push(requested_label);
783783
}
784-
.into());
785-
} else {
786-
found_labels.push(&found.first().unwrap().name);
787-
}
784+
[label] => {
785+
found_labels.push(label.name.clone());
786+
}
787+
[..] => {
788+
return Err(AmbiguousLabelMatch {
789+
requested_label: requested_label.to_string(),
790+
labels: found.into_iter().map(|l| l.name.clone()).collect(),
791+
}
792+
.into());
793+
}
794+
};
788795
}
789796

790797
if !unknown_labels.is_empty() {
791798
return Err(UnknownLabels {
792-
labels: unknown_labels.into_iter().map(String::from).collect(),
799+
labels: unknown_labels.into_iter().map(|s| s.to_string()).collect(),
793800
}
794801
.into());
795802
}
796803

797-
Ok(found_labels.into_iter().map(|s| s.clone()).collect())
804+
Ok(found_labels)
798805
}
799806

800807
pub async fn remove_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<()> {
801808
log::info!("remove_label from {}: {:?}", self.global_id(), label);
802809

803-
let normalized_labels = self
804-
.normalize_and_match_labels(client, &[label.to_string()])
805-
.await?;
806-
let label = normalized_labels.first().unwrap();
810+
let normalized_labels = self.normalize_and_match_labels(client, &[label]).await?;
811+
let label = normalized_labels
812+
.first()
813+
.context("failed to find label on repository")?;
807814
log::info!(
808815
"remove_label from {}: matched label to {:?}",
809816
self.global_id(),
@@ -849,7 +856,7 @@ impl Issue {
849856
let labels = self
850857
.normalize_and_match_labels(
851858
client,
852-
&labels.into_iter().map(|l| l.name).collect::<Vec<_>>(),
859+
&labels.iter().map(|l| l.name.as_str()).collect::<Vec<_>>(),
853860
)
854861
.await?;
855862
log::info!(

0 commit comments

Comments
 (0)