refactor(hive-c0re): port forge module + knowledge hooks to forgejo-api
This commit is contained in:
parent
4468e86e2d
commit
b8a3927c43
5 changed files with 651 additions and 426 deletions
|
|
@ -18,8 +18,11 @@ pub use repos::{
|
|||
};
|
||||
pub use users::{core_token, ensure_user_for, provision_user_token};
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use reqwest::StatusCode;
|
||||
use forgejo_api::{Auth, Forgejo};
|
||||
use url::Url;
|
||||
|
||||
use repos::{ensure_mirrors, ensure_operators_team, ensure_org};
|
||||
use users::{
|
||||
|
|
@ -105,34 +108,20 @@ async fn forge_admin(args: &[&str]) -> Result<String> {
|
|||
Ok(stdout)
|
||||
}
|
||||
|
||||
/// Thin Forgejo REST helper. Sends `method` to `url` with a JSON body
|
||||
/// and `Authorization: token <token>`, returns the HTTP status code.
|
||||
/// All Forgejo API calls that don't shell out to `forgejo admin` go
|
||||
/// through here — one place for auth header, content-type, error
|
||||
/// propagation, and the shared reqwest Client.
|
||||
/// Returns the response status **and body**. The body lets callers log
|
||||
/// *why* Forgejo rejected a request (e.g. the validation message on a
|
||||
/// 422); status-only callers just bind `(status, _)`. Body read is
|
||||
/// best-effort — a read error yields an empty string rather than
|
||||
/// failing the whole call.
|
||||
async fn forge_http(
|
||||
method: reqwest::Method,
|
||||
url: &str,
|
||||
token: &str,
|
||||
body: &str,
|
||||
) -> Result<(StatusCode, String)> {
|
||||
let client = reqwest::Client::new();
|
||||
let resp = client
|
||||
.request(method, url)
|
||||
.header("Authorization", format!("token {token}"))
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body.to_owned())
|
||||
.send()
|
||||
.await
|
||||
.with_context(|| format!("forge HTTP request to {url}"))?;
|
||||
let status = resp.status();
|
||||
let text = resp.text().await.unwrap_or_default();
|
||||
Ok((status, text))
|
||||
/// Typed Forgejo API client for the local forge ([`FORGE_HTTP`]),
|
||||
/// authenticated as `token`. All Forgejo API calls that don't shell
|
||||
/// out to `forgejo admin` go through clients built here — one place
|
||||
/// for the base URL and auth. Tokens differ per call site (core admin
|
||||
/// token vs per-agent tokens), so the token is passed per call; the
|
||||
/// base URL is parsed once. Failures surface as
|
||||
/// `forgejo_api::ForgejoError`, whose Display carries the HTTP status
|
||||
/// and the API's error message (e.g. the validation reason on a 422).
|
||||
pub(crate) fn api(token: &str) -> Result<Forgejo> {
|
||||
static URL: OnceLock<Url> = OnceLock::new();
|
||||
let url = URL
|
||||
.get_or_init(|| Url::parse(FORGE_HTTP).expect("FORGE_HTTP is a valid URL"))
|
||||
.clone();
|
||||
Forgejo::new(Auth::Token(token), url).context("build forgejo api client")
|
||||
}
|
||||
|
||||
/// Whether `ns` is a hive-managed Forgejo namespace that agent-initiated
|
||||
|
|
|
|||
Loading…
Reference in a new issue