use axum::extract::{Path, State};
use axum::Json;
use serde_json::json;
use crate::web::github::{self, Refusal};
use crate::web::state::WebState;
use wsb_github::Scope;
async fn list(state: &WebState, scope: Scope) -> Result<Json<wsb_github::service::Listed>, Refusal> {
let service = github::of(state)?.service.clone();
github::blocking(move || service.list(&scope)).await.map(Json)
}
async fn single(state: &WebState, scope: Scope) -> Result<Json<wsb_github::service::Single>, Refusal> {
let service = github::of(state)?.service.clone();
github::blocking(move || service.single(&scope)).await.map(Json)
}
pub async fn list_pulls(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<wsb_github::service::Listed>, Refusal> {
list(&state, Scope::Pulls(format!("{owner}/{repo}"))).await
}
pub async fn list_releases(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<wsb_github::service::Listed>, Refusal> {
list(&state, Scope::Releases(format!("{owner}/{repo}"))).await
}
pub async fn list_tags(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<wsb_github::service::Listed>, Refusal> {
list(&state, Scope::Tags(format!("{owner}/{repo}"))).await
}
pub async fn list_milestones(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<wsb_github::service::Listed>, Refusal> {
list(&state, Scope::Milestones(format!("{owner}/{repo}"))).await
}
pub async fn list_labels(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<wsb_github::service::Listed>, Refusal> {
list(&state, Scope::Labels(format!("{owner}/{repo}"))).await
}
pub async fn repository(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<wsb_github::service::Single>, Refusal> {
single(&state, Scope::Repository(format!("{owner}/{repo}"))).await
}
pub async fn checks(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<serde_json::Value>, Refusal> {
let name = format!("{owner}/{repo}");
let head = github::repositories(&state)
.await?
.into_iter()
.find(|repository| repository.name == name)
.and_then(|repository| repository.head);
let Some(commit) = head else {
return Ok(Json(json!({ "item": null, "sync": wsb_github::Standing::default() })));
};
let answer = single(&state, Scope::Checks { repo: name, commit }).await?;
Ok(Json(serde_json::to_value(answer.0).map_err(|e| {
github::failure(wsb_github::Failure::Mirror(e.into()))
})?))
}
pub async fn traffic(
State(state): State<WebState>,
Path((owner, repo)): Path<(String, String)>,
) -> Result<Json<wsb_github::service::Single>, Refusal> {
single(&state, Scope::Traffic(format!("{owner}/{repo}"))).await
}
pub async fn notifications(State(state): State<WebState>) -> Result<Json<wsb_github::service::Listed>, Refusal> {
list(&state, Scope::Notifications).await
}
pub async fn rate_limit(State(state): State<WebState>) -> Result<Json<wsb_github::service::Single>, Refusal> {
single(&state, Scope::RateLimit).await
}
pub async fn overview(State(state): State<WebState>) -> Result<Json<serde_json::Value>, Refusal> {
let service = github::of(&state)?.service.clone();
let repositories = github::repositories(&state).await?;
let facts = github::blocking(move || service.overview(&repositories)).await?;
Ok(Json(json!({ "repositories": facts })))
}
pub async fn refresh(State(state): State<WebState>) -> Result<Json<wsb_github::sync::SyncReport>, Refusal> {
super::issues::refresh(&state).await.map(Json)
}