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

@ -2,13 +2,15 @@
//! <file>` — upload a file as an attachment. Prints the browser
//! download URL.
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use anyhow::{Result, bail};
use anyhow::{Context, Result, bail};
use clap::Args as ClapArgs;
use serde_json::Value;
use forgejo_api::structs::{
Attachment, IssueCreateIssueAttachmentQuery, IssueCreateIssueCommentAttachmentQuery,
};
use crate::client::Client;
use crate::client::{Client, index};
#[derive(ClapArgs)]
pub struct IssueArgs {
@ -39,11 +41,16 @@ pub fn run_issue(client: &Client, args: IssueArgs) -> Result<()> {
args.file.display()
);
}
let repo = client.repo();
let resp = client.post_multipart_file(
&format!("/repos/{repo}/issues/{}/assets", args.number),
&args.file,
)?;
let (owner, name) = client.owner_repo()?;
let bytes = read_file(&args.file)?;
let query = IssueCreateIssueAttachmentQuery {
name: file_name(&args.file),
updated_at: None,
};
let resp = client
.api()
.issue_create_issue_attachment(owner, name, index(args.number)?, &bytes, query)
.send()?;
print_url(&resp);
Ok(())
}
@ -61,17 +68,33 @@ pub fn run_comment(client: &Client, args: CommentArgs) -> Result<()> {
args.file.display()
);
}
let repo = client.repo();
let resp = client.post_multipart_file(
&format!("/repos/{repo}/issues/comments/{}/assets", args.id),
&args.file,
)?;
let (owner, name) = client.owner_repo()?;
let bytes = read_file(&args.file)?;
let query = IssueCreateIssueCommentAttachmentQuery {
name: file_name(&args.file),
updated_at: None,
};
let resp = client
.api()
.issue_create_issue_comment_attachment(owner, name, index(args.id)?, &bytes, query)
.send()?;
print_url(&resp);
Ok(())
}
fn print_url(v: &Value) {
if let Some(url) = v.get("browser_download_url").and_then(Value::as_str) {
fn read_file(path: &Path) -> Result<Vec<u8>> {
std::fs::read(path).with_context(|| format!("read {}", path.display()))
}
/// The upload's attachment name — the file's basename, matching what
/// the old multipart form (which sent the file with its real name)
/// made the server record.
fn file_name(path: &Path) -> Option<String> {
path.file_name().map(|n| n.to_string_lossy().into_owned())
}
fn print_url(v: &Attachment) {
if let Some(url) = &v.browser_download_url {
println!("{url}");
}
}