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.

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