hyperhive/hive-forge/src/verbs/attach.rs

65 lines
1.5 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,
}
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(())
}
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}");
}
}