ci_info/
ci.rs

1//! # ci
2//!
3//! Loads CI environment information.
4//!
5
6#[cfg(test)]
7#[path = "./ci_test.rs"]
8mod ci_test;
9
10use crate::config;
11use crate::types::{CiInfo, EnvValue, Vendor};
12
13fn validate(env_info: &EnvValue) -> bool {
14    match env_info {
15        EnvValue::Exists(ref key) => envmnt::exists(key),
16        EnvValue::AllExists(ref keys) => envmnt::is_all_exists(keys),
17        EnvValue::AnyExists(ref keys) => envmnt::is_any_exists(keys),
18        EnvValue::Value(ref key, ref value) => envmnt::is_equal(key, value),
19        EnvValue::NotEqual(ref key, ref value) => !envmnt::is_equal(key, value),
20        EnvValue::Contains(ref key, ref value) => envmnt::contains_ignore_case(key, value),
21        EnvValue::NotEmpty(ref key) => {
22            let value = envmnt::get_or(key, "");
23            !value.is_empty()
24        }
25    }
26}
27
28/// Loads and returns the CI info of the current environment.
29pub(crate) fn get() -> CiInfo {
30    let mut info = CiInfo::new();
31    let vendor_config_list = config::create();
32
33    for vendor_config in vendor_config_list.iter() {
34        let found = validate(&vendor_config.ci_env);
35
36        if found {
37            info.ci = true;
38            info.vendor = match vendor_config.vendor {
39                Vendor::Unknown => None,
40                _ => Some(vendor_config.vendor),
41            };
42            info.name = match vendor_config.vendor {
43                Vendor::Unknown => None,
44                _ => Some(vendor_config.name.clone()),
45            };
46
47            info.pr = match vendor_config.pr_env {
48                Some(ref env_value) => {
49                    let is_pr = validate(env_value);
50
51                    Some(is_pr)
52                }
53                None => None,
54            };
55
56            info.branch_name = match vendor_config.branch_name_env {
57                Some(ref env_key) => {
58                    let value = envmnt::get_or(env_key, "");
59
60                    if !value.is_empty() {
61                        Some(value.to_string())
62                    } else {
63                        None
64                    }
65                }
66                None => None,
67            };
68
69            break;
70        }
71    }
72
73    info
74}
75
76/// Returns true if a CI environment is detected.
77pub(crate) fn is_ci() -> bool {
78    let info = get();
79
80    info.ci
81}