hive-forge: hard-error unresolved --label names; render dependency timeline events
This commit is contained in:
parent
10285ff764
commit
7bad72d354
4 changed files with 134 additions and 20 deletions
|
|
@ -46,7 +46,7 @@ pub fn run(client: &Client, args: Args) -> Result<()> {
|
|||
bail!("hive-forge labels add: pass at least one label name");
|
||||
}
|
||||
let all = repo_labels(client)?;
|
||||
let ids: Vec<serde_json::Value> = resolve_ids(&all, &labels)
|
||||
let ids: Vec<serde_json::Value> = resolve_ids(&all, &labels)?
|
||||
.into_iter()
|
||||
.map(|id| json!(id))
|
||||
.collect();
|
||||
|
|
@ -104,11 +104,36 @@ pub(crate) fn repo_labels(client: &Client) -> Result<Vec<Label>> {
|
|||
Ok(labels)
|
||||
}
|
||||
|
||||
/// Resolve label names to ids, silently dropping any name that doesn't
|
||||
/// match a repo label (same behavior as `labels add`/`remove` above —
|
||||
/// keeps this one non-fatal-typo policy in one place).
|
||||
pub(crate) fn resolve_ids(all: &[Label], names: &[String]) -> Vec<i64> {
|
||||
names.iter().filter_map(|n| lookup_id(all, n)).collect()
|
||||
/// Resolve label names to ids, hard-erroring if any name doesn't match an
|
||||
/// existing repo label. A typo used to silently produce fewer labels than
|
||||
/// intended with no signal — not even a nonzero exit code — so callers
|
||||
/// (`labels add`, `issue-create --label`, `pr-create --label`) had no way
|
||||
/// to notice without manually diffing what they asked for against what
|
||||
/// landed. The error lists both the exact names that didn't resolve and
|
||||
/// every label actually available on the repo, so it's fixable from the
|
||||
/// error alone without a second round-trip to `labels list`.
|
||||
pub(crate) fn resolve_ids(all: &[Label], names: &[String]) -> Result<Vec<i64>> {
|
||||
let mut ids = Vec::with_capacity(names.len());
|
||||
let mut unresolved = Vec::new();
|
||||
for n in names {
|
||||
match lookup_id(all, n) {
|
||||
Some(id) => ids.push(id),
|
||||
None => unresolved.push(n.as_str()),
|
||||
}
|
||||
}
|
||||
if !unresolved.is_empty() {
|
||||
let available: Vec<&str> = all.iter().filter_map(|l| l.name.as_deref()).collect();
|
||||
bail!(
|
||||
"unresolved label name(s): {} — available labels: {}",
|
||||
unresolved.join(", "),
|
||||
if available.is_empty() {
|
||||
"(none)".to_owned()
|
||||
} else {
|
||||
available.join(", ")
|
||||
}
|
||||
);
|
||||
}
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
fn lookup_id(all: &[Label], name: &str) -> Option<i64> {
|
||||
|
|
@ -121,3 +146,44 @@ fn print_label_names(labels: &[Label]) {
|
|||
let names: Vec<&str> = labels.iter().filter_map(|l| l.name.as_deref()).collect();
|
||||
let _ = print_json(&json!(names));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::resolve_ids;
|
||||
use forgejo_api::structs::Label;
|
||||
|
||||
fn label(id: i64, name: &str) -> Label {
|
||||
Label {
|
||||
color: Some(String::new()),
|
||||
description: Some(String::new()),
|
||||
exclusive: None,
|
||||
id: Some(id),
|
||||
is_archived: None,
|
||||
name: Some(name.to_owned()),
|
||||
url: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_names_resolve_returns_ids_in_order() {
|
||||
let all = vec![label(1, "area/ops"), label(2, "type/bug")];
|
||||
let ids = resolve_ids(&all, &["type/bug".to_owned(), "area/ops".to_owned()]).unwrap();
|
||||
assert_eq!(ids, vec![2, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unresolved_name_errors_listing_the_typo_and_available_labels() {
|
||||
let all = vec![label(1, "area/ops"), label(2, "type/bug")];
|
||||
let err = resolve_ids(&all, &["area/op".to_owned()]).unwrap_err();
|
||||
let msg = err.to_string();
|
||||
assert!(msg.contains("area/op"), "missing typo'd name: {msg}");
|
||||
assert!(msg.contains("area/ops"), "missing available label: {msg}");
|
||||
assert!(msg.contains("type/bug"), "missing available label: {msg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unresolved_name_on_empty_repo_says_none_available() {
|
||||
let err = resolve_ids(&[], &["anything".to_owned()]).unwrap_err();
|
||||
assert!(err.to_string().contains("(none)"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue