hyperhive/hive-forge/src/verbs/assign.rs
atlas dc4c5460d5 feat(#1877): restructure hive-forge into pr/issue sub-verbs
Group issue/PR operations under `pr` and `issue` parent commands
(`hive-forge pr close 42`, `issue create …`, `pr status --pr 42`) per the
operator decision — kind-namespaced verbs replace the flat surface.

- new `verbs::pr_cmd` / `verbs::issue_cmd` parent commands wrap the existing
  per-verb modules (reuse their Args + run fns) under `#[command(subcommand)]`.
- kind-validation (the win over the old generic verbs): the generics that work
  on both (view/comment/comments/close/labels/assign/timeline) call
  `assert_kind` first, so `pr close <issue>` / `issue close <pr>` are rejected
  with a 'use the other command' message. PR-only / issue-only verbs are
  kind-correct by construction. `number` exposed `pub(crate)` on the shared
  verbs so the wrappers can probe it.
- every flat kind verb (`close`, `pr-create`, `pr-status`, `issue-edit`, …)
  kept as a `#[command(hide = true)]` back-compat alias — still parses, dropped
  from --help; removed in a later sweep once usage migrates. (`pr`/`issue`
  bare-show become `pr show` / `issue show` — the names are now parents.)
- docs/tools/forge.md documents the new surface + the deprecated aliases.

cargo build/clippy/fmt clean, 54 tests pass; --help surface + alias parsing
smoke-tested.
2026-06-22 16:01:13 +02:00

58 lines
1.8 KiB
Rust

//! `assign <number> <user> [--remove]` — add or remove a user from an
//! issue/PR's assignee list. Forgejo has no dedicated POST endpoint —
//! we read the current list, mutate, and PATCH the issue back (Forgejo
//! has no such endpoint; matches the bash helper's logic).
use anyhow::Result;
use clap::Args as ClapArgs;
use serde_json::{Value, json};
use crate::client::Client;
use crate::verbs::print_json;
#[derive(ClapArgs)]
pub struct Args {
/// Issue or PR number.
pub(crate) number: u64,
/// User login to assign (or unassign with `--remove`).
user: String,
/// Remove the user instead of adding.
#[arg(long)]
remove: bool,
}
pub fn run(client: &Client, args: Args) -> Result<()> {
let repo = client.repo();
let current = client.get_json(&format!("/repos/{repo}/issues/{}", args.number))?;
let mut assignees: Vec<String> = current
.get("assignees")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|u| u.get("login").and_then(Value::as_str).map(str::to_owned))
.collect()
})
.unwrap_or_default();
if args.remove {
assignees.retain(|u| u != &args.user);
} else if !assignees.contains(&args.user) {
assignees.push(args.user.clone());
}
let resp = client.patch_json(
&format!("/repos/{repo}/issues/{}", args.number),
&json!({ "assignees": assignees }),
)?;
let logins: Vec<&str> = resp
.get("assignees")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|u| u.get("login").and_then(Value::as_str))
.collect()
})
.unwrap_or_default();
print_json(&json!({
"number": resp.get("number"),
"assignees": logins,
}))
}