diff --git a/swarm-controller/src/git_forge/issue_report.rs b/swarm-controller/src/git_forge/issue_report.rs index f4819d414..3ecc795bd 100644 --- a/swarm-controller/src/git_forge/issue_report.rs +++ b/swarm-controller/src/git_forge/issue_report.rs @@ -13,7 +13,8 @@ use axum::{ }; use crate::forge; -use crate::routes::{AppState, StatusUnavailable, error_problem}; +use crate::git_forge::GitForgeState; +use crate::routes::{StatusUnavailable, error_problem}; /// The 503 detail when the forge did not answer within /// [`forge::FORGE_DEADLINE`]. @@ -35,7 +36,7 @@ const DEADLINE_DETAIL: &str = "the forge did not answer within the deadline"; tag = "repos" )] pub async fn get_repos( - State(state): State, + State(state): State, ) -> Result>, StatusUnavailable> { let Some(client) = state.forge.get() else { return Err(StatusUnavailable( @@ -71,7 +72,7 @@ pub async fn get_repos( tag = "repos" )] pub async fn get_issue_report_all( - State(state): State, + State(state): State, ) -> Result>, StatusUnavailable> { let Some(client) = state.forge.get() else { return Err(StatusUnavailable( @@ -119,7 +120,7 @@ pub async fn get_issue_report_all( tag = "repos" )] pub async fn get_issue_report( - State(state): State, + State(state): State, Path((org, repo)): Path<(String, String)>, ) -> Result>, problem_details::ProblemDetails> { let Some(client) = state.forge.get() else { diff --git a/swarm-controller/src/git_forge/mod.rs b/swarm-controller/src/git_forge/mod.rs index 41f7f63f3..979b9ffc7 100644 --- a/swarm-controller/src/git_forge/mod.rs +++ b/swarm-controller/src/git_forge/mod.rs @@ -9,3 +9,27 @@ pub(crate) mod pr_conflicts; pub(crate) mod pr_report; pub(crate) mod pulls; pub(crate) mod webhook; + +use std::sync::Arc; + +use axum::extract::FromRef; + +use crate::jobq::Late; +use crate::routes::AppState; + +/// What the forge read routes may reach of [`AppState`]: the forge client. +/// +/// Derived per request from [`AppState`] (`FromRef`), so the field is a clone +/// of the same `Arc` and nothing is held twice. +#[derive(Clone)] +pub(crate) struct GitForgeState { + pub(crate) forge: Late>, +} + +impl FromRef for GitForgeState { + fn from_ref(state: &AppState) -> Self { + Self { + forge: Arc::clone(&state.forge), + } + } +} diff --git a/swarm-controller/src/git_forge/pr_report.rs b/swarm-controller/src/git_forge/pr_report.rs index 53fb7618b..270a6344a 100644 --- a/swarm-controller/src/git_forge/pr_report.rs +++ b/swarm-controller/src/git_forge/pr_report.rs @@ -10,8 +10,9 @@ use axum::{ }; use crate::forge; +use crate::git_forge::GitForgeState; use crate::issue_report::issue_report_error_status; -use crate::routes::{AppState, error_problem}; +use crate::routes::error_problem; /// The 503 detail when the forge did not answer within /// [`forge::FORGE_DEADLINE`]. @@ -28,7 +29,7 @@ const DEADLINE_DETAIL: &str = "the forge did not answer within the deadline"; tag = "pulls" )] pub async fn get_pr_report_all( - State(state): State, + State(state): State, ) -> Result>, problem_details::ProblemDetails> { let Some(client) = state.forge.get() else { return Err(error_problem( @@ -74,7 +75,7 @@ pub async fn get_pr_report_all( tag = "pulls" )] pub async fn get_pr_report( - State(state): State, + State(state): State, Path((org, repo)): Path<(String, String)>, ) -> Result>, problem_details::ProblemDetails> { let Some(client) = state.forge.get() else { diff --git a/swarm-controller/src/git_forge/pulls.rs b/swarm-controller/src/git_forge/pulls.rs index acb806bfa..916473dda 100644 --- a/swarm-controller/src/git_forge/pulls.rs +++ b/swarm-controller/src/git_forge/pulls.rs @@ -13,9 +13,10 @@ use serde::Deserialize; use utoipa::IntoParams; use crate::forge::{self, OpenPull}; +use crate::git_forge::GitForgeState; use crate::issue_report::issue_report_error_status; use crate::review_classifier::split_repo; -use crate::routes::{AppState, error_problem}; +use crate::routes::error_problem; /// Whose open pull requests to list. #[derive(Debug, Deserialize, IntoParams)] @@ -36,7 +37,7 @@ pub(crate) struct PullsQuery { tag = "pulls" )] pub(crate) async fn get_pull_repos( - State(state): State, + State(state): State, ) -> Result>, ProblemDetails> { let Some(client) = state.forge.get() else { return Err(no_forge()); @@ -61,7 +62,7 @@ pub(crate) async fn get_pull_repos( tag = "pulls" )] pub(crate) async fn get_pulls( - State(state): State, + State(state): State, query: Result, QueryRejection>, ) -> Result>, ProblemDetails> { let Query(query) = query.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.body_text()))?;