Skip to content

Commit f9c5bac

Browse files
committed
Revert "Merge pull request rust-lang#2128 from kailan/normalize-labels"
This reverts commit 72dbff7, reversing changes made to 4c371f1. It doesn't account for the labels endpoint page limit.
1 parent 72dbff7 commit f9c5bac

File tree

5 files changed

+70
-220
lines changed

5 files changed

+70
-220
lines changed

src/github.rs

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use std::{
1616
};
1717
use tracing as log;
1818

19-
pub mod labels;
20-
2119
pub type UserId = u64;
2220
pub type PullRequestNumber = u64;
2321

@@ -567,15 +565,38 @@ impl IssueRepository {
567565
format!("{}/{}", self.organization, self.repository)
568566
}
569567

570-
async fn labels(&self, client: &GithubClient) -> anyhow::Result<Vec<Label>> {
571-
let url = format!("{}/labels", self.url(client));
572-
client
573-
.json(client.get(&url))
574-
.await
575-
.context("failed to get labels")
568+
async fn has_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<bool> {
569+
#[allow(clippy::redundant_pattern_matching)]
570+
let url = format!("{}/labels/{}", self.url(client), label);
571+
match client.send_req(client.get(&url)).await {
572+
Ok(_) => Ok(true),
573+
Err(e) => {
574+
if e.downcast_ref::<reqwest::Error>()
575+
.map_or(false, |e| e.status() == Some(StatusCode::NOT_FOUND))
576+
{
577+
Ok(false)
578+
} else {
579+
Err(e)
580+
}
581+
}
582+
}
576583
}
577584
}
578585

586+
#[derive(Debug)]
587+
pub(crate) struct UnknownLabels {
588+
labels: Vec<String>,
589+
}
590+
591+
// NOTE: This is used to post the Github comment; make sure it's valid markdown.
592+
impl fmt::Display for UnknownLabels {
593+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
594+
write!(f, "Unknown labels: {}", &self.labels.join(", "))
595+
}
596+
}
597+
598+
impl std::error::Error for UnknownLabels {}
599+
579600
impl Issue {
580601
pub fn to_zulip_github_reference(&self) -> ZulipGitHubReference {
581602
ZulipGitHubReference {
@@ -709,39 +730,8 @@ impl Issue {
709730
Ok(())
710731
}
711732

712-
async fn normalize_and_match_labels(
713-
&self,
714-
client: &GithubClient,
715-
requested_labels: &[&str],
716-
) -> anyhow::Result<Vec<String>> {
717-
let available_labels = self
718-
.repository()
719-
.labels(client)
720-
.await
721-
.context("unable to retrieve the repository labels")?;
722-
723-
labels::normalize_and_match_labels(
724-
&available_labels
725-
.iter()
726-
.map(|l| l.name.as_str())
727-
.collect::<Vec<_>>(),
728-
requested_labels,
729-
)
730-
}
731-
732733
pub async fn remove_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<()> {
733734
log::info!("remove_label from {}: {:?}", self.global_id(), label);
734-
735-
let normalized_labels = self.normalize_and_match_labels(client, &[label]).await?;
736-
let label = normalized_labels
737-
.first()
738-
.context("failed to find label on repository")?;
739-
log::info!(
740-
"remove_label from {}: matched label to {:?}",
741-
self.global_id(),
742-
label
743-
);
744-
745735
// DELETE /repos/:owner/:repo/issues/:number/labels/{name}
746736
let url = format!(
747737
"{repo_url}/issues/{number}/labels/{name}",
@@ -777,19 +767,6 @@ impl Issue {
777767
labels: Vec<Label>,
778768
) -> anyhow::Result<()> {
779769
log::info!("add_labels: {} +{:?}", self.global_id(), labels);
780-
781-
let labels = self
782-
.normalize_and_match_labels(
783-
client,
784-
&labels.iter().map(|l| l.name.as_str()).collect::<Vec<_>>(),
785-
)
786-
.await?;
787-
log::info!(
788-
"add_labels: {} matched requested labels to +{:?}",
789-
self.global_id(),
790-
labels
791-
);
792-
793770
// POST /repos/:owner/:repo/issues/:number/labels
794771
// repo_url = https://api.github.com/repos/Codertocat/Hello-World
795772
let url = format!(
@@ -801,7 +778,8 @@ impl Issue {
801778
// Don't try to add labels already present on this issue.
802779
let labels = labels
803780
.into_iter()
804-
.filter(|l| !self.labels().iter().any(|existing| existing.name == *l))
781+
.filter(|l| !self.labels().contains(&l))
782+
.map(|l| l.name)
805783
.collect::<Vec<_>>();
806784

807785
log::info!("add_labels: {} filtered to {:?}", self.global_id(), labels);
@@ -810,13 +788,32 @@ impl Issue {
810788
return Ok(());
811789
}
812790

791+
let mut unknown_labels = vec![];
792+
let mut known_labels = vec![];
793+
for label in labels {
794+
if !self.repository().has_label(client, &label).await? {
795+
unknown_labels.push(label);
796+
} else {
797+
known_labels.push(label);
798+
}
799+
}
800+
801+
if !unknown_labels.is_empty() {
802+
return Err(UnknownLabels {
803+
labels: unknown_labels,
804+
}
805+
.into());
806+
}
807+
813808
#[derive(serde::Serialize)]
814809
struct LabelsReq {
815810
labels: Vec<String>,
816811
}
817812

818813
client
819-
.send_req(client.post(&url).json(&LabelsReq { labels }))
814+
.send_req(client.post(&url).json(&LabelsReq {
815+
labels: known_labels,
816+
}))
820817
.await
821818
.context("failed to add labels")?;
822819

@@ -3220,3 +3217,16 @@ impl Submodule {
32203217
client.repository(fullname).await
32213218
}
32223219
}
3220+
3221+
#[cfg(test)]
3222+
mod tests {
3223+
use super::*;
3224+
3225+
#[test]
3226+
fn display_labels() {
3227+
let x = UnknownLabels {
3228+
labels: vec!["A-bootstrap".into(), "xxx".into()],
3229+
};
3230+
assert_eq!(x.to_string(), "Unknown labels: A-bootstrap, xxx");
3231+
}
3232+
}

src/github/labels.rs

Lines changed: 0 additions & 162 deletions
This file was deleted.

src/handlers/assign.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
2323
use crate::db::issue_data::IssueData;
2424
use crate::db::review_prefs::{RotationMode, get_review_prefs_batch};
25-
use crate::github::{UserId, labels};
25+
use crate::github::UserId;
2626
use crate::handlers::pr_tracking::ReviewerWorkqueue;
2727
use crate::{
2828
config::AssignConfig,
@@ -563,7 +563,7 @@ pub(super) async fn handle_command(
563563
.add_labels(&ctx.github, vec![github::Label { name: t_label }])
564564
.await
565565
{
566-
if let Some(labels::UnknownLabels { .. }) = err.downcast_ref() {
566+
if let Some(github::UnknownLabels { .. }) = err.downcast_ref() {
567567
log::warn!("Error assigning label: {}", err);
568568
} else {
569569
return Err(err);

src/handlers/autolabel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
config::AutolabelConfig,
3-
github::{IssuesAction, IssuesEvent, Label, labels::UnknownLabels},
3+
github::{IssuesAction, IssuesEvent, Label},
44
handlers::Context,
55
};
66
use anyhow::Context as _;
@@ -209,6 +209,7 @@ pub(super) async fn handle_input(
209209
match event.issue.add_labels(&ctx.github, input.add).await {
210210
Ok(()) => {}
211211
Err(e) => {
212+
use crate::github::UnknownLabels;
212213
if let Some(err @ UnknownLabels { .. }) = e.downcast_ref() {
213214
event
214215
.issue

src/handlers/relabel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use crate::team_data::TeamClient;
1212
use crate::{
1313
config::RelabelConfig,
14-
github::{self, Event, labels::UnknownLabels},
14+
github::UnknownLabels,
15+
github::{self, Event},
1516
handlers::Context,
1617
interactions::ErrorComment,
1718
};

0 commit comments

Comments
 (0)