refactor(hive-forge): port CLI verbs to forgejo-api
This commit is contained in:
parent
b8a3927c43
commit
4636987469
36 changed files with 1463 additions and 1153 deletions
|
|
@ -10,15 +10,19 @@
|
|||
|
||||
use anyhow::Result;
|
||||
use clap::Args as ClapArgs;
|
||||
use forgejo_api::structs::{Commit, RepoGetPullRequestCommitsQuery};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::client::Client;
|
||||
use crate::verbs::print_json;
|
||||
use crate::client::{Client, index};
|
||||
use crate::verbs::{print_json, rfc3339};
|
||||
|
||||
/// Page cap for the commit list. Forgejo serves up to 50 commits per
|
||||
/// page; 40 pages (2000 commits) is far beyond any real PR.
|
||||
const MAX_PAGES: u32 = 40;
|
||||
|
||||
/// Page size on the commit list endpoint (Forgejo's cap).
|
||||
const PAGE_SIZE: u32 = 50;
|
||||
|
||||
#[derive(ClapArgs)]
|
||||
pub struct Args {
|
||||
/// PR number.
|
||||
|
|
@ -26,22 +30,35 @@ pub struct Args {
|
|||
}
|
||||
|
||||
pub fn run(client: &Client, args: Args) -> Result<()> {
|
||||
let repo = client.repo();
|
||||
let commits = client.get_json_all(
|
||||
&format!("/repos/{repo}/pulls/{}/commits", args.number),
|
||||
MAX_PAGES,
|
||||
)?;
|
||||
let (owner, name) = client.owner_repo()?;
|
||||
let idx = index(args.number)?;
|
||||
let mut commits: Vec<Commit> = Vec::new();
|
||||
for page in 1..=MAX_PAGES {
|
||||
let (_, batch) = client
|
||||
.api()
|
||||
.repo_get_pull_request_commits(
|
||||
owner,
|
||||
name,
|
||||
idx,
|
||||
RepoGetPullRequestCommitsQuery::default(),
|
||||
)
|
||||
.page(page)
|
||||
.page_size(PAGE_SIZE)
|
||||
.send()?;
|
||||
let short = batch.len() < PAGE_SIZE as usize;
|
||||
commits.extend(batch);
|
||||
if short {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let trimmed: Vec<_> = commits
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let commit = c.get("commit");
|
||||
json!({
|
||||
"sha": c.get("sha"),
|
||||
"message": commit.and_then(|x| x.get("message")),
|
||||
"author_date": commit
|
||||
.and_then(|x| x.get("author"))
|
||||
.and_then(|a| a.get("date")),
|
||||
"author": c.get("author").and_then(|u| u.get("login")),
|
||||
"sha": c.sha,
|
||||
"message": c.commit.as_ref().and_then(|x| x.message.as_deref()),
|
||||
"author_date": rfc3339(c.commit.as_ref().and_then(|x| x.author.as_ref()).and_then(|a| a.date)),
|
||||
"author": c.author.as_ref().and_then(|u| u.login.as_deref()),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
|
|
|||
Loading…
Reference in a new issue