1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Parsing error messages from the Yocto and other workflows
use crate::*;
use crate::{config::commands::WorkflowKind, err_parse::yocto::util::YoctoFailureKind};

use self::yocto::YoctoError;

/// Maximum size of a logfile we'll add to the issue body
///
/// The maximum size of a GitHub issue body is 65536
pub const LOGFILE_MAX_LEN: usize = 5000;

pub mod yocto;

#[derive(Debug)]
pub enum ErrorMessageSummary {
    Yocto(YoctoError),
    Other(String),
}

impl ErrorMessageSummary {
    pub fn summary(&self) -> &str {
        match self {
            ErrorMessageSummary::Yocto(err) => err.summary(),
            ErrorMessageSummary::Other(o) => o.as_str(),
        }
    }
    pub fn log(&self) -> Option<&str> {
        match self {
            ErrorMessageSummary::Yocto(err) => err.logfile().map(|log| log.contents.as_str()),
            ErrorMessageSummary::Other(_) => None, // Does not come with a log file
        }
    }
    pub fn logfile_name(&self) -> Option<&str> {
        match self {
            ErrorMessageSummary::Yocto(err) => err.logfile().map(|log| log.name.as_str()),
            ErrorMessageSummary::Other(_) => None, // Does not come with a log file
        }
    }

    pub fn failure_label(&self) -> Option<String> {
        match self {
            ErrorMessageSummary::Yocto(err) => Some(err.kind().to_string()),
            ErrorMessageSummary::Other(_) => None,
        }
    }
}

pub fn parse_error_message(
    err_msg: &str,
    workflow: WorkflowKind,
) -> anyhow::Result<ErrorMessageSummary> {
    let err_msg = if Config::global().trim_timestamp() {
        log::info!("Trimming timestamps from the log error message");
        remove_timestamp_prefixes(err_msg)
    } else {
        err_msg.into()
    };
    let err_msg = if Config::global().trim_ansi_codes() {
        log::info!("Trimming ansi codes from the log error message");
        remove_ansi_codes(&err_msg)
    } else {
        err_msg
    };
    let err_msg = err_msg.to_string();

    let err_msg = match workflow {
        WorkflowKind::Yocto => {
            ErrorMessageSummary::Yocto(yocto::parse_yocto_error(&err_msg).unwrap_or_else(|e| {
                log::warn!("Failed to parse Yocto error, returning error message as is: {e}");
                YoctoError::new(err_msg, YoctoFailureKind::default(), None)
            }))
        }
        WorkflowKind::Other => ErrorMessageSummary::Other(err_msg.to_string()),
    };
    Ok(err_msg)
}