| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::path::{Path, PathBuf}; |
| 6 | |
| 7 | use forge_core::Repo; |
| 8 | use forge_core::ci::{CiJob, CiRun, JobState, RunState}; |
| 9 | use forge_core::store::commit_status::StatusState; |
| 10 | |
| 11 | use crate::state::AppState; |
| 12 | |
| 13 | pub mod api; |
| 14 | pub mod live; |
| 15 | pub mod reaper; |
| 16 | pub mod schedule; |
| 17 | |
| 18 | pub use live::CiLive; |
| 19 | pub use schedule::{manual_run, on_pull_event, on_ref_update}; |
| 20 | |
| 21 | |
| 22 | const MAX_DESCRIPTION: usize = 140; |
| 23 | |
| 24 | |
| 25 | pub fn log_path(data_dir: &Path, job_id: &str) -> PathBuf { |
| 26 | data_dir |
| 27 | .join("ci") |
| 28 | .join("logs") |
| 29 | .join(format!("{job_id}.log")) |
| 30 | } |
| 31 | |
| 32 | |
| 33 | pub fn artifact_key(artifact_id: &str) -> String { |
| 34 | format!("ci/{artifact_id}") |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | pub async fn sync_status(state: &AppState, run: &CiRun, job: &CiJob) { |
| 40 | let repo = match state.store.repo_by_id(&run.repo_id).await { |
| 41 | Ok(Some(repo)) => repo, |
| 42 | Ok(None) => return, |
| 43 | Err(e) => { |
| 44 | tracing::error!(error = %e, run_id = %run.id, "ci: loading repo for status failed"); |
| 45 | return; |
| 46 | } |
| 47 | }; |
| 48 | let (status, description) = status_for(run, job); |
| 49 | let url = format!( |
| 50 | "{}/{}/{}/ci/jobs/{}", |
| 51 | state.config.base_url.trim_end_matches('/'), |
| 52 | repo.owner_name, |
| 53 | repo.name, |
| 54 | job.id |
| 55 | ); |
| 56 | publish( |
| 57 | state, |
| 58 | &repo, |
| 59 | run, |
| 60 | &format!("ci/{}", job.name), |
| 61 | status, |
| 62 | &description, |
| 63 | &url, |
| 64 | ) |
| 65 | .await; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | pub async fn sync_run_statuses(state: &AppState, run: &CiRun) -> forge_core::Result<bool> { |
| 71 | let jobs = state.store.jobs_for_run(&run.id).await?; |
| 72 | let mut queued = false; |
| 73 | for job in &jobs { |
| 74 | sync_status(state, run, job).await; |
| 75 | queued |= matches!(job.state, JobState::Queued); |
| 76 | } |
| 77 | Ok(queued) |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | pub(crate) async fn publish( |
| 83 | state: &AppState, |
| 84 | repo: &Repo, |
| 85 | run: &CiRun, |
| 86 | context: &str, |
| 87 | status: StatusState, |
| 88 | description: &str, |
| 89 | target_url: &str, |
| 90 | ) { |
| 91 | let Some(creator) = status_creator(state, repo, &run.actor_id).await else { |
| 92 | tracing::error!(run_id = %run.id, context, "ci: no person to record the status for"); |
| 93 | return; |
| 94 | }; |
| 95 | let description = short(description); |
| 96 | if let Err(e) = state |
| 97 | .store |
| 98 | .set_status( |
| 99 | &repo.id, |
| 100 | &run.commit_oid, |
| 101 | context, |
| 102 | status, |
| 103 | Some(&description), |
| 104 | Some(target_url), |
| 105 | &creator, |
| 106 | ) |
| 107 | .await |
| 108 | { |
| 109 | tracing::error!(error = %e, run_id = %run.id, context, "ci: writing commit status failed"); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | async fn status_creator(state: &AppState, repo: &Repo, actor_id: &str) -> Option<String> { |
| 117 | let candidate = if actor_id.is_empty() { |
| 118 | repo.owner_id.clone() |
| 119 | } else { |
| 120 | actor_id.to_owned() |
| 121 | }; |
| 122 | match state.store.user_by_id(&candidate).await { |
| 123 | Ok(Some(user)) if !user.is_org() => Some(candidate), |
| 124 | _ => state |
| 125 | .store |
| 126 | .ensure_ghost_user() |
| 127 | .await |
| 128 | .ok() |
| 129 | .map(|ghost| ghost.id), |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | fn status_for(run: &CiRun, job: &CiJob) -> (StatusState, String) { |
| 135 | let message = |fallback: &str| job.message.clone().unwrap_or_else(|| fallback.to_owned()); |
| 136 | match job.state { |
| 137 | JobState::Blocked => { |
| 138 | let why = if matches!(run.state, RunState::WaitingApproval) { |
| 139 | "Waiting for approval" |
| 140 | } else { |
| 141 | "Waiting for other jobs" |
| 142 | }; |
| 143 | (StatusState::Pending, why.to_owned()) |
| 144 | } |
| 145 | JobState::Queued => (StatusState::Pending, "Queued".to_owned()), |
| 146 | JobState::Running => (StatusState::Pending, "Running".to_owned()), |
| 147 | JobState::Success => (StatusState::Success, "Succeeded".to_owned()), |
| 148 | JobState::Failure => (StatusState::Failure, "Failed".to_owned()), |
| 149 | JobState::Cancelled => (StatusState::Error, message("Cancelled")), |
| 150 | JobState::Error => (StatusState::Error, message("Errored")), |
| 151 | JobState::Skipped => (StatusState::Error, "skipped".to_owned()), |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | fn short(text: &str) -> String { |
| 157 | text.lines() |
| 158 | .next() |
| 159 | .unwrap_or_default() |
| 160 | .chars() |
| 161 | .take(MAX_DESCRIPTION) |
| 162 | .collect() |
| 163 | } |
| 164 | |
| 165 | #[cfg(test)] |
| 166 | mod org_tests { |
| 167 | use super::*; |
| 168 | use forge_core::Visibility; |
| 169 | |
| 170 | |
| 171 | |
| 172 | #[tokio::test] |
| 173 | async fn an_org_never_is_the_status_creator() { |
| 174 | let dir = tempfile::tempdir().unwrap(); |
| 175 | let state = crate::test_support::test_state(dir.path()).await; |
| 176 | let alice = state |
| 177 | .store |
| 178 | .create_user("alice", None, "h", false) |
| 179 | .await |
| 180 | .unwrap(); |
| 181 | let org = state.store.create_org("acme", &alice.id).await.unwrap(); |
| 182 | let repo = state |
| 183 | .store |
| 184 | .create_repo(&org.id, "proj", "", Visibility::Private, "main") |
| 185 | .await |
| 186 | .unwrap(); |
| 187 | let ghost = state.store.ensure_ghost_user().await.unwrap(); |
| 188 | |
| 189 | assert_eq!( |
| 190 | status_creator(&state, &repo, &alice.id).await, |
| 191 | Some(alice.id.clone()) |
| 192 | ); |
| 193 | assert_eq!( |
| 194 | status_creator(&state, &repo, &org.id).await, |
| 195 | Some(ghost.id.clone()) |
| 196 | ); |
| 197 | assert_eq!(status_creator(&state, &repo, "").await, Some(ghost.id)); |
| 198 | } |
| 199 | } |