arks-server 1.3.11

Git server: upload-pack / receive-pack + smart/dumb HTTP + daemon (M11/M25)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Single-source `API_ROUTES` table and OpenAPI 3.1.0 document generator
//! (governed by `rest-api-standards.sot.md` RA-OAS-1..7).
//!
//! Runtime `GET /api/v1/openapi.json`, CLI `arks serve openapi`, and the
//! published `docs/assets/data/openapi.json` artifact are all generated from
//! [`API_ROUTES`] and enforced byte-for-byte by unit tests.

use serde_json::{Map, Value, json};

/// Server listener surface owning an API route (`x-arks-surface`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApiSurface {
    /// `arks serve` HTTP listener (`:8080` demo default).
    Serve,
    /// `arks hub` forge listener (`:3920` demo default).
    Forge,
    /// `arks serve` operational probes & metrics on the `serve` listener.
    Ops,
}

impl ApiSurface {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Serve => "serve",
            Self::Forge => "forge",
            Self::Ops => "ops",
        }
    }
}

/// Release line availability of a route (`v1.0` stays Kinetic-free).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReleaseLine {
    Both,
    V2Only,
}

/// Declarative API route definition driving OpenAPI 3.1.0 generation and route audits.
#[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,
}

/// Single source of truth for `arks serve` and `arks hub` documented REST operations.
pub const API_ROUTES: &[ApiRouteDef] = &[
    // Discovery & OpenAPI (`serve`)
    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,
    },
    // Native `serve` `/api/v1/*`
    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,
    },
    // GitHub-compatible `/repos/{owner}/{repo}/*` on `serve :8080`
    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,
    },
    // GitHub-compatible routes served exclusively by `arks hub` (`forge :3920`)
    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,
    },
    // Git LFS Batch API (`serve`)
    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,
    },
    // Operational probes & Prometheus metrics (`ops`)
    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,
    },
];

/// Generate the canonical OpenAPI 3.1.0 JSON document from [`API_ROUTES`].
///
/// `include_v2` must be `false` on `v1.0` (ensuring zero Kinetic paths) and `true` on `v2.0`.
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" } }
                }
            }
        }
    })
}

/// Deterministic pretty-printed JSON string (with trailing newline) for CLI and drift tests.
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`"
        );
    }
}