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

@ -3,9 +3,10 @@
use anyhow::{Result, bail};
use clap::Args as ClapArgs;
use forgejo_api::structs::CreatePullReviewOptions;
use serde_json::{Value, json};
use crate::client::Client;
use crate::client::{Client, index};
use crate::verbs::print_json;
#[derive(ClapArgs)]
@ -55,53 +56,69 @@ pub fn run(client: &Client, args: Args) -> Result<()> {
/// Submit a review event (`APPROVED` / `REQUEST_CHANGES` / `COMMENT`) and print
/// a compact summary of the created review.
fn submit_review(client: &Client, number: u64, event: &str, body: Option<String>) -> Result<()> {
let repo = client.repo();
let payload = json!({
"event": event,
"body": body.unwrap_or_default(),
});
let v = client.post_json(&format!("/repos/{repo}/pulls/{number}/reviews"), &payload)?;
let (owner, name) = client.owner_repo()?;
let payload = CreatePullReviewOptions {
body: Some(body.unwrap_or_default()),
comments: None,
commit_id: None,
event: Some(event.to_owned()),
};
let review = client
.api()
.repo_create_pull_review(owner, name, index(number)?, payload)
.send()?;
print_json(&json!({
"id": v.get("id"),
"state": v.get("state"),
"user": v.get("user").and_then(|u| u.get("login")),
"id": review.id,
"state": review.state,
"user": review.user.as_ref().and_then(|u| u.login.as_deref()),
}))
}
/// Fetch inline diff comments for a single review. Returns an empty vec on
/// any error (missing review, network failure) so callers can degrade
/// gracefully.
fn fetch_inline_comments(client: &Client, repo: &str, pr: u64, review_id: u64) -> Vec<Value> {
/// Fetch inline diff comments for a single review, serialized back to
/// the API's JSON shape. Returns an empty vec on any error (missing
/// review, network failure) so callers can degrade gracefully.
fn fetch_inline_comments(client: &Client, pr: u64, review_id: i64) -> Vec<Value> {
let Ok((owner, name)) = client.owner_repo() else {
return Vec::new();
};
let Ok(idx) = index(pr) else {
return Vec::new();
};
client
.get_json(&format!(
"/repos/{repo}/pulls/{pr}/reviews/{review_id}/comments"
))
.api()
.repo_get_pull_review_comments(owner, name, idx, review_id)
.send()
.ok()
.and_then(|comments| serde_json::to_value(comments).ok())
.and_then(|v| v.as_array().cloned())
.unwrap_or_default()
}
/// List all reviews for a PR, dispatching to the appropriate output mode.
fn list_reviews(client: &Client, number: u64) -> Result<()> {
let repo = client.repo();
let v = client.get_json(&format!("/repos/{repo}/pulls/{number}/reviews"))?;
let reviews = v.as_array().cloned().unwrap_or_default();
let (owner, name) = client.owner_repo()?;
let (_, reviews) = client
.api()
.repo_list_pull_reviews(owner, name, index(number)?)
.send()?;
let reviews = serde_json::to_value(reviews)?;
let reviews = reviews.as_array().cloned().unwrap_or_default();
if client.json_mode() {
list_reviews_json(client, repo, number, &reviews)
list_reviews_json(client, number, &reviews)
} else {
list_reviews_text(client, repo, number, &reviews);
list_reviews_text(client, number, &reviews);
Ok(())
}
}
/// JSON output: one object per review, with an inline `comments` array.
fn list_reviews_json(client: &Client, repo: &str, number: u64, reviews: &[Value]) -> Result<()> {
fn list_reviews_json(client: &Client, number: u64, reviews: &[Value]) -> Result<()> {
let trimmed: Vec<Value> = reviews
.iter()
.map(|r| {
let id = r.get("id").and_then(Value::as_u64).unwrap_or(0);
let id = r.get("id").and_then(Value::as_i64).unwrap_or(0);
let inline: Vec<Value> = if id > 0 {
fetch_inline_comments(client, repo, number, id)
fetch_inline_comments(client, number, id)
.iter()
.map(|c| {
json!({
@ -130,13 +147,13 @@ fn list_reviews_json(client: &Client, repo: &str, number: u64, reviews: &[Value]
/// Human-readable output: Markdown-style heading per review, inline
/// comments as `[path:line] body` (line omitted for PR-level comments).
fn list_reviews_text(client: &Client, repo: &str, number: u64, reviews: &[Value]) {
fn list_reviews_text(client: &Client, number: u64, reviews: &[Value]) {
if reviews.is_empty() {
println!("(no reviews)");
return;
}
for r in reviews {
let id = r.get("id").and_then(Value::as_u64).unwrap_or(0);
let id = r.get("id").and_then(Value::as_i64).unwrap_or(0);
let user = r
.get("user")
.and_then(|u| u.get("login"))
@ -149,7 +166,7 @@ fn list_reviews_text(client: &Client, repo: &str, number: u64, reviews: &[Value]
println!("{body}");
}
if id > 0 {
for c in &fetch_inline_comments(client, repo, number, id) {
for c in &fetch_inline_comments(client, number, id) {
let path = c.get("path").and_then(Value::as_str).unwrap_or("?");
let cbody = c.get("body").and_then(Value::as_str).unwrap_or("").trim();
// PR-level comments have no line; omit `:line` when absent.