Skip to content

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.sh
876 linesCodeBlameRaw
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 about;
8mod run_artifacts;
9mod archives;
10mod addresses;
11mod alerts;
12mod audit;
13mod billing;
14mod blobs;
15mod checks;
16mod deployments;
17mod telemetry;
18mod deploy_keys;
19mod limits;
20mod logs;
21mod mcp;
22mod notifications;
23mod oauth;
24mod oidc;
25mod packages;
26mod folios;
27mod people;
28mod openapi;
29mod pins;
30mod projects;
31mod protection;
32mod operations;
33mod renamed;
34#[cfg(test)]
35mod responses;
36mod rest;
37mod rules;
38mod runners;
39mod security;
40mod tools;
41mod token_policy;
42mod toolkit;
43
44use g1t_contracts::identity::{
45 DeviceClaim, DeviceClaimArgs, DeviceStart, DeviceStartArgs, TokenArgs,
46};
47use g1t_contracts::work::{
48 CheckRun, Mergeable, QueueState, ReportChecksArgs, ReportMergecheckArgs,
49 ReportQueueArgs,
50};
51use g1t_contracts::{Failure, FailureCode, Outcome, PrincipalKind, User, Viewer};
52use g1t_kit::wire;
53use serde_json::{Value, json};
54use worker::{Context, Env, Method, Request, Response, Result, event};
55
56use operations::Services;
57
58fn method_name(method: Method) -> &'static str {
59 match method {
60 Method::Get => "GET",
61 Method::Post => "POST",
62 Method::Patch => "PATCH",
63 Method::Put => "PUT",
64 Method::Delete => "DELETE",
65 Method::Options => "OPTIONS",
66 Method::Head => "HEAD",
67 _ => "OTHER",
68 }
69}
70
71/// A JSON response. Every body the API sends has its keys in `snake_case`;
72/// the contracts it passes through are `camelCase`, so they are converted
73/// here, on the way out (see [`g1t_kit::wire`]). The OpenAPI document and
74/// the MCP protocol's own envelope keep the spelling their standards use.
75pub(crate) fn reply<T: serde::Serialize>(value: &T) -> Result<Response> {
76 Response::from_json(&wire::snake_case(serde_json::to_value(value)?))
77}
78
79/// The parts of a job's spec (`POST /actions/jobs/{job}/spec`) that are the
80/// workflow file, GitHub's contexts and event, and where to check out, all
81/// passed through as they are.
82const JOB_SPEC_AS_GIVEN: &[&str] = &[
83 "spec", "workflow", "github", "event", "contexts", "checkout", "permissions",
84];
85
86/// Puts the toolkit's variables in a job's spec: its runtime token, where
87/// the toolkit's services are, and where to ask for an OIDC token when the
88/// job may have one and this installation issues them. The `runtime` the
89/// actions service sent goes no further.
90fn with_runtime(spec: &mut Value, api: &str, oidc: bool) {
91 let Some(runtime) = spec.as_object_mut().and_then(|s| s.remove("runtime")) else { return };
92 let Some(token) = runtime["token"].as_str().filter(|t| !t.is_empty()) else { return };
93 let id_token = oidc && runtime["id_token"].as_bool() == Some(true);
94 let vars = toolkit::runtime_variables(api, token, id_token);
95 if let Some(variables) = spec.get_mut("variables").and_then(Value::as_object_mut) {
96 variables.extend(vars);
97 }
98}
99
100/// What a person whose account has not confirmed its email address may
101/// call: who they are, their addresses, and confirming one with the code
102/// from the email. Nothing over MCP.
103fn pending_may(method: &str, path: &str, on_mcp: bool) -> bool {
104 !on_mcp
105 && matches!(
106 (method, path.trim_end_matches('/')),
107 ("GET", "/user") | ("GET", "/user/emails") | ("POST", "/user/emails/confirm")
108 )
109}
110
111/// An error in the shape every endpoint uses.
112fn failure(failure: &Failure) -> Result<Response> {
113 Ok(reply(&json!({ "error": failure }))?.with_status(failure.code.http_status()))
114}
115
116fn fail(code: FailureCode, message: &str) -> Result<Response> {
117 failure(&Failure {
118 code,
119 message: message.to_owned(),
120 })
121}
122
123/// A request body as JSON. An empty or malformed body is no input.
124async fn json_body(request: &mut Request) -> Value {
125 request.json().await.unwrap_or(Value::Null)
126}
127
128/// Who a request's `Authorization: Bearer g1t_…` (or `token g1t_…`, as
129/// Octokit sends it) names. A missing token is an anonymous viewer; a
130/// wrong one is refused, so that a typo does not silently look signed out.
131async fn authenticate(
132 request: &Request,
133 services: &Services,
134) -> Result<std::result::Result<Viewer, Response>> {
135 let header = request.headers().get("authorization")?.unwrap_or_default();
136 let Some(token) = limits::bearer(&header) else {
137 return Ok(Ok(None));
138 };
139 let viewer: Viewer = g1t_kit::call(
140 &services.identity,
141 "user_for_access_token",
142 &TokenArgs {
143 token: token.to_owned(),
144 },
145 )
146 .await?;
147 if viewer.is_some() {
148 return Ok(Ok(viewer));
149 }
150 let mut response = fail(FailureCode::Unauthenticated, "Invalid access token.")?;
151 // Tells an MCP client where to sign in again.
152 response.headers_mut().set(
153 "www-authenticate",
154 &format!("{}, error=\"invalid_token\"", services.addresses.mcp_challenge()),
155 )?;
156 Ok(Err(response))
157}
158
159/// Where everything is, for someone or something exploring the API.
160fn index(addresses: &addresses::Addresses) -> Value {
161 let api = &addresses.api;
162 let repo = format!("{api}/repos/{{owner}}/{{name}}");
163 json!({
164 "documentation_url": "https://docs.g1t.sh/reference/api/",
165 "openapi_url": format!("{api}/openapi.json"),
166 "mcp_url": addresses.mcp,
167 "current_user_url": format!("{api}/user"),
168 "workspaces_url": format!("{api}/workspaces"),
169 "repositories_url": format!("{api}/repos{{?q}}"),
170 "search_url": format!("{api}/search{{?q,type,page,per_page}}"),
171 "repository_url": repo,
172 "repository_events_url": format!("{repo}/events{{?before}}"),
173 "labels_url": format!("{repo}/labels"),
174 "issues_url": format!("{repo}/issues{{?state,label}}"),
175 "issue_url": format!("{repo}/issues/{{number}}"),
176 "issue_comments_url": format!("{repo}/issues/{{number}}/comments"),
177 "pulls_url": format!("{repo}/pulls{{?state}}"),
178 "pull_url": format!("{repo}/pulls/{{number}}"),
179 "pull_changes_url": format!("{repo}/pulls/{{number}}/changes"),
180 "pull_reviews_url": format!("{repo}/pulls/{{number}}/reviews"),
181 "pull_session_url": format!("{repo}/pulls/{{number}}/session{{?after}}"),
182 "device_code_url": format!("{api}/device/code"),
183 "device_token_url": format!("{api}/device/token"),
184 "oauth_metadata_url": format!("{api}/.well-known/oauth-authorization-server"),
185 "git_url": format!("{}/{{owner}}/{{name}}.git", addresses.site),
186 "integrations_url": format!("{api}/workspaces/{{workspace}}/integrations"),
187 "context_url": format!("{repo}/context{{?reference}}"),
188 "import_issue_url": format!("{repo}/issues/import"),
189 "hooks_url": format!("{api}/hooks/{{integration}}"),
190 })
191}
192
193// Signing in from a tool. Accounts are created, and passwords typed, only
194// in a browser; a tool gets its token by having a person approve a code.
195
196/// Passes a request from an outside system to its connection, as it came:
197/// its signature covers the exact bytes of the body.
198async fn receive_hook(request: &mut Request, services: &Services, id: &str) -> Result<Response> {
199 let headers: std::collections::HashMap<String, String> = request
200 .headers()
201 .entries()
202 .map(|(name, value)| (name.to_lowercase(), value))
203 .collect();
204 let body = request.text().await.unwrap_or_default();
205 // A push to GitHub with many commits makes a large payload.
206 let limit = if id == "github" { 10_000_000 } else { 1_000_000 };
207 if body.len() > limit {
208 return Ok(reply(&json!({ "message": "The body is too large." }))?.with_status(413));
209 }
210 // g1t's GitHub App has one webhook for every installation; it is
211 // checked against the app's own secret.
212 let (method, args) = if id == "github" {
213 ("github_receive", json!({ "headers": headers, "body": body }))
214 } else {
215 ("receive", json!({ "id": id, "headers": headers, "body": body }))
216 };
217 let received: g1t_contracts::integrations::Received =
218 g1t_kit::call(&services.integrations, method, &args).await?;
219 Ok(reply(&json!({ "message": received.message }))?.with_status(received.status))
220}
221
222async fn receive_stripe(request: &mut Request, env: &Env) -> Result<Response> {
223 let signature = request.headers().get("stripe-signature")?.unwrap_or_default();
224 let payload = request.text().await.unwrap_or_default();
225 if payload.len() > 1_000_000 || signature.is_empty() {
226 return Ok(reply(&json!({ "message": "Not a Stripe event." }))?.with_status(400));
227 }
228 let handled: g1t_contracts::Outcome<bool> = g1t_kit::call(
229 &env.service("BILLING")?,
230 "stripe_webhook",
231 &g1t_contracts::billing::StripeWebhookArgs { payload, signature },
232 )
233 .await?;
234 // A refusal is a 400, so Stripe shows it as failed; anything handled,
235 // or already handled, is a 200, so Stripe stops sending it.
236 Ok(match handled {
237 g1t_contracts::Outcome::Ok(_) => reply(&json!({ "received": true }))?,
238 g1t_contracts::Outcome::Fail(failure) => {
239 reply(&json!({ "message": failure.message }))?.with_status(400)
240 }
241 })
242}
243
244async fn device_code(request: &mut Request, services: &Services) -> Result<Response> {
245 let body = json_body(request).await;
246 let started: DeviceStart = g1t_kit::call(
247 &services.identity,
248 "device_start",
249 &DeviceStartArgs {
250 client_name: body["client_name"].as_str().unwrap_or_default().to_owned(),
251 },
252 )
253 .await?;
254 reply(&json!({
255 "device_code": started.device_code,
256 "user_code": started.user_code,
257 "verification_uri": format!("{}/device", services.addresses.site),
258 "verification_uri_complete": format!("{}/device?code={}", services.addresses.site, started.user_code),
259 "expires_in": started.expires_in,
260 "interval": started.interval,
261 }))
262}
263
264async fn device_token(request: &mut Request, services: &Services) -> Result<Response> {
265 let body = json_body(request).await;
266 let claim: DeviceClaim = g1t_kit::call(
267 &services.identity,
268 "device_claim",
269 &DeviceClaimArgs {
270 device_code: body["device_code"].as_str().unwrap_or_default().to_owned(),
271 },
272 )
273 .await?;
274 reply(&match claim {
275 DeviceClaim::Approved { token, user } => json!({
276 "status": "approved",
277 "token": token,
278 "display_username": user.display_username.clone().filter(|display| display.eq_ignore_ascii_case(&user.username)).unwrap_or_else(|| user.username.clone()),
279 "username": user.username,
280 "verified": user.verified,
281 }),
282 DeviceClaim::Pending => json!({ "status": "pending" }),
283 DeviceClaim::Denied => json!({ "status": "denied" }),
284 DeviceClaim::Expired => json!({ "status": "expired" }),
285 })
286}
287
288/// A sandbox reporting on a run of an issue's commands, from before a pull
289/// request's checks were the workflows run on it.
290/// The run's own token, in the body, is the credential: it was given to
291/// that sandbox and to nothing else.
292async fn report_checks(
293 request: &mut Request,
294 services: &Services,
295 run_id: &str,
296) -> Result<Response> {
297 let body = json_body(request).await;
298 let reported: Outcome<CheckRun> = g1t_kit::call(
299 &services.work,
300 "report_checks",
301 &ReportChecksArgs {
302 run_id: run_id.to_owned(),
303 token: body["token"].as_str().unwrap_or_default().to_owned(),
304 results: serde_json::from_value(body["results"].clone()).unwrap_or_default(),
305 error: body["error"].as_str().map(str::to_owned),
306 skip: false,
307 },
308 )
309 .await?;
310 match reported {
311 Outcome::Ok(run) => reply(&json!({ "status": run.status })),
312 Outcome::Fail(refused) => failure(&refused),
313 }
314}
315
316/// A sandbox reporting one tested state of a merge queue. As with checks,
317/// the entry's own token is the credential.
318async fn report_queue(
319 request: &mut Request,
320 services: &Services,
321 entry_id: &str,
322) -> Result<Response> {
323 let body = json_body(request).await;
324 let reported: Outcome<QueueState> = g1t_kit::call(
325 &services.work,
326 "report_queue",
327 &ReportQueueArgs {
328 entry_id: entry_id.to_owned(),
329 token: body["token"].as_str().unwrap_or_default().to_owned(),
330 combined_commit: body["combinedCommit"].as_str().map(str::to_owned),
331 results: serde_json::from_value(body["results"].clone()).unwrap_or_default(),
332 error: body["error"].as_str().map(str::to_owned),
333 conflict_with: body["conflictWith"].as_u64().map(|n| n as u32),
334 conflicts: serde_json::from_value(body["conflicts"].clone()).unwrap_or_default(),
335 },
336 )
337 .await?;
338 match reported {
339 Outcome::Ok(state) => reply(&json!({ "state": state })),
340 Outcome::Fail(refused) => failure(&refused),
341 }
342}
343
344/// A sandbox reporting whether a pull request merges cleanly. As with
345/// checks, the probe's own token is the credential.
346async fn report_mergecheck(
347 request: &mut Request,
348 services: &Services,
349 pull_id: &str,
350) -> Result<Response> {
351 let body = json_body(request).await;
352 let reported: Outcome<Mergeable> = g1t_kit::call(
353 &services.work,
354 "report_mergecheck",
355 &ReportMergecheckArgs {
356 pull_id: pull_id.to_owned(),
357 token: body["token"].as_str().unwrap_or_default().to_owned(),
358 conflicts: serde_json::from_value(body["conflicts"].clone()).unwrap_or_default(),
359 error: body["error"].as_str().map(str::to_owned),
360 },
361 )
362 .await?;
363 match reported {
364 Outcome::Ok(state) => reply(&json!({ "mergeable": state })),
365 Outcome::Fail(refused) => failure(&refused),
366 }
367}
368
369/// A backup's sandbox, passed on to the repos service, which holds the
370/// job (services/repos/src/backups.rs; the flow is in
371/// `g1t_contracts::backups`):
372///
373/// - `POST /backups/{job}/spec`: what to cut, and a read-only git credential
374/// - `PUT /backups/{job}/parts/{n}`: one part of the bundle, as bytes
375/// - `POST /backups/{job}/complete` with `{ refs, size, sha256, parts, fetched_bytes }`
376/// - `POST /backups/{job}/fail` with `{ error, fetched_bytes }`
377///
378/// Bodies are passed through as they are: snake_case already, and a
379/// bundle's refs are keyed by ref names, which must not be converted.
380async fn backup_job(request: &mut Request, services: &Services, method: &str, path: &str) -> Result<Response> {
381 use g1t_contracts::backups::TOKEN_HEADER;
382 let token = request.headers().get(TOKEN_HEADER)?.unwrap_or_default();
383 let rest = path.trim_start_matches("/backups/");
384 let (job, action) = rest.split_once('/').unwrap_or((rest, ""));
385 if job.is_empty() || token.is_empty() {
386 return fail(FailureCode::Unauthenticated, "A backup job's token is required.");
387 }
388 if method == "PUT" && action.starts_with("parts/") {
389 let bytes = request.bytes().await?;
390 if bytes.len() as u64 > g1t_contracts::backups::PART_BYTES {
391 return fail(FailureCode::Invalid, "A part holds 32 MiB at most.");
392 }
393 let headers = worker::Headers::new();
394 headers.set(TOKEN_HEADER, &token)?;
395 let mut init = worker::RequestInit::new();
396 init.with_method(Method::Put)
397 .with_headers(headers)
398 .with_body(Some(worker::js_sys::Uint8Array::from(bytes.as_slice()).into()));
399 let forwarded = Request::new_with_init(&format!("https://repos/backups/{job}/{action}"), &init)?;
400 let mut answered = services.repos.fetch_request(forwarded).await?;
401 return outcome_as_given(answered.json().await?);
402 }
403 let rpc = match (method, action) {
404 ("POST", "spec") => "backup_spec",
405 ("POST", "complete") => "backup_complete",
406 ("POST", "fail") => "backup_fail",
407 _ => return fail(FailureCode::NotFound, "No such endpoint."),
408 };
409 let mut body = json_body(request).await;
410 if !body.is_object() {
411 body = json!({});
412 }
413 body["job_id"] = json!(job);
414 body["token"] = json!(token);
415 let answered: Value = g1t_kit::call(&services.repos, rpc, &body).await?;
416 outcome_as_given(answered)
417}
418
419/// An `Outcome` from a service whose keys are already the API's: the value,
420/// or the failure in the shape every endpoint uses.
421fn outcome_as_given(answered: Value) -> Result<Response> {
422 match serde_json::from_value::<Outcome<Value>>(answered)? {
423 Outcome::Ok(value) => Response::from_json(&value),
424 Outcome::Fail(refused) => failure(&refused),
425 }
426}
427
428async fn respond(mut request: Request, env: &Env) -> Result<Response> {
429 let method = method_name(request.method());
430 if method == "OPTIONS" {
431 return Ok(Response::empty()?.with_status(204));
432 }
433 let url = request.url()?;
434 // Paths carry no version. An earlier form began with `/v1`, which is
435 // still accepted so that nothing already written against it breaks.
436 let path = match url.path().strip_prefix("/v1") {
437 Some(rest) if rest.is_empty() || rest.starts_with('/') => rest.to_owned(),
438 _ => url.path().to_owned(),
439 };
440 let mut services = Services::new(env)?;
441 // MCP is a host of its own hosted, and may be a path on this one
442 // self-hosted (addresses.rs).
443 let on_mcp = services.addresses.mcp_path(&url).is_some();
444
445 // Stripe reporting to billing. Signed with the secret of the endpoint
446 // billing registered; the body goes through exactly as received, since
447 // the signature covers its bytes.
448 if method == "POST" && !on_mcp && path == "/stripe/webhook" {
449 return receive_stripe(&mut request, env).await;
450 }
451
452 // Outside systems reporting to a connection. They sign what they send
453 // with the connection's own secret, which is not a g1t token, so this
454 // comes before anything that would read one.
455 if method == "POST" && !on_mcp
456 && let Some(id) = path.strip_prefix("/hooks/").filter(|id| !id.is_empty() && !id.contains('/')) {
457 return receive_hook(&mut request, &services, id).await;
458 }
459
460 // A sandbox building a deployment, reporting with its build's token,
461 // which is not a g1t token, or a container app's Worker reporting how
462 // long its container ran, with its own token. The body goes through as
463 // it is: it can carry a Worker's bundled code.
464 if method == "POST" && !on_mcp
465 && let Some(rest) = path
466 .strip_prefix("/deployments/jobs/")
467 .map(|rest| format!("jobs/{rest}"))
468 .or_else(|| path.strip_prefix("/deployments/apps/").filter(|rest| rest.ends_with("/meter")).map(|rest| format!("apps/{rest}")))
469 {
470 let target = format!("https://deployments/{rest}");
471 let body = request.bytes().await?;
472 let headers = worker::Headers::new();
473 headers.set("content-type", "application/json")?;
474 let mut init = worker::RequestInit::new();
475 init.with_method(Method::Post)
476 .with_headers(headers)
477 .with_body(Some(worker::js_sys::Uint8Array::from(body.as_slice()).into()));
478 let mut answer = env
479 .service("DEPLOYMENTS")?
480 .fetch_request(Request::new_with_init(&target, &init)?)
481 .await?;
482 // A fresh response: a fetched one's headers cannot be changed, and
483 // every response gets the API's own on the way out.
484 let status = answer.status_code();
485 let bytes = answer.bytes().await?;
486 let bytes = match serde_json::from_slice::<Value>(&bytes) {
487 Ok(body) => serde_json::to_vec(&wire::snake_case(body))?,
488 Err(_) => bytes,
489 };
490 return Ok(Response::from_bytes(bytes)?
491 .with_status(status)
492 .with_headers({
493 let headers = worker::Headers::new();
494 headers.set("content-type", "application/json")?;
495 headers
496 }));
497 }
498
499 // The services GitHub's toolkit calls from inside a job, with its
500 // runtime token, and the links they hand out (toolkit.rs).
501 if !on_mcp && method == "POST"
502 && let Some(rest) = path.strip_prefix("/twirp/")
503 {
504 let (service, rpc) = rest.split_once('/').unwrap_or((rest, ""));
505 let (service, rpc) = (service.to_owned(), rpc.to_owned());
506 return toolkit::twirp(request, env, &services, &service, &rpc).await;
507 }
508 if !on_mcp && let Some(rest) = path.strip_prefix("/actions/toolkit/_apis/artifactcache/") {
509 let rest = rest.to_owned();
510 return toolkit::cache_v1(request, env, &services, method, &rest).await;
511 }
512 if !on_mcp && let Some(token) = path.strip_prefix("/actions/toolkit/blobs/") {
513 let token = token.to_owned();
514 return toolkit::blob(request, env, &services, method, &token).await;
515 }
516 // g1t as an OIDC issuer for workflow jobs (oidc.rs).
517 if !on_mcp && method == "GET" && path.starts_with("/actions/oidc/") {
518 return oidc::handle(&request, env, &services, &path).await;
519 }
520
521 // A sandbox's artifacts and cache, with its job's token, which is not a
522 // g1t token either.
523 if !on_mcp
524 && let Some(rest) = path.strip_prefix("/actions/jobs/")
525 && (rest.contains("/artifacts") || rest.ends_with("/cache") || rest.contains("/cache/uploads"))
526 {
527 let rest = rest.to_owned();
528 return blobs::for_job(request, env, &services, method, &rest).await;
529 }
530
531 // A self-hosted runner, with a registration token or its own
532 // credential, neither of which is a g1t access token.
533 if method == "POST"
534 && !on_mcp
535 && path.starts_with("/runners/")
536 && let Some(response) = runners::handle(&mut request, &services, &path).await?
537 {
538 return Ok(response);
539 }
540
541 // Per token, or per address without one (limits.rs).
542 if let Some(limited) = limits::limited(&request, env, method, &path, on_mcp).await? {
543 return Ok(limited);
544 }
545 let viewer = match authenticate(&request, &services).await? {
546 Ok(viewer) => viewer,
547 Err(refused) => return Ok(limits::wrong_token(&request, env, on_mcp).await?.unwrap_or(refused)),
548 };
549 // A person who has not confirmed their email address: who they are,
550 // their addresses, and confirming one, nothing else (REST or MCP).
551 if viewer.as_ref().is_some_and(User::awaits_confirmation) && !pending_may(method, &path, on_mcp) {
552 return fail(
553 FailureCode::Forbidden,
554 &g1t_contracts::accounts::confirm_email_first(&services.addresses.site),
555 );
556 }
557 services.audit = audit::AuditContext::of(&request, on_mcp);
558 // A run's token: what it may do comes with it, on the composite
559 // identity identity resolved it to. Any other token of g1t's own
560 // principal (the agent tokens g1t no longer issues) does nothing.
561 if let Some(acting) = viewer.as_ref().and_then(|viewer| viewer.acting.as_ref()) {
562 services.scope = Some(acting.scope.clone());
563 } else if viewer.as_ref().is_some_and(|viewer| viewer.kind == PrincipalKind::Agent) {
564 return fail(FailureCode::Unauthenticated, "Invalid access token.");
565 }
566 if let Some(response) = oauth::handle(&mut request, &services, method, &path).await? {
567 return Ok(response);
568 }
569 if on_mcp {
570 return mcp::handle(request, &services, &viewer).await;
571 }
572
573 // Workflow logs to download: a run's as a zip, a job's as text (logs.rs).
574 if method == "GET" {
575 let text = url.query_pairs().any(|(name, value)| name == "format" && value == "text");
576 if let Some(wanted) = logs::wanted(&path, text) {
577 return logs::download(&services, &viewer, wanted).await;
578 }
579 }
580
581 match (method, path.trim_end_matches('/')) {
582 ("GET", "") => return reply(&index(&services.addresses)),
583 ("GET", "/openapi.json") => return Response::from_json(&openapi::document()),
584 // One of a run's artifacts downloaded by name (the run's artifacts
585 // are listed by the REST route in rest.rs).
586 ("GET", path) if path.starts_with("/repos/") && path.contains("/actions/runs/") && path.contains("/artifacts/") => {
587 let parts: Vec<&str> = path.trim_start_matches("/repos/").split('/').collect();
588 if let [owner, repo, "actions", "runs", run, "artifacts", name] = parts.as_slice() {
589 // A workflow job's token reaches its own repository only.
590 if viewer
591 .as_ref()
592 .and_then(|user| user.token.as_deref())
593 .is_some_and(|token| !token.reaches(&format!("{owner}/{repo}")))
594 {
595 return fail(FailureCode::NotFound, "No such run.");
596 }
597 return blobs::download(env, &services, &viewer, owner, repo, run, name).await;
598 }
599 }
600 ("POST", "/device/code") => return device_code(&mut request, &services).await,
601 ("POST", "/device/token") => return device_token(&mut request, &services).await,
602 // Where a pull request lives, for a tool that knows only its fork.
603 ("GET", path) if path.starts_with("/pulls/") && !path[7..].contains('/') => {
604 let located: Outcome<Value> = g1t_kit::call(
605 &services.work,
606 "locate_pull",
607 &json!({ "id": &path[7..], "viewer": viewer }),
608 )
609 .await?;
610 return match located {
611 Outcome::Ok(value) => reply(&value),
612 Outcome::Fail(refused) => failure(&refused),
613 };
614 }
615 ("POST", path) if path.starts_with("/mergechecks/") => {
616 let pull_id = path.trim_start_matches("/mergechecks/").to_owned();
617 return report_mergecheck(&mut request, &services, &pull_id).await;
618 }
619 // A sandbox making a repository's nightly backup. The job's own
620 // token, in its header, is the credential.
621 (method, path) if path.starts_with("/backups/") => {
622 return backup_job(&mut request, &services, method, path).await;
623 }
624 ("POST", path) if path.starts_with("/queue/") => {
625 let entry_id = path.trim_start_matches("/queue/").to_owned();
626 return report_queue(&mut request, &services, &entry_id).await;
627 }
628 // A sandbox running a GitHub Actions job: fetching the job, and
629 // reporting how it goes. The job's own token is the credential.
630 // A sandbox downloading an action's archive that g1t keeps
631 // (actions' registry.rs). The job's own token is the credential.
632 ("POST", path) if path.starts_with("/actions/jobs/") && path.ends_with("/archive") => {
633 let job = path.trim_start_matches("/actions/jobs/").trim_end_matches("/archive").to_owned();
634 let body = json_body(&mut request).await;
635 let kept: Outcome<Value> = g1t_kit::call(
636 &services.actions,
637 "job_action_archive",
638 &json!({ "job": job, "token": body["token"], "report": body["report"] }),
639 )
640 .await?;
641 let kept = match kept {
642 Outcome::Ok(kept) => kept,
643 Outcome::Fail(refused) => return failure(&refused),
644 };
645 let bucket = env.bucket("ACTIONS_CACHE")?;
646 let object = kept["object"].as_str().unwrap_or_default();
647 let Some(stored) = bucket.get(object).execute().await? else {
648 return fail(FailureCode::NotFound, "The archive is gone.");
649 };
650 let Some(body) = stored.body() else {
651 return fail(FailureCode::NotFound, "The archive is gone.");
652 };
653 let mut response = Response::from_body(body.response_body()?)?;
654 response.headers_mut().set("content-type", "application/gzip")?;
655 return Ok(response);
656 }
657 ("POST", path) if path.starts_with("/actions/jobs/") => {
658 let rest = path.trim_start_matches("/actions/jobs/");
659 // `/action`: where to fetch another repository's action from (on
660 // g1t, with a read token for a private one, or GitHub).
661 let (job, method) = match (rest.strip_suffix("/spec"), rest.strip_suffix("/action")) {
662 (Some(job), _) => (job.to_owned(), "job_spec"),
663 (_, Some(job)) => (job.to_owned(), "job_action"),
664 _ => (rest.to_owned(), "job_report"),
665 };
666 let body = json_body(&mut request).await;
667 // `rotate`: a harness that takes a new token with the spec and
668 // lets go of the one it asked with (plan.rs `job_spec`).
669 let rotate = method == "job_spec" && body["rotate"].as_bool() == Some(true);
670 let answered: Outcome<Value> = g1t_kit::call(
671 &services.actions,
672 method,
673 &json!({ "job": job, "token": body["token"], "report": body["report"], "rotate": rotate }),
674 )
675 .await?;
676 return match answered {
677 // A job's spec is the workflow and its contexts as GitHub
678 // has them; only g1t's own keys around them are converted.
679 Outcome::Ok(value) => {
680 let mut spec = wire::snake_case_keeping(value, JOB_SPEC_AS_GIVEN);
681 with_runtime(&mut spec, &services.addresses.api, oidc::configured(env));
682 Response::from_json(&spec)
683 }
684 Outcome::Fail(refused) => failure(&refused),
685 };
686 }
687 ("POST", path) if path.starts_with("/checks/") => {
688 let run_id = path.trim_start_matches("/checks/").to_owned();
689 return report_checks(&mut request, &services, &run_id).await;
690 }
691 _ => {}
692 }
693
694 let query: Vec<(String, String)> = url
695 .query_pairs()
696 .map(|(name, value)| (name.into_owned(), value.into_owned()))
697 .collect();
698 let body = if method == "GET" {
699 Value::Null
700 } else {
701 snake_case_keys(json_body(&mut request).await)
702 };
703 let Some((route, input)) = rest::resolve(method, &path, &query, body) else {
704 return fail(FailureCode::NotFound, "No such endpoint.");
705 };
706 match audit::run(route.op, &services, &viewer, &input).await? {
707 // A download is a redirect to its signed link, as GitHub's is: an
708 // artifact's, and a repository's tarball and zipball.
709 Outcome::Ok(value) if route.op.redirects() => {
710 match value["url"].as_str().and_then(|url| worker::Url::parse(url).ok()) {
711 Some(url) => Response::redirect_with_status(url, 302),
712 None => reply(&value),
713 }
714 }
715 // A deleted comment has nothing to say, as GitHub's says nothing.
716 Outcome::Ok(_) if route.no_content() => Ok(Response::empty()?.with_status(204)),
717 Outcome::Ok(value) => Response::from_json(&wire::snake_case_keeping(value, route.op.as_given())),
718 // A token without the scope a call needs is told which one.
719 Outcome::Fail(refused) => match (refused.code, audit::missing_scope(route.op, &viewer, &input)) {
720 (FailureCode::Forbidden, Some(scope)) => Ok(reply(&json!({
721 "error": {
722 "code": refused.code,
723 "message": refused.message,
724 "needed_scope": scope.as_str(),
725 }
726 }))?
727 .with_status(403)),
728 _ => failure(&refused),
729 },
730 }
731}
732
733/// Request bodies take the same keys as the MCP tools, `snake_case`, as
734/// responses use; the `camelCase` spelling is accepted too.
735fn snake_case_keys(body: Value) -> Value {
736 let Value::Object(fields) = body else {
737 return body;
738 };
739 let mut out = serde_json::Map::new();
740 for (key, value) in fields {
741 let mut snake = String::with_capacity(key.len() + 4);
742 for c in key.chars() {
743 if c.is_ascii_uppercase() {
744 snake.push('_');
745 snake.push(c.to_ascii_lowercase());
746 } else {
747 snake.push(c);
748 }
749 }
750 // A key given in both spellings keeps the snake_case one.
751 if snake != key && out.contains_key(&snake) {
752 continue;
753 }
754 out.insert(snake, value);
755 }
756 Value::Object(out)
757}
758
759#[cfg(test)]
760mod tests {
761 use super::snake_case_keys;
762 use serde_json::json;
763
764 #[test]
765 fn an_unconfirmed_account_may_only_see_itself_and_confirm_its_address() {
766 assert!(super::pending_may("GET", "/user", false));
767 assert!(super::pending_may("GET", "/user/emails/", false));
768 assert!(super::pending_may("POST", "/user/emails/confirm", false));
769 assert!(!super::pending_may("POST", "/user/emails", false));
770 assert!(!super::pending_may("POST", "/workspaces", false));
771 assert!(!super::pending_may("GET", "/repos/acme/rocket", false));
772 assert!(!super::pending_may("POST", "/user/emails/confirm", true));
773 assert!(!super::pending_may("POST", "/", true));
774 }
775
776 #[test]
777 fn a_job_spec_gets_the_toolkits_variables() {
778 // As the actions service sends it, converted as the API does.
779 let sent = json!({ "variables": { "GITHUB_SHA": "abc" }, "runtime": { "token": "h.p.s", "idToken": true }, "nextToken": "n" });
780 let mut spec = g1t_kit::wire::snake_case_keeping(sent.clone(), super::JOB_SPEC_AS_GIVEN);
781 super::with_runtime(&mut spec, "https://api.g1t.sh", true);
782 assert!(spec.get("runtime").is_none(), "the runner never sees it");
783 // The token that replaces the one the harness asked with.
784 assert_eq!(spec["next_token"], "n");
785 let vars = &spec["variables"];
786 assert_eq!(vars["GITHUB_SHA"], "abc");
787 assert_eq!(vars["ACTIONS_RUNTIME_TOKEN"], "h.p.s");
788 assert_eq!(vars["ACTIONS_CACHE_URL"], "https://api.g1t.sh/actions/toolkit/");
789 assert_eq!(vars["ACTIONS_ID_TOKEN_REQUEST_TOKEN"], "h.p.s");
790 // No OIDC key here: no OIDC variables, whatever the job may do.
791 let mut spec = g1t_kit::wire::snake_case_keeping(sent, super::JOB_SPEC_AS_GIVEN);
792 super::with_runtime(&mut spec, "https://api.g1t.sh", false);
793 assert!(spec["variables"].get("ACTIONS_ID_TOKEN_REQUEST_URL").is_none());
794 assert_eq!(spec["variables"]["ACTIONS_RESULTS_URL"], "https://api.g1t.sh/");
795 }
796
797 #[test]
798 fn camel_case_keys_are_accepted() {
799 assert_eq!(
800 snake_case_keys(json!({ "countAgentApprovals": false, "title": "x" })),
801 json!({ "count_agent_approvals": false, "title": "x" })
802 );
803 }
804
805 #[test]
806 fn snake_case_wins_when_both_are_given() {
807 assert_eq!(
808 snake_case_keys(json!({ "keep_issue_open": true, "keepIssueOpen": false })),
809 json!({ "keep_issue_open": true })
810 );
811 }
812}
813
814// The API is called from browsers too: the reference's explorer, and apps
815// built on g1t. It carries no cookies, so any origin may call it.
816/// The signal an OTLP path sends (`/otlp/v1/traces`, `logs` or `metrics`),
817/// if it is one.
818fn otlp_signal(path: &str) -> Option<&'static str> {
819 let path = path.strip_prefix("/v1").filter(|rest| rest.starts_with('/')).unwrap_or(path);
820 match path.trim_end_matches('/') {
821 "/otlp/v1/traces" => Some("traces"),
822 "/otlp/v1/logs" => Some("logs"),
823 "/otlp/v1/metrics" => Some("metrics"),
824 _ => None,
825 }
826}
827
828/// OpenTelemetry sent to the API's own address: a self-hosted install's
829/// only door, and a second one hosted. It goes to the telemetry service as
830/// it came, which checks the project's ingest token itself (not a g1t
831/// token), and its answer comes back as it is, CORS headers included.
832async fn forward_otlp(mut request: Request, env: &Env, signal: &str) -> Result<Response> {
833 let method = request.method();
834 let headers = worker::Headers::new();
835 for name in ["authorization", "x-g1t-token", "content-type", "content-encoding"] {
836 if let Some(value) = request.headers().get(name)? {
837 headers.set(name, &value)?;
838 }
839 }
840 let mut init = worker::RequestInit::new();
841 init.with_method(method.clone()).with_headers(headers);
842 if method == Method::Post {
843 let body = request.bytes().await?;
844 init.with_body(Some(worker::js_sys::Uint8Array::from(body.as_slice()).into()));
845 }
846 let target = format!("https://telemetry/v1/{signal}");
847 let mut answer = env.service("TELEMETRY")?.fetch_request(Request::new_with_init(&target, &init)?).await?;
848 // A fresh response, with the service's headers: a fetched one's cannot
849 // be changed, and nothing should be added to them.
850 let status = answer.status_code();
851 let headers = worker::Headers::new();
852 for (name, value) in answer.headers().entries() {
853 headers.set(&name, &value)?;
854 }
855 let bytes = answer.bytes().await?;
856 let response = if bytes.is_empty() { Response::empty()? } else { Response::from_bytes(bytes)? };
857 Ok(response.with_status(status).with_headers(headers))
858}
859
860#[event(fetch)]
861async fn fetch(request: Request, env: Env, _ctx: Context) -> Result<Response> {
862 // OTLP passes through untouched, before any g1t token is read.
863 if let Some(signal) = otlp_signal(&request.path()) {
864 return forward_otlp(request, &env, signal).await;
865 }
866 let mut response = respond(request, &env).await?;
867 let headers = response.headers_mut();
868 headers.set("access-control-allow-origin", "*")?;
869 headers.set(
870 "access-control-allow-headers",
871 "authorization, content-type",
872 )?;
873 headers.set("access-control-allow-methods", "GET, POST, PATCH, OPTIONS")?;
874 headers.set("access-control-expose-headers", "www-authenticate, retry-after")?;
875 Ok(response)
876}