forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.3 MiB·0d793e7·Tahinli·6 hours ago·359 commits
| 1 | use forge_core::ci::{CiRun, JobStep, StepState}; |
| 2 | use forge_core::{Action, Store, User, authorize}; |
| 3 | use serde_json::{Value, json}; |
| 4 | use topcoat::context::{Cx, context}; |
| 5 | use topcoat::router::request::headers; |
| 6 | use topcoat::router::response::Response; |
| 7 | use topcoat::router::{StatusCode, path_param, route}; |
| 8 | |
| 9 | use crate::api; |
| 10 | use crate::client_ip::client_ip; |
| 11 | use crate::live::{changed_stream, events_response}; |
| 12 | use crate::state::AppState; |
| 13 | use crate::views::query_value; |
| 14 | |
| 15 | path_param!(owner); |
| 16 | path_param!(repo); |
| 17 | path_param!(run_number); |
| 18 | |
| 19 | const DEFAULT_LIMIT: u32 = 20; |
| 20 | |
| 21 | fn step_state(state: StepState) -> &'static str { |
| 22 | match state { |
| 23 | StepState::Running => "running", |
| 24 | StepState::Passed => "success", |
| 25 | StepState::Failed => "failure", |
| 26 | StepState::Cancelled => "cancelled", |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | async fn run_json(store: &Store, run: &CiRun) -> forge_core::Result<Value> { |
| 31 | let jobs = store.jobs_for_run(&run.id).await?; |
| 32 | let steps = store.steps_for_run(&run.id).await?; |
| 33 | let started_at = jobs |
| 34 | .iter() |
| 35 | .filter_map(|job| job.started_at.as_deref()) |
| 36 | .min(); |
| 37 | let jobs: Vec<Value> = jobs |
| 38 | .iter() |
| 39 | .map(|job| { |
| 40 | let steps: Vec<Value> = steps |
| 41 | .iter() |
| 42 | .filter(|step: &&JobStep| step.job_id == job.id) |
| 43 | .map(|step| { |
| 44 | json!({ |
| 45 | "name": step.name, |
| 46 | "state": step_state(step.state(job.state)), |
| 47 | "started_at": step.started_at, |
| 48 | "finished_at": step.finished_at, |
| 49 | }) |
| 50 | }) |
| 51 | .collect(); |
| 52 | json!({ |
| 53 | "id": job.id, |
| 54 | "name": job.name, |
| 55 | "state": job.state.as_str(), |
| 56 | "started_at": job.started_at, |
| 57 | "finished_at": job.finished_at, |
| 58 | "exit_code": job.exit_code, |
| 59 | "steps": steps, |
| 60 | }) |
| 61 | }) |
| 62 | .collect(); |
| 63 | Ok(json!({ |
| 64 | "number": run.number, |
| 65 | "ref": run.git_ref, |
| 66 | "sha": run.commit_oid, |
| 67 | "trigger": run.trigger, |
| 68 | "state": run.state.as_str(), |
| 69 | "created_at": run.created_at, |
| 70 | "started_at": started_at, |
| 71 | "finished_at": run.finished_at, |
| 72 | "jobs": jobs, |
| 73 | })) |
| 74 | } |
| 75 | |
| 76 | async fn readable(store: &Store, actor: Option<&User>, repo_id: &str) -> bool { |
| 77 | let Ok(Some(repo)) = store.repo_by_id(repo_id).await else { |
| 78 | return false; |
| 79 | }; |
| 80 | let Ok((role, org_role)) = store.user_roles(&repo, actor).await else { |
| 81 | return false; |
| 82 | }; |
| 83 | authorize(actor, &repo, role, org_role, Action::Read).is_ok() |
| 84 | } |
| 85 | |
| 86 | async fn reader(cx: &Cx) -> Result<(forge_core::Repo, Option<User>), api::Refusal> { |
| 87 | let state = context::<AppState>(cx); |
| 88 | let actor = api::authenticate(state, client_ip(cx), headers(cx)).await?; |
| 89 | let repo = api::load_repo(state, actor.as_ref(), owner(cx), repo(cx), Action::Read).await?; |
| 90 | Ok((repo, actor)) |
| 91 | } |
| 92 | |
| 93 | async fn run_of(cx: &Cx, repo_id: &str) -> Result<CiRun, api::Refusal> { |
| 94 | let state = context::<AppState>(cx); |
| 95 | let missing = || Box::new(api::error(StatusCode::NOT_FOUND, "run not found")); |
| 96 | let number = run_number(cx).parse::<i64>().map_err(|_| missing())?; |
| 97 | match state.store.run_by_number(repo_id, number).await { |
| 98 | Ok(Some(run)) => Ok(run), |
| 99 | Ok(None) => Err(missing()), |
| 100 | Err(e) => Err(api::internal(e)), |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #[route(GET "/api/v1/repos/{owner}/{repo}/ci/runs")] |
| 105 | async fn list_runs(cx: &Cx) -> topcoat::Result<Response> { |
| 106 | let state = context::<AppState>(cx); |
| 107 | let (repo, _) = match reader(cx).await { |
| 108 | Ok(found) => found, |
| 109 | Err(res) => return Ok(*res), |
| 110 | }; |
| 111 | let limit = query_value(cx, "limit") |
| 112 | .and_then(|raw| raw.parse::<u32>().ok()) |
| 113 | .unwrap_or(DEFAULT_LIMIT); |
| 114 | let runs = match query_value(cx, "ref") { |
| 115 | Some(git_ref) => state.store.runs_on_ref(&repo.id, &git_ref, limit).await, |
| 116 | None => state |
| 117 | .store |
| 118 | .list_runs(&repo.id, 1, limit) |
| 119 | .await |
| 120 | .map(|(runs, _)| runs), |
| 121 | }; |
| 122 | let runs = match runs { |
| 123 | Ok(runs) => runs, |
| 124 | Err(e) => return Ok(*api::internal(e)), |
| 125 | }; |
| 126 | let mut out = Vec::with_capacity(runs.len()); |
| 127 | for run in &runs { |
| 128 | match run_json(&state.store, run).await { |
| 129 | Ok(value) => out.push(value), |
| 130 | Err(e) => return Ok(*api::internal(e)), |
| 131 | } |
| 132 | } |
| 133 | Ok(api::json(StatusCode::OK, &out)) |
| 134 | } |
| 135 | |
| 136 | #[route(GET "/api/v1/repos/{owner}/{repo}/ci/runs/{run_number}")] |
| 137 | async fn get_run(cx: &Cx) -> topcoat::Result<Response> { |
| 138 | let state = context::<AppState>(cx); |
| 139 | let result = async { |
| 140 | let (repo, _) = reader(cx).await?; |
| 141 | let run = run_of(cx, &repo.id).await?; |
| 142 | run_json(&state.store, &run).await.map_err(api::internal) |
| 143 | } |
| 144 | .await; |
| 145 | Ok(match result { |
| 146 | Ok(value) => api::json(StatusCode::OK, &value), |
| 147 | Err(res) => *res, |
| 148 | }) |
| 149 | } |
| 150 | |
| 151 | #[route(GET "/api/v1/repos/{owner}/{repo}/ci/runs/{run_number}/events")] |
| 152 | async fn run_events(cx: &Cx) -> topcoat::Result<Response> { |
| 153 | let state = context::<AppState>(cx); |
| 154 | let (repo, actor, run) = match async { |
| 155 | let (repo, actor) = reader(cx).await?; |
| 156 | let run = run_of(cx, &repo.id).await?; |
| 157 | Ok::<_, api::Refusal>((repo, actor, run)) |
| 158 | } |
| 159 | .await |
| 160 | { |
| 161 | Ok(found) => found, |
| 162 | Err(res) => return Ok(*res), |
| 163 | }; |
| 164 | let wake = state.ci_live.subscribe_run(&run.id); |
| 165 | let store = state.store.clone(); |
| 166 | let events = changed_stream(wake, true, "run", move || { |
| 167 | let store = store.clone(); |
| 168 | let actor = actor.clone(); |
| 169 | let repo_id = repo.id.clone(); |
| 170 | let run_id = run.id.clone(); |
| 171 | async move { |
| 172 | if !readable(&store, actor.as_ref(), &repo_id).await { |
| 173 | return Ok(None); |
| 174 | } |
| 175 | let Some(run) = store.run_by_id(&run_id).await? else { |
| 176 | return Ok(None); |
| 177 | }; |
| 178 | let value = run_json(&store, &run).await?; |
| 179 | Ok(Some((value.to_string(), run.state.is_terminal()))) |
| 180 | } |
| 181 | }); |
| 182 | events_response(cx, events) |
| 183 | } |