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

@ -9,7 +9,7 @@
use anyhow::Result;
use clap::Args as ClapArgs;
use clap::ValueEnum;
use serde_json::json;
use forgejo_api::structs::{AddCollaboratorOption, AddCollaboratorOptionPermission};
use crate::client::Client;
@ -25,7 +25,8 @@ pub enum Permission {
}
impl Permission {
/// The wire string Forgejo's API expects.
/// The wire string Forgejo's API expects (used for the printed
/// confirmation).
fn as_api(self) -> &'static str {
match self {
Permission::Read => "read",
@ -33,6 +34,15 @@ impl Permission {
Permission::Admin => "admin",
}
}
/// The typed permission for the request body.
fn as_option(self) -> AddCollaboratorOptionPermission {
match self {
Permission::Read => AddCollaboratorOptionPermission::Read,
Permission::Write => AddCollaboratorOptionPermission::Write,
Permission::Admin => AddCollaboratorOptionPermission::Admin,
}
}
}
#[derive(ClapArgs)]
@ -53,11 +63,19 @@ pub struct Args {
/// writing the confirmation to stdout.
pub fn run(client: &Client, args: Args) -> Result<()> {
let repo = client.repo();
let (owner, name) = client.owner_repo()?;
let perm = args.permission.as_api();
client.put_no_content(
&format!("/repos/{repo}/collaborators/{}", args.user),
&json!({ "permission": perm }),
)?;
client
.api()
.repo_add_collaborator(
owner,
name,
&args.user,
AddCollaboratorOption {
permission: Some(args.permission.as_option()),
},
)
.send()?;
println!("added {} to {repo} ({perm})", args.user);
Ok(())
}