systemic gap argus flagged on PR #798 (timeline verb). every `pub fn run` in hive-forge/src/verbs/*.rs lacked a `# Errors` block — violates Rust API guidelines + obscures the failure surface for operators reading the source. uniform doc per verb category: - pure GET + print verbs: "transport error from the Forgejo REST call + I/O error from stdout" - body-from-file verbs (comment/comment_edit/issue_create/issue_edit/ pr_create): adds 'I/O error from --body-file/stdin input' - file-upload verbs (attach-issue, attach-comment): adds 'file read/exist check' - pr_create: also mentions the --push shellout 23 `pub fn run` signatures touched. no behaviour change; pure documentation sweep. cargo test green (38 tests).
77 lines
2.1 KiB
Rust
77 lines
2.1 KiB
Rust
//! `attach-issue <number> <file>` and `attach-comment <comment-id>
|
|
//! <file>` — upload a file as an attachment. Prints the browser
|
|
//! download URL.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use anyhow::{Result, bail};
|
|
use clap::Args as ClapArgs;
|
|
use serde_json::Value;
|
|
|
|
use crate::client::Client;
|
|
|
|
#[derive(ClapArgs)]
|
|
pub struct IssueArgs {
|
|
/// Issue number.
|
|
number: u64,
|
|
/// File path to upload.
|
|
file: PathBuf,
|
|
}
|
|
|
|
#[derive(ClapArgs)]
|
|
pub struct CommentArgs {
|
|
/// Comment id.
|
|
id: u64,
|
|
/// File path to upload.
|
|
file: PathBuf,
|
|
}
|
|
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the input file doesn't exist or can't be
|
|
/// read. Propagates any transport error from the Forgejo REST call
|
|
/// (network unreachable, 4xx/5xx response, token missing/invalid)
|
|
/// and any I/O error from writing the browser download URL to stdout.
|
|
pub fn run_issue(client: &Client, args: IssueArgs) -> Result<()> {
|
|
if !args.file.is_file() {
|
|
bail!(
|
|
"hive-forge attach-issue: file not found: {}",
|
|
args.file.display()
|
|
);
|
|
}
|
|
let repo = client.repo();
|
|
let resp = client.post_multipart_file(
|
|
&format!("/repos/{repo}/issues/{}/assets", args.number),
|
|
&args.file,
|
|
)?;
|
|
print_url(&resp);
|
|
Ok(())
|
|
}
|
|
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the input file doesn't exist or can't be
|
|
/// read. Propagates any transport error from the Forgejo REST call
|
|
/// (network unreachable, 4xx/5xx response, token missing/invalid)
|
|
/// and any I/O error from writing the browser download URL to stdout.
|
|
pub fn run_comment(client: &Client, args: CommentArgs) -> Result<()> {
|
|
if !args.file.is_file() {
|
|
bail!(
|
|
"hive-forge attach-comment: file not found: {}",
|
|
args.file.display()
|
|
);
|
|
}
|
|
let repo = client.repo();
|
|
let resp = client.post_multipart_file(
|
|
&format!("/repos/{repo}/issues/comments/{}/assets", args.id),
|
|
&args.file,
|
|
)?;
|
|
print_url(&resp);
|
|
Ok(())
|
|
}
|
|
|
|
fn print_url(v: &Value) {
|
|
if let Some(url) = v.get("browser_download_url").and_then(Value::as_str) {
|
|
println!("{url}");
|
|
}
|
|
}
|