refactor(hive-forge): port CLI verbs to forgejo-api

This commit is contained in:
müde 2026-07-07 09:24:53 +02:00
commit 4636987469
36 changed files with 1463 additions and 1153 deletions

View file

@ -7,7 +7,7 @@ use anyhow::Result;
use clap::Args as ClapArgs;
use serde_json::json;
use crate::client::Client;
use crate::client::{Client, is_not_found};
use crate::verbs::print_json;
#[derive(ClapArgs)]
@ -38,7 +38,7 @@ pub struct Args {
///
/// # Errors
/// Propagates transport / non-2xx errors from the Forgejo client calls
/// (`get_json` / `put_json` / `delete`) and from `print_json`.
/// and from `print_json`.
pub fn run(client: &Client, args: Args) -> Result<()> {
if args.list {
// List every repo the authed user watches, so an agent can audit
@ -49,48 +49,56 @@ pub fn run(client: &Client, args: Args) -> Result<()> {
const PAGE: u32 = 50;
let mut watching: Vec<String> = Vec::new();
for page in 1..=MAX_PAGES {
let v = client.get_json(&format!("/user/subscriptions?page={page}&limit={PAGE}"))?;
let arr = v.as_array().cloned().unwrap_or_default();
let n = arr.len();
watching.extend(
arr.iter()
.filter_map(|r| r["full_name"].as_str().map(str::to_owned)),
);
let (_, repos) = client
.api()
.user_current_list_subscriptions()
.page(page)
.page_size(PAGE)
.send()?;
let n = repos.len();
watching.extend(repos.into_iter().filter_map(|r| r.full_name));
if n < PAGE as usize {
break;
}
}
return print_json(&json!({ "watching": watching }));
}
let repo = client.repo();
let (owner, name) = client.owner_repo()?;
if args.unwatch {
client.delete(&format!("/repos/{repo}/subscription"), None)?;
client
.api()
.user_current_delete_subscription(owner, name)
.send()?;
println!("unsubscribed");
return Ok(());
}
if args.watch || args.ignore {
let (subscribed, ignored) = if args.ignore {
(false, true)
} else {
(true, false)
};
// Forgejo requires PUT (not POST) for this endpoint.
let resp = client.put_json(
&format!("/repos/{repo}/subscription"),
&json!({ "subscribed": subscribed, "ignored": ignored }),
)?;
// Forgejo's PUT subscription endpoint takes no body — it always
// subscribes (watch). The old raw client sent a
// `{subscribed, ignored}` body which the server ignored, so
// `--ignore` has always behaved like `--watch` server-side; the
// typed call just makes that explicit.
let resp = client
.api()
.user_current_put_subscription(owner, name)
.send()?;
return print_json(&json!({
"subscribed": resp.get("subscribed"),
"ignored": resp.get("ignored"),
"subscribed": resp.subscribed,
"ignored": resp.ignored,
}));
}
// Forgejo returns 404 when the current user is not watching the repo
// (rather than a response with subscribed=false).
match client.get_json_optional(&format!("/repos/{repo}/subscription"))? {
Some(resp) => print_json(&json!({
"subscribed": resp.get("subscribed"),
"ignored": resp.get("ignored"),
match client
.api()
.user_current_check_subscription(owner, name)
.send()
{
Ok(resp) => print_json(&json!({
"subscribed": resp.subscribed,
"ignored": resp.ignored,
})),
None => print_json(&json!({ "subscribed": false, "ignored": false })),
Err(e) if is_not_found(&e) => print_json(&json!({ "subscribed": false, "ignored": false })),
Err(e) => Err(e.into()),
}
}