Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller, swarm-ci-metrics: build forge clients through forge-client

This commit is contained in:
atlas 2026-11-15 23:02:54 +01:00
commit a77131ca3f
16 changed files with 75 additions and 28 deletions

View file

@ -112,7 +112,8 @@ Wire types + socket plumbing:
in the path, unlike `hive-core-agent-sock`. in the path, unlike `hive-core-agent-sock`.
- **`forge-client/`** — the shared Forgejo client construction (`async_client`, - **`forge-client/`** — the shared Forgejo client construction (`async_client`,
`sync_client`), `TimeoutPolicy`, `TokenSource`, and the one drift-tolerant `sync_client`), `TimeoutPolicy`, `TokenSource`, and the one drift-tolerant
`NotificationThread`; used by `hive-forge` and `hive-forge-notify`. `NotificationThread`; used by `hive-forge`, `hive-forge-notify`,
`swarm-controller` and `swarm-ci-metrics`.
- **`hive-sock-client/`** — the shared JSON-line unix-socket client; retry - **`hive-sock-client/`** — the shared JSON-line unix-socket client; retry
is a policy value (`Retry::None` / `Retry::RideOutRestart`). Also holds is a policy value (`Retry::None` / `Retry::RideOutRestart`). Also holds
the bounded request-line reader (`read_request_line`) the socket servers the bounded request-line reader (`read_request_line`) the socket servers

2
Cargo.lock generated
View file

@ -5087,6 +5087,7 @@ name = "swarm-ci-metrics"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"forge-client",
"forgejo-api", "forgejo-api",
"hive-log", "hive-log",
"reqwest", "reqwest",
@ -5132,6 +5133,7 @@ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"bytes", "bytes",
"chrono", "chrono",
"forge-client",
"forgejo-api", "forgejo-api",
"futures-util", "futures-util",
"getrandom 0.3.4", "getrandom 0.3.4",

View file

@ -1,6 +1,6 @@
# `forge-client` — the one place hyperhive builds a Forgejo client and # `forge-client` — the one place hyperhive builds a Forgejo client and
# parses a Forgejo notification. Shared by `hive-forge` (blocking CLI) and # parses a Forgejo notification. Shared by `hive-forge`, `hive-forge-notify`,
# `hive-forge-notify` (async poller) so that forgejo-api schema drift is # `swarm-controller` and `swarm-ci-metrics` so that forgejo-api schema drift is
# handled once. # handled once.
[package] [package]
name = "forge-client" name = "forge-client"

View file

@ -1,9 +1,9 @@
# forge-client # forge-client
The one place hyperhive builds a Forgejo client and reads a Forgejo The one place hyperhive builds a Forgejo client and reads a Forgejo
notification. `hive-forge` (the blocking CLI) and `hive-forge-notify` (the notification. `hive-forge` (the blocking CLI), `hive-forge-notify` (the
async poller) share it, so a Forgejo schema change that `forgejo-api` does not async poller), `swarm-controller` and `swarm-ci-metrics` share it, so a Forgejo
yet model is fixed here once. schema change that `forgejo-api` does not yet model is fixed here once.
## When to use it ## When to use it

View file

@ -1,5 +1,5 @@
//! Forgejo client construction, timeout policy and the drift-tolerant //! Forgejo client construction, timeout policy and the drift-tolerant
//! notification thread, shared by `hive-forge` and `hive-forge-notify`. //! notification thread, shared by every hyperhive forge consumer.
//! //!
//! Construction takes a base URL and a [`TokenSource`]; the bounds on how //! Construction takes a base URL and a [`TokenSource`]; the bounds on how
//! long a call may take come from a [`TimeoutPolicy`] the caller names. //! long a call may take come from a [`TimeoutPolicy`] the caller names.

View file

@ -9,6 +9,8 @@ path = "src/main.rs"
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
# Builds the forge client, from the `ci-metrics` account token.
forge-client.workspace = true
# The runs list and the repo search, as the `ci-metrics` account. # The runs list and the repo search, as the `ci-metrics` account.
forgejo-api.workspace = true forgejo-api.workspace = true
hive-log.workspace = true hive-log.workspace = true

View file

@ -19,8 +19,9 @@ use std::path::{Path, PathBuf};
use std::time::Duration; use std::time::Duration;
use anyhow::{Context as _, Result, bail}; use anyhow::{Context as _, Result, bail};
use forge_client::{StaticToken, async_client};
use forgejo_api::Forgejo;
use forgejo_api::structs::{ActionRun, ListActionRunsQuery, RepoSearchQuery}; use forgejo_api::structs::{ActionRun, ListActionRunsQuery, RepoSearchQuery};
use forgejo_api::{Auth, Forgejo};
use swarm_queue_client::{ClientSecret, QueueConfig}; use swarm_queue_client::{ClientSecret, QueueConfig};
use swarm_secret_client::ci_metrics::{ClientSecret as StoredClientSecret, client_secret_path}; use swarm_secret_client::ci_metrics::{ClientSecret as StoredClientSecret, client_secret_path};
use swarm_secret_client::client::ENV_CLIENT_CERT; use swarm_secret_client::client::ENV_CLIENT_CERT;
@ -84,7 +85,10 @@ async fn main() -> Result<()> {
state_file: PathBuf::from(state_dir).join("cursor.json"), state_file: PathBuf::from(state_dir).join("cursor.json"),
}; };
let forge = Forgejo::new(Auth::Token(&forge_token().await?), cfg.forge_url.clone()) let forge = async_client(
cfg.forge_url.clone(),
&StaticToken::new(forge_token().await?),
)
.context("building the forge client")?; .context("building the forge client")?;
let http = reqwest::Client::builder() let http = reqwest::Client::builder()
.timeout(Duration::from_secs(30)) .timeout(Duration::from_secs(30))

View file

@ -18,6 +18,8 @@ backon.workspace = true
# neither, does not claim to need them. # neither, does not claim to need them.
async-nats = { workspace = true, features = ["kv"] } async-nats = { workspace = true, features = ["kv"] }
axum.workspace = true axum.workspace = true
# Builds every forgejo-api client (`forge/`), so the construction lives once.
forge-client.workspace = true
# swarm-controller's own forge client (`forge.rs`) — self-contained, # swarm-controller's own forge client (`forge.rs`) — self-contained,
# deliberately not sharing code with `hive-c0re::forge` across the crate # deliberately not sharing code with `hive-c0re::forge` across the crate
# boundary (see #3306's design discussion: forcing that split now, over a # boundary (see #3306's design discussion: forcing that split now, over a

View file

@ -18,8 +18,9 @@ use std::collections::BTreeSet;
use std::sync::Arc; use std::sync::Arc;
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use forge_client::{StaticToken, async_client};
use forgejo_api::structs::{AccessToken, CreateAccessTokenOption}; use forgejo_api::structs::{AccessToken, CreateAccessTokenOption};
use forgejo_api::{ApiErrorKind, Auth, Forgejo, ForgejoError}; use forgejo_api::{ApiErrorKind, ForgejoError};
use reqwest::StatusCode; use reqwest::StatusCode;
use swarm_secret_client::{client::DEFAULT_CERT_MOUNT, forge, policy}; use swarm_secret_client::{client::DEFAULT_CERT_MOUNT, forge, policy};
@ -242,7 +243,7 @@ impl Client {
) )
.await?; .await?;
let as_agent = Forgejo::new(Auth::Token(&credential.value), self.url.clone()) let as_agent = async_client(self.url.clone(), &StaticToken::new(&credential.value))
.context("building a forge client with the new token")?; .context("building a forge client with the new token")?;
let me = as_agent let me = as_agent
.user_get_current() .user_get_current()

View file

@ -9,7 +9,8 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use forgejo_api::{ApiErrorKind, Auth, Forgejo, ForgejoError}; use forge_client::{StaticToken, async_client};
use forgejo_api::{ApiErrorKind, Forgejo, ForgejoError};
use futures_util::future::BoxFuture; use futures_util::future::BoxFuture;
use reqwest::StatusCode; use reqwest::StatusCode;
use swarm_secret_client::stored_secret::StoredSecret; use swarm_secret_client::stored_secret::StoredSecret;
@ -32,7 +33,7 @@ impl Api {
/// `url` is not http(s), or the token is not ASCII. /// `url` is not http(s), or the token is not ASCII.
pub(crate) fn new(url: url::Url, token: StoredSecret) -> Result<Self, ForgejoError> { pub(crate) fn new(url: url::Url, token: StoredSecret) -> Result<Self, ForgejoError> {
let current = token.current(); let current = token.current();
let api = Arc::new(Forgejo::new(Auth::Token(&current), url.clone())?); let api = Arc::new(async_client(url.clone(), &StaticToken::new(&current))?);
Ok(Self { Ok(Self {
url, url,
token, token,
@ -45,7 +46,7 @@ impl Api {
let token = self.token.current(); let token = self.token.current();
let mut built = self.built.lock().expect("unpoisoned"); let mut built = self.built.lock().expect("unpoisoned");
if built.0 != token { if built.0 != token {
let api = Arc::new(Forgejo::new(Auth::Token(&token), self.url.clone())?); let api = Arc::new(async_client(self.url.clone(), &StaticToken::new(&token))?);
*built = (token.clone(), api); *built = (token.clone(), api);
} }
Ok((token, Arc::clone(&built.1))) Ok((token, Arc::clone(&built.1)))
@ -237,6 +238,38 @@ pub(crate) mod tests {
assert_eq!(served.load(Ordering::SeqCst), 4); assert_eq!(served.load(Ordering::SeqCst), 4);
} }
#[tokio::test(start_paused = true)]
async fn an_unchanged_token_keeps_the_client() {
let (url, served) = forge_accepting("old", None).await;
let (token, reads) = token("old", "new");
let api = Api::new(url, token).unwrap();
let (first_token, first) = api.current().unwrap();
let (second_token, second) = api.current().unwrap();
assert_eq!(
(first_token.as_str(), second_token.as_str()),
("old", "old")
);
assert!(Arc::ptr_eq(&first, &second));
first.user_get_current().await.expect("sends the old token");
assert_eq!(reads.load(Ordering::SeqCst), 0);
assert_eq!(served.load(Ordering::SeqCst), 1);
}
#[tokio::test(start_paused = true)]
async fn a_changed_token_builds_a_client_sending_the_new_one() {
let (url, _) = forge_accepting("new", None).await;
let (token, _) = token("old", "new");
let api = Api::new(url, token).unwrap();
let (_, before) = api.current().unwrap();
api.token.after_refusal("old").await.expect("a new token");
let (token, after) = api.current().unwrap();
assert_eq!(token, "new");
assert!(!Arc::ptr_eq(&before, &after));
after.user_get_current().await.expect("sends the new token");
let err = before.user_get_current().await.expect_err("still the old");
assert!(refuses_the_token(&err), "{err}");
}
#[test] #[test]
fn only_a_401_counts_as_refusing_the_token() { fn only_a_401_counts_as_refusing_the_token() {
let unauthorized = forgejo_api::ApiError { let unauthorized = forgejo_api::ApiError {

View file

@ -15,8 +15,8 @@
use std::sync::Arc; use std::sync::Arc;
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use forge_client::{StaticToken, async_client};
use forgejo_api::structs::RepoSearchQuery; use forgejo_api::structs::RepoSearchQuery;
use forgejo_api::{Auth, Forgejo};
use swarm_secret_client::builder::forge_token_path; use swarm_secret_client::builder::forge_token_path;
use swarm_secret_client::forge; use swarm_secret_client::forge;
@ -93,7 +93,7 @@ impl Client {
.replace_token(&store, LOGIN, TOKEN_NAME, TOKEN_SCOPES, &listed, &path) .replace_token(&store, LOGIN, TOKEN_NAME, TOKEN_SCOPES, &listed, &path)
.await?; .await?;
Forgejo::new(Auth::Token(&credential.value), self.url.clone()) async_client(self.url.clone(), &StaticToken::new(&credential.value))
.context("building a forge client with the new token")? .context("building a forge client with the new token")?
.repo_search(RepoSearchQuery::default()) .repo_search(RepoSearchQuery::default())
.await .await

View file

@ -17,11 +17,11 @@
use std::sync::Arc; use std::sync::Arc;
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use forge_client::{StaticToken, async_client};
use forgejo_api::structs::{ use forgejo_api::structs::{
CreateTeamOption, CreateTeamOptionPermission, EditTeamOption, EditTeamOptionPermission, CreateTeamOption, CreateTeamOptionPermission, EditTeamOption, EditTeamOptionPermission,
RepoSearchQuery, Team, TeamPermission, RepoSearchQuery, Team, TeamPermission,
}; };
use forgejo_api::{Auth, Forgejo};
use swarm_secret_client::ci_metrics::forge_token_path; use swarm_secret_client::ci_metrics::forge_token_path;
use swarm_secret_client::forge; use swarm_secret_client::forge;
@ -124,7 +124,7 @@ impl Client {
.replace_token(&store, LOGIN, TOKEN_NAME, TOKEN_SCOPES, &listed, &path) .replace_token(&store, LOGIN, TOKEN_NAME, TOKEN_SCOPES, &listed, &path)
.await?; .await?;
Forgejo::new(Auth::Token(&credential.value), self.url.clone()) async_client(self.url.clone(), &StaticToken::new(&credential.value))
.context("building a forge client with the new token")? .context("building a forge client with the new token")?
.repo_search(RepoSearchQuery::default()) .repo_search(RepoSearchQuery::default())
.await .await

View file

@ -20,11 +20,12 @@
use std::sync::Arc; use std::sync::Arc;
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use forge_client::{StaticToken, async_client};
use forgejo_api::Forgejo;
use forgejo_api::structs::{ use forgejo_api::structs::{
ChangeFileOperation, ChangeFileOperationOperation, ChangeFilesOptions, CreatePullReviewOptions, ChangeFileOperation, ChangeFileOperationOperation, ChangeFilesOptions, CreatePullReviewOptions,
Identity, RepoSearchQuery, Identity, RepoSearchQuery,
}; };
use forgejo_api::{Auth, Forgejo};
use swarm_secret_client::forge; use swarm_secret_client::forge;
use swarm_secret_client::review_classifier::forge_token_path; use swarm_secret_client::review_classifier::forge_token_path;
@ -122,7 +123,7 @@ impl Client {
.replace_token(&store, LOGIN, TOKEN_NAME, TOKEN_SCOPES, &listed, &path) .replace_token(&store, LOGIN, TOKEN_NAME, TOKEN_SCOPES, &listed, &path)
.await?; .await?;
Forgejo::new(Auth::Token(&credential.value), self.url.clone()) async_client(self.url.clone(), &StaticToken::new(&credential.value))
.context("building a forge client with the new token")? .context("building a forge client with the new token")?
.repo_search(RepoSearchQuery::default()) .repo_search(RepoSearchQuery::default())
.await .await
@ -221,7 +222,7 @@ impl Client {
else { else {
bail!("nothing at {path}: the classifier's forge token is not minted yet"); bail!("nothing at {path}: the classifier's forge token is not minted yet");
}; };
Forgejo::new(Auth::Token(&credential.value), self.url.clone()) async_client(self.url.clone(), &StaticToken::new(&credential.value))
.context("building a forge client with the classifier's token") .context("building a forge client with the classifier's token")
} }
@ -462,7 +463,7 @@ mod tests {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap(); let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
tokio::spawn(async move { axum::serve(listener, app).await }); tokio::spawn(async move { axum::serve(listener, app).await });
let forge = Forgejo::new(Auth::Token("stub-token"), url).unwrap(); let forge = async_client(url, &StaticToken::new("stub-token")).unwrap();
let commit = BranchCommit { let commit = BranchCommit {
base: "main".to_owned(), base: "main".to_owned(),
branch: "review-classifier/alice-0a1b2c3d".to_owned(), branch: "review-classifier/alice-0a1b2c3d".to_owned(),

View file

@ -19,11 +19,11 @@
use std::sync::Arc; use std::sync::Arc;
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use forge_client::{StaticToken, async_client};
use forgejo_api::structs::{ use forgejo_api::structs::{
AccessToken, CreateTeamOption, CreateTeamOptionPermission, EditTeamOption, AccessToken, CreateTeamOption, CreateTeamOptionPermission, EditTeamOption,
EditTeamOptionPermission, RepoSearchQuery, Team, TeamPermission, EditTeamOptionPermission, RepoSearchQuery, Team, TeamPermission,
}; };
use forgejo_api::{Auth, Forgejo};
use swarm_secret_client::forge; use swarm_secret_client::forge;
use super::agent_token::{Decision, Observed, classify_named, is_not_found}; use super::agent_token::{Decision, Observed, classify_named, is_not_found};
@ -175,7 +175,7 @@ impl Client {
) )
.await?; .await?;
Forgejo::new(Auth::Token(&credential.value), self.url.clone()) async_client(self.url.clone(), &StaticToken::new(&credential.value))
.context("building a forge client with the new token")? .context("building a forge client with the new token")?
.repo_search(RepoSearchQuery::default()) .repo_search(RepoSearchQuery::default())
.await .await

View file

@ -21,8 +21,8 @@ use std::time::Duration;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use base64::Engine as _; use base64::Engine as _;
use forge_client::{StaticToken, async_client};
use forgejo_api::structs::{CreateAccessTokenOption, UpdateUserAvatarOption}; use forgejo_api::structs::{CreateAccessTokenOption, UpdateUserAvatarOption};
use forgejo_api::{Auth, Forgejo};
use sha2::{Digest as _, Sha256}; use sha2::{Digest as _, Sha256};
use super::agent_token::is_not_found; use super::agent_token::is_not_found;
@ -177,7 +177,7 @@ impl Client {
.sha1 .sha1
.filter(|v| !v.is_empty()) .filter(|v| !v.is_empty())
.with_context(|| format!("the forge created {login}'s token but returned no value"))?; .with_context(|| format!("the forge created {login}'s token but returned no value"))?;
let uploaded = Forgejo::new(Auth::Token(&token), self.url.clone()) let uploaded = async_client(self.url.clone(), &StaticToken::new(&token))
.context("building a forge client with the avatar token")? .context("building a forge client with the avatar token")?
.user_update_avatar(UpdateUserAvatarOption { .user_update_avatar(UpdateUserAvatarOption {
image: Some(base64::engine::general_purpose::STANDARD.encode(image)), image: Some(base64::engine::general_purpose::STANDARD.encode(image)),

View file

@ -2542,7 +2542,8 @@ mod tests {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap(); let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
drop(listener); drop(listener);
let forge = forgejo_api::Forgejo::new(forgejo_api::Auth::Token("stub-token"), url).unwrap(); let forge =
forge_client::async_client(url, &forge_client::StaticToken::new("stub-token")).unwrap();
forge.user_get_current().await.expect_err("nothing listens") forge.user_get_current().await.expect_err("nothing listens")
} }