use serde_json::{Map, Value, json};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApiSurface {
Serve,
Forge,
Ops,
}
impl ApiSurface {
pub const fn as_str(self) -> &'static str {
match self {
Self::Serve => "serve",
Self::Forge => "forge",
Self::Ops => "ops",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReleaseLine {
Both,
V2Only,
}
#[derive(Debug, Clone, Copy)]
pub struct ApiRouteDef {
pub method: &'static str,
pub pattern: &'static str,
pub op_id: &'static str,
pub tag: &'static str,
pub summary: &'static str,
pub scopes: &'static [&'static str],
pub surface: ApiSurface,
pub line: ReleaseLine,
}
pub const API_ROUTES: &[ApiRouteDef] = &[
ApiRouteDef {
method: "GET",
pattern: "/api/v1",
op_id: "getApiIndex",
tag: "Discovery",
summary: "API v1 discovery index",
scopes: &[],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/api/v1/openapi.json",
op_id: "getOpenApiSpec",
tag: "Discovery",
summary: "OpenAPI 3.1.0 specification generated from API_ROUTES",
scopes: &[],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/api/v1/repo",
op_id: "getRepoSummary",
tag: "Native Ops & LFS",
summary: "Repository summary (default branch, counts, archived state)",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/api/v1/leases",
op_id: "listLeases",
tag: "Native Ops & LFS",
summary: "List active multi-session revision leases",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "DELETE",
pattern: "/api/v1/leases/{name}",
op_id: "releaseLease",
tag: "Native Ops & LFS",
summary: "Release or force-unlock a named lease",
scopes: &["repo:write"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/api/v1/issues",
op_id: "listNativeIssues",
tag: "Native Ops & LFS",
summary: "List repository issues (native JSON)",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "POST",
pattern: "/api/v1/issues",
op_id: "createNativeIssue",
tag: "Native Ops & LFS",
summary: "Create a repository issue (native JSON)",
scopes: &["repo:write"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/api/v1/pulls",
op_id: "listNativePulls",
tag: "Native Ops & LFS",
summary: "List pull requests (native JSON)",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/api/v1/releases",
op_id: "listNativeReleases",
tag: "Native Ops & LFS",
summary: "List releases (native JSON)",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/issues",
op_id: "listRepoIssues",
tag: "GitHub-Compatible",
summary: "List repository issues",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "POST",
pattern: "/repos/{owner}/{repo}/issues",
op_id: "createRepoIssue",
tag: "GitHub-Compatible",
summary: "Create a repository issue",
scopes: &["repo:write"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/issues/{number}",
op_id: "getRepoIssue",
tag: "GitHub-Compatible",
summary: "Get a single issue by number",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "PATCH",
pattern: "/repos/{owner}/{repo}/issues/{number}",
op_id: "updateRepoIssue",
tag: "GitHub-Compatible",
summary: "Update issue title, body, state, labels, or assignee",
scopes: &["repo:write"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "POST",
pattern: "/repos/{owner}/{repo}/issues/{number}/comments",
op_id: "createIssueComment",
tag: "GitHub-Compatible",
summary: "Add a comment to an issue or pull request",
scopes: &["repo:write"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/pulls",
op_id: "listRepoPulls",
tag: "GitHub-Compatible",
summary: "List pull requests",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "POST",
pattern: "/repos/{owner}/{repo}/pulls",
op_id: "createRepoPull",
tag: "GitHub-Compatible",
summary: "Open a pull request",
scopes: &["repo:write"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/releases",
op_id: "listRepoReleases",
tag: "GitHub-Compatible",
summary: "List releases",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "POST",
pattern: "/repos/{owner}/{repo}/releases",
op_id: "createRepoRelease",
tag: "GitHub-Compatible",
summary: "Create a release",
scopes: &["repo:write"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/actions/runs",
op_id: "listActionRuns",
tag: "GitHub-Compatible",
summary: "List workflow runs",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/issues/{number}/comments",
op_id: "listIssueCommentsForge",
tag: "Forge Listener (:3920)",
summary: "List comments on an issue or pull request (forge listener)",
scopes: &["repo:read"],
surface: ApiSurface::Forge,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/pulls/{number}",
op_id: "getRepoPullForge",
tag: "Forge Listener (:3920)",
summary: "Get a pull request by number (forge listener)",
scopes: &["repo:read"],
surface: ApiSurface::Forge,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "PUT",
pattern: "/repos/{owner}/{repo}/pulls/{number}/merge",
op_id: "mergeRepoPullForge",
tag: "Forge Listener (:3920)",
summary: "Merge a pull request (forge listener)",
scopes: &["repo:write"],
surface: ApiSurface::Forge,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/commits/{ref}/status",
op_id: "getCommitStatusForge",
tag: "Forge Listener (:3920)",
summary: "Get combined commit status (forge listener)",
scopes: &["repo:read"],
surface: ApiSurface::Forge,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/repos/{owner}/{repo}/rulesets",
op_id: "listRepoRulesetsForge",
tag: "Forge Listener (:3920)",
summary: "List repository branch protection rulesets (forge listener)",
scopes: &["repo:read"],
surface: ApiSurface::Forge,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "POST",
pattern: "/info/lfs/objects/batch",
op_id: "lfsBatch",
tag: "Native Ops & LFS",
summary: "Git LFS Batch API (download / upload object negotiation)",
scopes: &["repo:read"],
surface: ApiSurface::Serve,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/livez",
op_id: "getLivez",
tag: "Native Ops & LFS",
summary: "Process liveness probe",
scopes: &[],
surface: ApiSurface::Ops,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/readyz",
op_id: "getReadyz",
tag: "Native Ops & LFS",
summary: "Readiness probe (shutdown state, writable data dir, healthy store)",
scopes: &[],
surface: ApiSurface::Ops,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/healthz",
op_id: "getHealthz",
tag: "Native Ops & LFS",
summary: "Readiness probe alias (/readyz)",
scopes: &[],
surface: ApiSurface::Ops,
line: ReleaseLine::Both,
},
ApiRouteDef {
method: "GET",
pattern: "/metrics",
op_id: "getMetrics",
tag: "Native Ops & LFS",
summary: "Prometheus metrics (loopback or Bearer ARKS_METRICS_TOKEN)",
scopes: &[],
surface: ApiSurface::Ops,
line: ReleaseLine::Both,
},
];
pub fn openapi_document(include_v2: bool) -> Value {
let mut paths: Map<String, Value> = Map::new();
for route in API_ROUTES {
if !include_v2 && route.line == ReleaseLine::V2Only {
continue;
}
let path_entry = paths
.entry(route.pattern.to_string())
.or_insert_with(|| Value::Object(Map::new()));
let Some(path_obj) = path_entry.as_object_mut() else {
continue;
};
let method_key = route.method.to_ascii_lowercase();
let params = extract_path_params(route.pattern);
let mut op = Map::new();
op.insert("operationId".into(), json!(route.op_id));
op.insert("tags".into(), json!([route.tag]));
op.insert("summary".into(), json!(route.summary));
op.insert("x-arks-surface".into(), json!(route.surface.as_str()));
if !route.scopes.is_empty() {
op.insert("x-arks-scopes".into(), json!(route.scopes));
}
if !params.is_empty() {
op.insert("parameters".into(), Value::Array(params));
}
op.insert(
"responses".into(),
json!({
"200": { "description": "Successful response" },
"400": { "$ref": "#/components/responses/Problem" },
"401": { "$ref": "#/components/responses/Problem" },
"403": { "$ref": "#/components/responses/Problem" },
"404": { "$ref": "#/components/responses/Problem" }
}),
);
path_obj.insert(method_key, Value::Object(op));
}
json!({
"openapi": "3.1.0",
"jsonSchemaDialect": "https://spec.openapis.org/oas/3.1/dialect/base",
"info": {
"title": "arks serve REST API",
"version": env!("CARGO_PKG_VERSION"),
"summary": "GitHub-compatible forge REST API and native ops, Git LFS, and lease endpoints served by single-binary arks serve.",
"description": "Generated directly from API_ROUTES in arks-server. Operations are tagged by listener surface (x-arks-surface: serve | forge | ops). Errors use RFC 9457 application/problem+json with message and documentation_url extensions.",
"license": {
"name": "Apache-2.0 OR MIT",
"url": "https://github.com/modootoday/arks/blob/main/LICENSE"
}
},
"servers": [
{
"url": "http://127.0.0.1:8080",
"description": "arks serve HTTP listener (Web UI, /api/v1/*, unified REST, Git Smart HTTP, LFS, ops probes)",
"x-arks-surface": "serve"
},
{
"url": "http://127.0.0.1:3920",
"description": "arks hub forge listener (GitHub-class REST surface for arks hub client)",
"x-arks-surface": "forge"
},
{
"url": "https://{host}",
"description": "Production self-hosted arks serve instance behind TLS",
"x-arks-surface": "serve",
"variables": {
"host": {
"default": "forge.example.com"
}
}
}
],
"security": [
{ "bearerAuth": [] },
{ "basicAuth": [] }
],
"tags": [
{
"name": "Discovery",
"description": "API index and runtime OpenAPI 3.1.0 specification."
},
{
"name": "GitHub-Compatible",
"description": "Drop-in /repos/{owner}/{repo}/* endpoints on the arks serve listener."
},
{
"name": "Forge Listener (:3920)",
"description": "GitHub-class endpoints served by the companion arks hub forge listener."
},
{
"name": "Native Ops & LFS",
"description": "Multi-session leases, Git LFS Batch API, health probes, and Prometheus metrics."
}
],
"components": {
"securitySchemes": {
"bearerAuth": {
"type": "http",
"scheme": "bearer",
"description": "Personal Access Token (PAT) scoped by RBAC role (reader, writer, admin)."
},
"basicAuth": {
"type": "http",
"scheme": "basic",
"description": "HTTP Basic authentication with username and PAT (or password when 2FA is disabled)."
}
},
"responses": {
"Problem": {
"description": "RFC 9457 Problem Details error response",
"content": {
"application/problem+json": {
"schema": { "$ref": "#/components/schemas/ProblemDetails" }
}
}
}
},
"schemas": {
"ProblemDetails": {
"type": "object",
"required": ["type", "title", "status", "detail", "instance", "message"],
"properties": {
"type": { "type": "string", "format": "uri" },
"title": { "type": "string" },
"status": { "type": "integer" },
"detail": { "type": "string" },
"instance": { "type": "string" },
"message": { "type": "string" },
"documentation_url": { "type": "string", "format": "uri" },
"errors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field": { "type": "string" },
"code": { "type": "string" },
"message": { "type": "string" }
}
}
}
}
}
}
},
"paths": Value::Object(paths),
"webhooks": {
"push": {
"post": {
"summary": "Repository ref push event",
"responses": { "200": { "description": "Webhook received" } }
}
},
"issues": {
"post": {
"summary": "Issue created or updated event",
"responses": { "200": { "description": "Webhook received" } }
}
},
"pull_request": {
"post": {
"summary": "Pull request opened or merged event",
"responses": { "200": { "description": "Webhook received" } }
}
},
"release": {
"post": {
"summary": "Release published event",
"responses": { "200": { "description": "Webhook received" } }
}
}
}
})
}
pub fn openapi_json_pretty(include_v2: bool) -> String {
let doc = openapi_document(include_v2);
let mut out = serde_json::to_string_pretty(&doc).expect("serialize OpenAPI 3.1 document");
out.push('\n');
out
}
fn extract_path_params(pattern: &str) -> Vec<Value> {
let mut out = Vec::new();
for seg in pattern.split('/') {
if let Some(name) = seg.strip_prefix('{').and_then(|s| s.strip_suffix('}')) {
out.push(json!({
"name": name,
"in": "path",
"required": true,
"schema": { "type": "string" }
}));
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn openapi_spec_is_valid_3_1_with_per_surface_servers_and_zero_kinetic_on_v1() {
let doc = openapi_document(false);
assert_eq!(doc["openapi"], "3.1.0");
assert_eq!(
doc["jsonSchemaDialect"],
"https://spec.openapis.org/oas/3.1/dialect/base"
);
let servers = doc["servers"].as_array().unwrap();
assert!(servers.iter().any(|s| s["x-arks-surface"] == "serve"));
assert!(servers.iter().any(|s| s["x-arks-surface"] == "forge"));
let paths = doc["paths"].as_object().unwrap();
assert!(paths.contains_key("/api/v1/openapi.json"));
assert!(paths.contains_key("/livez"));
assert!(paths.contains_key("/readyz"));
for key in paths.keys() {
assert!(
!key.contains("kinetic"),
"v1.0 OpenAPI must stay Kinetic-free: {key}"
);
}
assert!(doc["webhooks"].as_object().unwrap().contains_key("release"));
}
#[test]
fn openapi_artifact_matches_generated_spec() {
let generated = openapi_json_pretty(false);
let artifact =
include_str!("../../../../../docs/assets/data/openapi.json").replace("\r\n", "\n");
assert_eq!(
artifact, generated,
"docs/assets/data/openapi.json drifted from API_ROUTES; run `arks serve openapi > docs/assets/data/openapi.json`"
);
}
}