63 lines
1.9 KiB
Rust
63 lines
1.9 KiB
Rust
//! `repo-add-collaborator <user> [--permission read|write|admin] [-r <repo>]`
|
|
//! — add `<user>` as a collaborator on the active repo (from `-r` /
|
|
//! `HIVE_FORGE_REPO`). Prints a one-line confirmation.
|
|
//!
|
|
//! Wraps `PUT /api/v1/repos/{owner}/{repo}/collaborators/{collaborator}`.
|
|
//! A fresh agent-namespace repo (see `repo-create`) usually needs peers
|
|
//! added before they can collaborate; this is the companion verb.
|
|
|
|
use anyhow::Result;
|
|
use clap::Args as ClapArgs;
|
|
use clap::ValueEnum;
|
|
use serde_json::json;
|
|
|
|
use crate::client::Client;
|
|
|
|
/// Collaborator permission level accepted by Forgejo.
|
|
#[derive(Clone, Copy, ValueEnum)]
|
|
pub enum Permission {
|
|
/// Pull (read-only) access.
|
|
Read,
|
|
/// Push (read/write) access.
|
|
Write,
|
|
/// Full administrative access to the repo.
|
|
Admin,
|
|
}
|
|
|
|
impl Permission {
|
|
/// The wire string Forgejo's API expects.
|
|
fn as_api(self) -> &'static str {
|
|
match self {
|
|
Permission::Read => "read",
|
|
Permission::Write => "write",
|
|
Permission::Admin => "admin",
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(ClapArgs)]
|
|
pub struct Args {
|
|
/// Collaborator's forge login to add.
|
|
user: String,
|
|
/// Permission level to grant (default: write — a freshly added
|
|
/// collaborator usually needs to push).
|
|
#[arg(long, value_enum, default_value_t = Permission::Write)]
|
|
permission: Permission,
|
|
}
|
|
|
|
/// # Errors
|
|
///
|
|
/// Propagates any transport error from the Forgejo REST call (network
|
|
/// unreachable, 4xx/5xx response such as an unknown user or insufficient
|
|
/// permission on the repo, token missing/invalid) and any I/O error from
|
|
/// writing the confirmation to stdout.
|
|
pub fn run(client: &Client, args: Args) -> Result<()> {
|
|
let repo = client.repo();
|
|
let perm = args.permission.as_api();
|
|
client.put_no_content(
|
|
&format!("/repos/{repo}/collaborators/{}", args.user),
|
|
&json!({ "permission": perm }),
|
|
)?;
|
|
println!("added {} to {repo} ({perm})", args.user);
|
|
Ok(())
|
|
}
|