| 1 | use topcoat::Result; |
| 2 | use topcoat::context::Cx; |
| 3 | use topcoat::view::{View, component, view}; |
| 4 | |
| 5 | use crate::ci::log_path; |
| 6 | use crate::i18n::{ |
| 7 | Lang, |
| 8 | releases_ci::{self as g, K}, |
| 9 | }; |
| 10 | use crate::pages::ci_job::log_tail; |
| 11 | use crate::pages::ci_runs::job_href; |
| 12 | use crate::state::AppState; |
| 13 | use forge_ci::steps::strip_ansi; |
| 14 | |
| 15 | const WINDOW: usize = 30; |
| 16 | const PATTERNS: [&str; 7] = [ |
| 17 | "error:", |
| 18 | "error[E", |
| 19 | "FAILED", |
| 20 | "panicked at", |
| 21 | "npm ERR!", |
| 22 | "fatal:", |
| 23 | "out of memory killer killed", |
| 24 | ]; |
| 25 | |
| 26 | fn first_error(lines: &[String]) -> Option<usize> { |
| 27 | lines |
| 28 | .iter() |
| 29 | .position(|line| PATTERNS.iter().any(|pattern| line.contains(pattern))) |
| 30 | } |
| 31 | |
| 32 | #[derive(Clone)] |
| 33 | pub struct FailFirst { |
| 34 | job_name: String, |
| 35 | job_href: String, |
| 36 | message: String, |
| 37 | has_log: bool, |
| 38 | line_no: Option<u64>, |
| 39 | error: Option<String>, |
| 40 | place: Option<String>, |
| 41 | } |
| 42 | |
| 43 | impl FailFirst { |
| 44 | pub fn line_no(&self) -> Option<u64> { |
| 45 | self.line_no |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn place_in(lines: &[String]) -> Option<String> { |
| 50 | lines |
| 51 | .iter() |
| 52 | .flat_map(|line| line.split_whitespace()) |
| 53 | .find_map(|word| { |
| 54 | let word = word.trim_matches(|c: char| matches!(c, '(' | ')' | ',')); |
| 55 | let mut parts = word.split(':'); |
| 56 | let file = parts.next()?; |
| 57 | let line = parts.next()?; |
| 58 | (file.contains('.') |
| 59 | && !file.contains("::") |
| 60 | && !line.is_empty() |
| 61 | && line.bytes().all(|b| b.is_ascii_digit())) |
| 62 | .then(|| format!("{}:{line}", file.rsplit('/').next().unwrap_or(file))) |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | pub async fn read( |
| 67 | state: &AppState, |
| 68 | title: &str, |
| 69 | job_id: &str, |
| 70 | job_name: &str, |
| 71 | message: &str, |
| 72 | ) -> Result<FailFirst> { |
| 73 | let tail = log_tail(&log_path(&state.config.data_dir, job_id)).await?; |
| 74 | let mut all: Vec<String> = tail.text.lines().map(strip_ansi).collect(); |
| 75 | while all.last().is_some_and(|line| line.trim().is_empty()) { |
| 76 | all.pop(); |
| 77 | } |
| 78 | let hit = first_error(&all); |
| 79 | let reason = hit.map(|index| { |
| 80 | all[index..] |
| 81 | .iter() |
| 82 | .take(WINDOW) |
| 83 | .find(|line| line.trim_start().starts_with("error")) |
| 84 | .unwrap_or(&all[index]) |
| 85 | .trim() |
| 86 | .to_owned() |
| 87 | }); |
| 88 | let place = hit.and_then(|index| place_in(&all[index..(index + WINDOW).min(all.len())])); |
| 89 | Ok(FailFirst { |
| 90 | job_name: job_name.to_owned(), |
| 91 | job_href: job_href(title, job_id), |
| 92 | message: message.to_owned(), |
| 93 | has_log: !all.is_empty(), |
| 94 | line_no: hit.map(|index| tail.first_line + index as u64), |
| 95 | error: reason, |
| 96 | place, |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | #[component] |
| 101 | pub async fn fail_first_block(cx: &Cx, lang: Lang, fail: FailFirst) -> Result<impl View> { |
| 102 | let line_href = fail.line_no.map(|no| format!("{}#L{no}", fail.job_href)); |
| 103 | let why = if fail.message.is_empty() { |
| 104 | fail.error.clone().unwrap_or_else(|| fail.job_name.clone()) |
| 105 | } else { |
| 106 | fail.message.clone() |
| 107 | }; |
| 108 | Ok(view! { |
| 109 | <div class="why" data-fail-first="true"> |
| 110 | <span>(why.as_str())</span> |
| 111 | if let Some(place) = fail.place.as_ref() { |
| 112 | <code>(place.as_str())</code> |
| 113 | } |
| 114 | if let Some(href) = line_href.as_ref() { |
| 115 | <a class="act danger" href=(href.as_str())>(g::t(lang, K::FailOpenLog))</a> |
| 116 | } |
| 117 | if !fail.has_log { |
| 118 | <span class="hint">(g::t(lang, K::FailNoLog))</span> |
| 119 | } else if line_href.is_none() { |
| 120 | <span class="hint">(g::t(lang, K::FailNoError))</span> |
| 121 | } |
| 122 | </div> |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | #[cfg(test)] |
| 127 | mod tests { |
| 128 | use super::place_in; |
| 129 | |
| 130 | #[test] |
| 131 | fn the_place_of_an_error_is_its_file_and_line() { |
| 132 | let lines = [ |
| 133 | "error: stream closed".to_owned(), |
| 134 | " --> crates/forge-e2e/tests/e2e/wiki_e2e.rs:212:9".to_owned(), |
| 135 | ]; |
| 136 | assert_eq!(place_in(&lines).as_deref(), Some("wiki_e2e.rs:212")); |
| 137 | assert_eq!(place_in(&["test a::b::c ... FAILED".to_owned()]), None); |
| 138 | } |
| 139 | } |