Watch
0
0
Fork
You've already forked hyperhive
0

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

View file

@ -3,7 +3,7 @@
use anyhow::Result;
use clap::Args as ClapArgs;
use serde_json::Value;
use forgejo_api::structs::RepoGetSingleCommitQuery;
use crate::client::Client;
@ -14,25 +14,35 @@ pub struct Args {
}
pub fn run(client: &Client, args: Args) -> Result<()> {
let repo = client.repo();
let (owner, name) = client.owner_repo()?;
// Try branch first; the bash helper silently treats failure as
// "not a branch, use the reference as a commit sha directly".
let commit_sha = match client.get_json(&format!("/repos/{repo}/branches/{}", args.reference)) {
Ok(v) => v
.get("commit")
.and_then(|c| c.get("id"))
.and_then(Value::as_str)
.unwrap_or(&args.reference)
.to_owned(),
let commit_sha = match client
.api()
.repo_get_branch(owner, name, &args.reference)
.send()
{
Ok(branch) => branch
.commit
.and_then(|c| c.id)
.unwrap_or_else(|| args.reference.clone()),
Err(_) => args.reference.clone(),
};
let commit = client.get_json(&format!("/repos/{repo}/git/commits/{commit_sha}"))?;
let commit = client
.api()
.repo_get_single_commit(
owner,
name,
&commit_sha,
RepoGetSingleCommitQuery::default(),
)
.send()?;
let sha = commit
.get("tree")
.and_then(|t| t.get("sha"))
.and_then(Value::as_str)
.or_else(|| commit.get("sha").and_then(Value::as_str))
.unwrap_or("");
.commit
.and_then(|c| c.tree)
.and_then(|t| t.sha)
.or(commit.sha)
.unwrap_or_default();
println!("{sha}");
Ok(())
}