flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/apps/api/src/lib.rs

586 lines23,134 bytesCodeBlame
1//! The public API: REST at api.g1t.sh and the MCP server at mcp.g1t.sh.
2//!
3//! One Worker, two hostnames. Both are thin adapters over the same
4//! operations (see [`operations::Op`]), which call the services that own
5//! the data. This Worker holds none.
6
7mod blobs;
8mod mcp;
9mod oauth;
10mod openapi;
11mod operations;
12mod rest;
13
14use g1t_contracts::billing::FinishRunArgs;
15use g1t_contracts::identity::{
16 DeviceClaim, DeviceClaimArgs, DeviceStart, DeviceStartArgs, TokenArgs,
17};
18use g1t_contracts::work::{
19 CheckRun, QueueState, ReportChecksArgs, ReportPlanArgs, ReportQueueArgs, ReportReviewArgs,
20};
21use g1t_contracts::identity::AgentScope;
22use g1t_contracts::{Failure, FailureCode, Outcome, PrincipalKind, Viewer};
23use serde_json::{Value, json};
24use worker::{Context, Env, Method, Request, Response, Result, event};
25
26use operations::Services;
27
28const API: &str = "https://api.g1t.sh";
29
30fn method_name(method: Method) -> &'static str {
31 match method {
32 Method::Get => "GET",
33 Method::Post => "POST",
34 Method::Patch => "PATCH",
35 Method::Put => "PUT",
36 Method::Delete => "DELETE",
37 Method::Options => "OPTIONS",
38 Method::Head => "HEAD",
39 _ => "OTHER",
40 }
41}
42
43/// An error in the shape every endpoint uses.
44fn failure(failure: &Failure) -> Result<Response> {
45 Ok(Response::from_json(&json!({ "error": failure }))?.with_status(failure.code.http_status()))
46}
47
48fn fail(code: FailureCode, message: &str) -> Result<Response> {
49 failure(&Failure {
50 code,
51 message: message.to_owned(),
52 })
53}
54
55/// A request body as JSON. An empty or malformed body is no input.
56async fn json_body(request: &mut Request) -> Value {
57 request.json().await.unwrap_or(Value::Null)
58}
59
60/// Who a request's `Authorization: Bearer g1t_…` names. A missing token is
61/// an anonymous viewer; a wrong one is refused, so that a typo does not
62/// silently look signed out.
63async fn authenticate(
64 request: &Request,
65 services: &Services,
66) -> Result<std::result::Result<Viewer, Response>> {
67 let header = request.headers().get("authorization")?.unwrap_or_default();
68 let token = match header.split_once(' ') {
69 Some((scheme, token)) if scheme.eq_ignore_ascii_case("bearer") && !token.is_empty() => {
70 token.trim()
71 }
72 _ => return Ok(Ok(None)),
73 };
74 let viewer: Viewer = g1t_kit::call(
75 &services.identity,
76 "user_for_access_token",
77 &TokenArgs {
78 token: token.to_owned(),
79 },
80 )
81 .await?;
82 if viewer.is_some() {
83 return Ok(Ok(viewer));
84 }
85 let mut response = fail(FailureCode::Unauthenticated, "Invalid access token.")?;
86 // Tells an MCP client where to sign in again.
87 response.headers_mut().set(
88 "www-authenticate",
89 &format!("{}, error=\"invalid_token\"", oauth::MCP_CHALLENGE),
90 )?;
91 Ok(Err(response))
92}
93
94/// Where everything is, for someone or something exploring the API.
95fn index() -> Value {
96 let repo = format!("{API}/repos/{{owner}}/{{name}}");
97 json!({
98 "documentation_url": "https://docs.g1t.sh/reference/api/",
99 "openapi_url": format!("{API}/openapi.json"),
100 "mcp_url": "https://mcp.g1t.sh",
101 "current_user_url": format!("{API}/user"),
102 "workspaces_url": format!("{API}/workspaces"),
103 "repositories_url": format!("{API}/repos{{?q}}"),
104 "repository_url": repo,
105 "repository_events_url": format!("{repo}/events{{?before}}"),
106 "labels_url": format!("{repo}/labels"),
107 "issues_url": format!("{repo}/issues{{?state,label}}"),
108 "issue_url": format!("{repo}/issues/{{number}}"),
109 "issue_comments_url": format!("{repo}/issues/{{number}}/comments"),
110 "pulls_url": format!("{repo}/pulls{{?state}}"),
111 "pull_url": format!("{repo}/pulls/{{number}}"),
112 "pull_changes_url": format!("{repo}/pulls/{{number}}/changes"),
113 "pull_reviews_url": format!("{repo}/pulls/{{number}}/reviews"),
114 "pull_session_url": format!("{repo}/pulls/{{number}}/session{{?after}}"),
115 "device_code_url": format!("{API}/device/code"),
116 "device_token_url": format!("{API}/device/token"),
117 "oauth_metadata_url": format!("{API}/.well-known/oauth-authorization-server"),
118 "git_url": "https://g1t.sh/{owner}/{name}.git",
119 "integrations_url": format!("{API}/workspaces/{{workspace}}/integrations"),
120 "context_url": format!("{repo}/context{{?reference}}"),
121 "import_issue_url": format!("{repo}/issues/import"),
122 "hooks_url": format!("{API}/hooks/{{integration}}"),
123 })
124}
125
126// Signing in from a tool. Accounts are created, and passwords typed, only
127// in a browser; a tool gets its token by having a person approve a code.
128
129/// Passes a request from an outside system to its connection, as it came:
130/// its signature covers the exact bytes of the body.
131async fn receive_hook(request: &mut Request, services: &Services, id: &str) -> Result<Response> {
132 let headers: std::collections::HashMap<String, String> = request
133 .headers()
134 .entries()
135 .map(|(name, value)| (name.to_lowercase(), value))
136 .collect();
137 let body = request.text().await.unwrap_or_default();
138 if body.len() > 1_000_000 {
139 return Ok(Response::from_json(&json!({ "message": "The body is too large." }))?.with_status(413));
140 }
141 let received: g1t_contracts::integrations::Received = g1t_kit::call(
142 &services.integrations,
143 "receive",
144 &json!({ "id": id, "headers": headers, "body": body }),
145 )
146 .await?;
147 Ok(Response::from_json(&json!({ "message": received.message }))?.with_status(received.status))
148}
149
150async fn device_code(request: &mut Request, services: &Services) -> Result<Response> {
151 let body = json_body(request).await;
152 let started: DeviceStart = g1t_kit::call(
153 &services.identity,
154 "device_start",
155 &DeviceStartArgs {
156 client_name: body["client_name"].as_str().unwrap_or_default().to_owned(),
157 },
158 )
159 .await?;
160 Response::from_json(&json!({
161 "device_code": started.device_code,
162 "user_code": started.user_code,
163 "verification_uri": "https://g1t.sh/device",
164 "verification_uri_complete": format!("https://g1t.sh/device?code={}", started.user_code),
165 "expires_in": started.expires_in,
166 "interval": started.interval,
167 }))
168}
169
170async fn device_token(request: &mut Request, services: &Services) -> Result<Response> {
171 let body = json_body(request).await;
172 let claim: DeviceClaim = g1t_kit::call(
173 &services.identity,
174 "device_claim",
175 &DeviceClaimArgs {
176 device_code: body["device_code"].as_str().unwrap_or_default().to_owned(),
177 },
178 )
179 .await?;
180 Response::from_json(&match claim {
181 DeviceClaim::Approved { token, user } => json!({
182 "status": "approved",
183 "token": token,
184 "username": user.username,
185 "verified": user.verified,
186 }),
187 DeviceClaim::Pending => json!({ "status": "pending" }),
188 DeviceClaim::Denied => json!({ "status": "denied" }),
189 DeviceClaim::Expired => json!({ "status": "expired" }),
190 })
191}
192
193/// A sandbox reporting on its run of a pull request's acceptance checks.
194/// The run's own token, in the body, is the credential: it was given to
195/// that sandbox and to nothing else.
196async fn report_checks(
197 request: &mut Request,
198 services: &Services,
199 run_id: &str,
200) -> Result<Response> {
201 let body = json_body(request).await;
202 let reported: Outcome<CheckRun> = g1t_kit::call(
203 &services.work,
204 "report_checks",
205 &ReportChecksArgs {
206 run_id: run_id.to_owned(),
207 token: body["token"].as_str().unwrap_or_default().to_owned(),
208 results: serde_json::from_value(body["results"].clone()).unwrap_or_default(),
209 error: body["error"].as_str().map(str::to_owned),
210 skip: false,
211 },
212 )
213 .await?;
214 match reported {
215 Outcome::Ok(run) => Response::from_json(&json!({ "status": run.status })),
216 Outcome::Fail(refused) => failure(&refused),
217 }
218}
219
220/// A sandbox reporting one tested state of a merge queue. As with checks,
221/// the entry's own token is the credential.
222async fn report_queue(
223 request: &mut Request,
224 services: &Services,
225 entry_id: &str,
226) -> Result<Response> {
227 let body = json_body(request).await;
228 let reported: Outcome<QueueState> = g1t_kit::call(
229 &services.work,
230 "report_queue",
231 &ReportQueueArgs {
232 entry_id: entry_id.to_owned(),
233 token: body["token"].as_str().unwrap_or_default().to_owned(),
234 combined_commit: body["combinedCommit"].as_str().map(str::to_owned),
235 results: serde_json::from_value(body["results"].clone()).unwrap_or_default(),
236 error: body["error"].as_str().map(str::to_owned),
237 conflict_with: body["conflictWith"].as_u64().map(|n| n as u32),
238 },
239 )
240 .await?;
241 match reported {
242 Outcome::Ok(state) => Response::from_json(&json!({ "state": state })),
243 Outcome::Fail(refused) => failure(&refused),
244 }
245}
246
247/// A sandbox reporting the review its agent wrote. As with checks, the
248/// run's own token is the credential.
249async fn report_review(
250 request: &mut Request,
251 services: &Services,
252 run_id: &str,
253) -> Result<Response> {
254 let body = json_body(request).await;
255 let reported: Outcome<bool> = g1t_kit::call(
256 &services.work,
257 "report_review",
258 &ReportReviewArgs {
259 run_id: run_id.to_owned(),
260 token: body["token"].as_str().unwrap_or_default().to_owned(),
261 verdict: serde_json::from_value(body["verdict"].clone()).unwrap_or(None),
262 body: body["body"].as_str().unwrap_or_default().to_owned(),
263 comments: serde_json::from_value(body["comments"].clone()).unwrap_or_default(),
264 model: body["model"].as_str().map(str::to_owned),
265 error: body["error"].as_str().map(str::to_owned),
266 },
267 )
268 .await?;
269 match reported {
270 Outcome::Ok(_) => Response::from_json(&json!({ "recorded": true })),
271 Outcome::Fail(refused) => failure(&refused),
272 }
273}
274
275/// A sandbox reporting the plan its agent wrote. As with checks, the
276/// plan's own token is the credential.
277async fn report_plan(
278 request: &mut Request,
279 services: &Services,
280 plan_id: &str,
281) -> Result<Response> {
282 let body = json_body(request).await;
283 let reported: Outcome<bool> = g1t_kit::call(
284 &services.work,
285 "report_plan",
286 &ReportPlanArgs {
287 plan_id: plan_id.to_owned(),
288 token: body["token"].as_str().unwrap_or_default().to_owned(),
289 summary: body["summary"].as_str().unwrap_or_default().to_owned(),
290 issues: serde_json::from_value(body["issues"].clone()).unwrap_or_default(),
291 error: body["error"].as_str().map(str::to_owned),
292 },
293 )
294 .await?;
295 match reported {
296 Outcome::Ok(_) => Response::from_json(&json!({ "recorded": true })),
297 Outcome::Fail(refused) => failure(&refused),
298 }
299}
300
301/// A sandbox reporting what its agent's run cost, so that the workspace
302/// it worked for is charged. As with checks, the run's own token is the
303/// credential.
304async fn report_usage(
305 request: &mut Request,
306 services: &Services,
307 run_id: &str,
308) -> Result<Response> {
309 let body = json_body(request).await;
310 let charged: Outcome<bool> = g1t_kit::call(
311 &services.billing,
312 "finish_run",
313 &FinishRunArgs {
314 run_id: run_id.to_owned(),
315 token: body["token"].as_str().unwrap_or_default().to_owned(),
316 cost_usd: body["cost_usd"].as_f64().unwrap_or_default(),
317 turns: body["turns"].as_u64().unwrap_or_default() as u32,
318 },
319 )
320 .await?;
321 match charged {
322 Outcome::Ok(_) => Response::from_json(&json!({ "recorded": true })),
323 Outcome::Fail(refused) => failure(&refused),
324 }
325}
326
327async fn respond(mut request: Request, env: &Env) -> Result<Response> {
328 let method = method_name(request.method());
329 if method == "OPTIONS" {
330 return Ok(Response::empty()?.with_status(204));
331 }
332 let url = request.url()?;
333 // Paths carry no version. An earlier form began with `/v1`, which is
334 // still accepted so that nothing already written against it breaks.
335 let path = match url.path().strip_prefix("/v1") {
336 Some(rest) if rest.is_empty() || rest.starts_with('/') => rest.to_owned(),
337 _ => url.path().to_owned(),
338 };
339 let on_mcp = url.host_str().is_some_and(|host| host.starts_with("mcp."));
340 let mut services = Services::new(env)?;
341
342 // Outside systems reporting to a connection. They sign what they send
343 // with the connection's own secret, which is not a g1t token, so this
344 // comes before anything that would read one.
345 if method == "POST" && !on_mcp
346 && let Some(id) = path.strip_prefix("/hooks/").filter(|id| !id.is_empty() && !id.contains('/')) {
347 return receive_hook(&mut request, &services, id).await;
348 }
349
350 // A sandbox building a deployment, reporting with its build's token,
351 // which is not a g1t token. The body goes through as it is: it can
352 // carry a Worker's bundled code.
353 if method == "POST" && !on_mcp
354 && let Some(rest) = path.strip_prefix("/deployments/jobs/")
355 {
356 let target = format!("https://deployments/jobs/{rest}");
357 let body = request.bytes().await?;
358 let headers = worker::Headers::new();
359 headers.set("content-type", "application/json")?;
360 let mut init = worker::RequestInit::new();
361 init.with_method(Method::Post)
362 .with_headers(headers)
363 .with_body(Some(worker::js_sys::Uint8Array::from(body.as_slice()).into()));
364 let mut answer = env
365 .service("DEPLOYMENTS")?
366 .fetch_request(Request::new_with_init(&target, &init)?)
367 .await?;
368 // A fresh response: a fetched one's headers cannot be changed, and
369 // every response gets the API's own on the way out.
370 let status = answer.status_code();
371 return Ok(Response::from_bytes(answer.bytes().await?)?
372 .with_status(status)
373 .with_headers({
374 let headers = worker::Headers::new();
375 headers.set("content-type", "application/json")?;
376 headers
377 }));
378 }
379
380 // A sandbox's artifacts and cache, with its job's token, which is not a
381 // g1t token either.
382 if !on_mcp
383 && let Some(rest) = path.strip_prefix("/actions/jobs/")
384 && (rest.contains("/artifacts") || rest.ends_with("/cache"))
385 {
386 let rest = rest.to_owned();
387 return blobs::for_job(request, env, &services, method, &rest).await;
388 }
389
390 let viewer = match authenticate(&request, &services).await? {
391 Ok(viewer) => viewer,
392 Err(refused) => return Ok(refused),
393 };
394 // An agent's token: what it may do comes with it.
395 if viewer.as_ref().is_some_and(|viewer| viewer.kind == PrincipalKind::Agent) {
396 let header = request.headers().get("authorization")?.unwrap_or_default();
397 let token = header.split_once(' ').map(|(_, token)| token.trim()).unwrap_or_default();
398 let scope: Option<AgentScope> = g1t_kit::call(
399 &services.identity,
400 "agent_scope",
401 &TokenArgs {
402 token: token.to_owned(),
403 },
404 )
405 .await?;
406 // A scope is what lets an agent's token do anything at all.
407 let Some(scope) = scope else {
408 return fail(FailureCode::Unauthenticated, "Invalid access token.");
409 };
410 services.scope = Some(scope);
411 }
412 if let Some(response) = oauth::handle(&mut request, &services, method, &path).await? {
413 return Ok(response);
414 }
415 if on_mcp {
416 return mcp::handle(request, &services, &viewer).await;
417 }
418
419 match (method, path.trim_end_matches('/')) {
420 ("GET", "") => return Response::from_json(&index()),
421 ("GET", "/openapi.json") => return Response::from_json(&openapi::document()),
422 // A run's artifacts: listed, or one downloaded.
423 ("GET", path) if path.starts_with("/repos/") && path.contains("/actions/runs/") && path.contains("/artifacts") => {
424 let parts: Vec<&str> = path.trim_start_matches("/repos/").split('/').collect();
425 if let [owner, repo, "actions", "runs", run, "artifacts", rest @ ..] = parts.as_slice() {
426 return match rest {
427 [] => {
428 let seen: Outcome<Value> = g1t_kit::call(
429 &services.actions,
430 "run",
431 &json!({ "repo": { "namespace": owner, "name": repo }, "viewer": viewer, "id": run }),
432 )
433 .await?;
434 match seen {
435 Outcome::Ok(_) => Response::from_json(&blobs::of_run(env, run).await?),
436 Outcome::Fail(refused) => failure(&refused),
437 }
438 }
439 [name] => blobs::download(env, &services, &viewer, owner, repo, run, name).await,
440 _ => fail(FailureCode::NotFound, "No such endpoint."),
441 };
442 }
443 }
444 ("POST", "/device/code") => return device_code(&mut request, &services).await,
445 ("POST", "/device/token") => return device_token(&mut request, &services).await,
446 // Where a pull request lives, for a tool that knows only its fork.
447 ("GET", path) if path.starts_with("/pulls/") && !path[7..].contains('/') => {
448 let located: Outcome<Value> = g1t_kit::call(
449 &services.work,
450 "locate_pull",
451 &json!({ "id": &path[7..], "viewer": viewer }),
452 )
453 .await?;
454 return match located {
455 Outcome::Ok(value) => Response::from_json(&value),
456 Outcome::Fail(refused) => failure(&refused),
457 };
458 }
459 ("POST", path) if path.starts_with("/queue/") => {
460 let entry_id = path.trim_start_matches("/queue/").to_owned();
461 return report_queue(&mut request, &services, &entry_id).await;
462 }
463 // A sandbox running a GitHub Actions job: fetching the job, and
464 // reporting how it goes. The job's own token is the credential.
465 ("POST", path) if path.starts_with("/actions/jobs/") => {
466 let rest = path.trim_start_matches("/actions/jobs/");
467 let (job, method) = match rest.strip_suffix("/spec") {
468 Some(job) => (job.to_owned(), "job_spec"),
469 None => (rest.to_owned(), "job_report"),
470 };
471 let body = json_body(&mut request).await;
472 let answered: Outcome<Value> = g1t_kit::call(
473 &services.actions,
474 method,
475 &json!({ "job": job, "token": body["token"], "report": body["report"] }),
476 )
477 .await?;
478 return match answered {
479 Outcome::Ok(value) => Response::from_json(&value),
480 Outcome::Fail(refused) => failure(&refused),
481 };
482 }
483 ("POST", path) if path.starts_with("/checks/") => {
484 let run_id = path.trim_start_matches("/checks/").to_owned();
485 return report_checks(&mut request, &services, &run_id).await;
486 }
487 ("POST", path) if path.starts_with("/runs/") && path.ends_with("/usage") => {
488 let run_id = path
489 .trim_start_matches("/runs/")
490 .trim_end_matches("/usage")
491 .to_owned();
492 return report_usage(&mut request, &services, &run_id).await;
493 }
494 ("POST", path) if path.starts_with("/plans/") => {
495 let plan_id = path.trim_start_matches("/plans/").to_owned();
496 return report_plan(&mut request, &services, &plan_id).await;
497 }
498 ("POST", path) if path.starts_with("/reviews/") => {
499 let run_id = path.trim_start_matches("/reviews/").to_owned();
500 return report_review(&mut request, &services, &run_id).await;
501 }
502 _ => {}
503 }
504
505 let query: Vec<(String, String)> = url
506 .query_pairs()
507 .map(|(name, value)| (name.into_owned(), value.into_owned()))
508 .collect();
509 let body = if method == "GET" {
510 Value::Null
511 } else {
512 snake_case_keys(json_body(&mut request).await)
513 };
514 let Some((route, input)) = rest::resolve(method, &path, &query, body) else {
515 return fail(FailureCode::NotFound, "No such endpoint.");
516 };
517 match route.op.run(&services, &viewer, &input).await? {
518 Outcome::Ok(value) => Response::from_json(&value),
519 Outcome::Fail(refused) => failure(&refused),
520 }
521}
522
523/// Request bodies take the same keys as the MCP tools, `snake_case`; the
524/// `camelCase` that responses use is accepted too, so a client can send
525/// back what it read.
526fn snake_case_keys(body: Value) -> Value {
527 let Value::Object(fields) = body else {
528 return body;
529 };
530 let mut out = serde_json::Map::new();
531 for (key, value) in fields {
532 let mut snake = String::with_capacity(key.len() + 4);
533 for c in key.chars() {
534 if c.is_ascii_uppercase() {
535 snake.push('_');
536 snake.push(c.to_ascii_lowercase());
537 } else {
538 snake.push(c);
539 }
540 }
541 // A key given in both spellings keeps the snake_case one.
542 if snake != key && out.contains_key(&snake) {
543 continue;
544 }
545 out.insert(snake, value);
546 }
547 Value::Object(out)
548}
549
550#[cfg(test)]
551mod tests {
552 use super::snake_case_keys;
553 use serde_json::json;
554
555 #[test]
556 fn camel_case_keys_are_accepted() {
557 assert_eq!(
558 snake_case_keys(json!({ "countAgentApprovals": false, "title": "x" })),
559 json!({ "count_agent_approvals": false, "title": "x" })
560 );
561 }
562
563 #[test]
564 fn snake_case_wins_when_both_are_given() {
565 assert_eq!(
566 snake_case_keys(json!({ "keep_issue_open": true, "keepIssueOpen": false })),
567 json!({ "keep_issue_open": true })
568 );
569 }
570}
571
572// The API is called from browsers too: the reference's explorer, and apps
573// built on g1t. It carries no cookies, so any origin may call it.
574#[event(fetch)]
575async fn fetch(request: Request, env: Env, _ctx: Context) -> Result<Response> {
576 let mut response = respond(request, &env).await?;
577 let headers = response.headers_mut();
578 headers.set("access-control-allow-origin", "*")?;
579 headers.set(
580 "access-control-allow-headers",
581 "authorization, content-type",
582 )?;
583 headers.set("access-control-allow-methods", "GET, POST, PATCH, OPTIONS")?;
584 headers.set("access-control-expose-headers", "www-authenticate")?;
585 Ok(response)
586}