forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.08.8 MiB7e521edTahinli2 hours ago329 commits

ci_fail.rs139 lines, 3.9 KiBHistoryRawDownload
1use topcoat::Result;
2use topcoat::context::Cx;
3use topcoat::view::{View, component, view};
4
5use crate::ci::log_path;
6use crate::i18n::{
7 Lang,
8 releases_ci::{self as g, K},
9};
10use crate::pages::ci_job::log_tail;
11use crate::pages::ci_runs::job_href;
12use crate::state::AppState;
13use forge_ci::steps::strip_ansi;
14
15const WINDOW: usize = 30;
16const 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
26fn 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)]
33pub 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
43impl FailFirst {
44 pub fn line_no(&self) -> Option<u64> {
45 self.line_no
46 }
47}
48
49fn 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
66pub 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]
101pub 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)]
127mod 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}