82 lines
2.6 KiB
Rust
82 lines
2.6 KiB
Rust
//! `repo-add-collaborator <user> [--permission read|write|admin] [-r <repo>]`
|
|
//! — add `<user>` as a collaborator on the active repo (see
|
|
//! `client::Client::from_env` for how it resolves). 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 forgejo_api::structs::{AddCollaboratorOption, AddCollaboratorOptionPermission};
|
|
|
|
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 (used for the printed
|
|
/// confirmation).
|
|
fn as_api(self) -> &'static str {
|
|
match self {
|
|
Permission::Read => "read",
|
|
Permission::Write => "write",
|
|
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)]
|
|
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 (owner, name) = client.owner_repo()?;
|
|
let perm = args.permission.as_api();
|
|
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(())
|
|
}
|