|
1 | 1 | use crate::github::{self, WorkflowRunJob};
|
2 | 2 | use crate::handlers::Context;
|
| 3 | +use crate::utils::AppError; |
3 | 4 | use anyhow::Context as _;
|
| 5 | +use axum::extract::{Path, State}; |
| 6 | +use axum::http::HeaderValue; |
| 7 | +use axum::response::IntoResponse; |
4 | 8 | use hyper::header::{CACHE_CONTROL, CONTENT_SECURITY_POLICY, CONTENT_TYPE};
|
5 |
| -use hyper::{Body, Response, StatusCode}; |
| 9 | +use hyper::{HeaderMap, StatusCode}; |
6 | 10 | use std::collections::VecDeque;
|
7 |
| -use std::str::FromStr; |
8 | 11 | use std::sync::Arc;
|
9 | 12 | use uuid::Uuid;
|
10 | 13 |
|
@@ -64,50 +67,29 @@ impl GitHubActionLogsCache {
|
64 | 67 | }
|
65 | 68 |
|
66 | 69 | pub async fn gha_logs(
|
67 |
| - ctx: Arc<Context>, |
68 |
| - owner: &str, |
69 |
| - repo: &str, |
70 |
| - log_id: &str, |
71 |
| -) -> Result<Response<Body>, hyper::Error> { |
72 |
| - let res = process_logs(ctx, owner, repo, log_id).await; |
73 |
| - let res = match res { |
74 |
| - Ok(r) => r, |
75 |
| - Err(e) => { |
76 |
| - tracing::error!("gha_logs: unable to serve logs for {owner}/{repo}#{log_id}: {e:?}"); |
77 |
| - return Ok(Response::builder() |
78 |
| - .status(StatusCode::INTERNAL_SERVER_ERROR) |
79 |
| - .body(Body::from(format!("{:?}", e))) |
80 |
| - .unwrap()); |
81 |
| - } |
82 |
| - }; |
83 |
| - |
84 |
| - Ok(res) |
85 |
| -} |
86 |
| - |
87 |
| -async fn process_logs( |
88 |
| - ctx: Arc<Context>, |
89 |
| - owner: &str, |
90 |
| - repo: &str, |
91 |
| - log_id: &str, |
92 |
| -) -> anyhow::Result<Response<Body>> { |
93 |
| - let log_id = u128::from_str(log_id).context("log_id is not a number")?; |
94 |
| - |
| 70 | + Path((owner, repo, log_id)): Path<(String, String, u128)>, |
| 71 | + State(ctx): State<Arc<Context>>, |
| 72 | +) -> axum::response::Result<impl IntoResponse, AppError> { |
95 | 73 | let repos = ctx
|
96 | 74 | .team
|
97 | 75 | .repos()
|
98 | 76 | .await
|
99 | 77 | .context("unable to retrieve team repos")?;
|
100 | 78 |
|
101 |
| - let Some(repos) = repos.repos.get(owner) else { |
102 |
| - return Ok(bad_request(format!( |
103 |
| - "organization `{owner}` is not part of the Rust Project team repos" |
104 |
| - ))); |
| 79 | + let Some(repos) = repos.repos.get(&owner) else { |
| 80 | + return Ok(( |
| 81 | + StatusCode::BAD_REQUEST, |
| 82 | + HeaderMap::new(), |
| 83 | + format!("organization `{owner}` is not part of the Rust Project team repos"), |
| 84 | + )); |
105 | 85 | };
|
106 | 86 |
|
107 | 87 | if !repos.iter().any(|r| r.name == repo) {
|
108 |
| - return Ok(bad_request(format!( |
109 |
| - "repository `{owner}` is not part of the Rust Project team repos" |
110 |
| - ))); |
| 88 | + return Ok(( |
| 89 | + StatusCode::BAD_REQUEST, |
| 90 | + HeaderMap::new(), |
| 91 | + format!("repository `{owner}` is not part of the Rust Project team repos"), |
| 92 | + )); |
111 | 93 | }
|
112 | 94 |
|
113 | 95 | let log_uuid = format!("{owner}/{repo}${log_id}");
|
@@ -368,54 +350,57 @@ body {{
|
368 | 350 |
|
369 | 351 | tracing::info!("gha_logs: serving logs for {log_uuid}");
|
370 | 352 |
|
371 |
| - return Ok(Response::builder() |
372 |
| - .status(StatusCode::OK) |
373 |
| - .header(CONTENT_TYPE, "text/html; charset=utf-8") |
374 |
| - .header( |
375 |
| - CONTENT_SECURITY_POLICY, |
376 |
| - format!( |
377 |
| - "default-src 'none'; script-src 'nonce-{nonce}' 'self'; style-src 'unsafe-inline'; img-src 'self' www.rust-lang.org" |
378 |
| - ), |
379 |
| - ) |
380 |
| - .body(Body::from(html))?); |
| 353 | + let mut headers = HeaderMap::new(); |
| 354 | + headers.insert( |
| 355 | + CONTENT_TYPE, |
| 356 | + HeaderValue::from_static("text/html; charset=utf-8"), |
| 357 | + ); |
| 358 | + headers.insert( |
| 359 | + CONTENT_SECURITY_POLICY, |
| 360 | + HeaderValue::from_str(&* |
| 361 | + format!( |
| 362 | + "default-src 'none'; script-src 'nonce-{nonce}' 'self'; style-src 'unsafe-inline'; img-src 'self' www.rust-lang.org" |
| 363 | + )).unwrap(), |
| 364 | + ); |
| 365 | + |
| 366 | + Ok((StatusCode::OK, headers, html)) |
381 | 367 | }
|
382 | 368 |
|
383 |
| -pub fn ansi_up_min_js() -> anyhow::Result<Response<Body>, hyper::Error> { |
| 369 | +pub async fn ansi_up_min_js() -> impl IntoResponse { |
384 | 370 | const ANSI_UP_MIN_JS: &str = include_str!("gha_logs/[email protected]");
|
385 | 371 |
|
386 |
| - Ok(Response::builder() |
387 |
| - .status(StatusCode::OK) |
388 |
| - .header(CACHE_CONTROL, "public, max-age=15552000, immutable") |
389 |
| - .header(CONTENT_TYPE, "text/javascript; charset=utf-8") |
390 |
| - .body(Body::from(ANSI_UP_MIN_JS)) |
391 |
| - .unwrap()) |
| 372 | + ( |
| 373 | + immutable_headers("text/javascript; charset=utf-8"), |
| 374 | + ANSI_UP_MIN_JS, |
| 375 | + ) |
392 | 376 | }
|
393 | 377 |
|
394 |
| -pub fn success_svg() -> anyhow::Result<Response<Body>, hyper::Error> { |
| 378 | +pub async fn success_svg() -> impl IntoResponse { |
395 | 379 | const SUCCESS_SVG: &str = include_str!("gha_logs/success.svg");
|
396 | 380 |
|
397 |
| - Ok(Response::builder() |
398 |
| - .status(StatusCode::OK) |
399 |
| - .header(CACHE_CONTROL, "public, max-age=15552000, immutable") |
400 |
| - .header(CONTENT_TYPE, "image/svg+xml; charset=utf-8") |
401 |
| - .body(Body::from(SUCCESS_SVG)) |
402 |
| - .unwrap()) |
| 381 | + ( |
| 382 | + immutable_headers("image/svg+xml; charset=utf-8"), |
| 383 | + SUCCESS_SVG, |
| 384 | + ) |
403 | 385 | }
|
404 | 386 |
|
405 |
| -pub fn failure_svg() -> anyhow::Result<Response<Body>, hyper::Error> { |
| 387 | +pub async fn failure_svg() -> impl IntoResponse { |
406 | 388 | const FAILURE_SVG: &str = include_str!("gha_logs/failure.svg");
|
407 | 389 |
|
408 |
| - Ok(Response::builder() |
409 |
| - .status(StatusCode::OK) |
410 |
| - .header(CACHE_CONTROL, "public, max-age=15552000, immutable") |
411 |
| - .header(CONTENT_TYPE, "image/svg+xml; charset=utf-8") |
412 |
| - .body(Body::from(FAILURE_SVG)) |
413 |
| - .unwrap()) |
| 390 | + ( |
| 391 | + immutable_headers("image/svg+xml; charset=utf-8"), |
| 392 | + FAILURE_SVG, |
| 393 | + ) |
414 | 394 | }
|
415 | 395 |
|
416 |
| -fn bad_request(body: String) -> Response<Body> { |
417 |
| - Response::builder() |
418 |
| - .status(StatusCode::BAD_REQUEST) |
419 |
| - .body(Body::from(body)) |
420 |
| - .unwrap() |
| 396 | +fn immutable_headers(content_type: &'static str) -> HeaderMap { |
| 397 | + let mut headers = HeaderMap::new(); |
| 398 | + |
| 399 | + headers.insert( |
| 400 | + CACHE_CONTROL, |
| 401 | + HeaderValue::from_static("public, max-age=15552000, immutable"), |
| 402 | + ); |
| 403 | + headers.insert(CONTENT_TYPE, HeaderValue::from_static(content_type)); |
| 404 | + |
| 405 | + headers |
421 | 406 | }
|
0 commit comments