ci_detective/
codefresh.rs

1use env;
2use std::path::PathBuf;
3use std::str::FromStr;
4
5/// Codefresh CI
6///
7/// # References
8///
9/// - <https://github.com/codecov/codecov-bash/blob/8b76995ad4a95a61cecd4b049a448a402d91d197/codecov#L511-L520>
10#[derive(Clone, Debug)]
11#[cfg_attr(feature = "nightly", non_exhaustive)]
12pub struct Codefresh {
13    /// Repository owner.
14    pub repo_owner: String,
15    /// Repository name.
16    pub repo_name: String,
17    /// Branch name of the Git repository of the main pipeline, at the time of execution.
18    pub branch: String,
19    /// Commit author.
20    pub commit_author: String,
21    /// Commit url.
22    pub commit_url: String,
23    /// Commit message of the git repository revision, at the time of execution.
24    pub commit_message: String,
25    /// Revision of the Git repository of the main pipeline, at the time of execution.
26    pub revision: String,
27    /// Abbreviated 7-character revision hash, as used in git.
28    pub short_revision: String,
29    /// Will refer to the volume that was generated for the specific flow.
30    /// Can be used in conjunction with a composition to provide access to your cloned repository.
31    pub volume_name: String,
32    /// Will refer to the mounted path of the workflow volume inside a Freestyle container.
33    pub volume_path: PathBuf,
34    /// Will be an indication of the current build was triggered.
35    pub build_trigger: BuildTrigger,
36    /// The build id.
37    pub build_id: String,
38    /// The timestamp the build was created.
39    pub build_timestamp: String,
40    /// The URL to the build in Codefresh
41    pub build_url: String,
42    /// Path to kubeconfig if exist
43    pub kubeconfig_path: Option<PathBuf>,
44    non_exhaustive: (),
45}
46
47impl Codefresh {
48    /// Construct this provider's information from the environment.
49    pub fn from_env() -> Option<Self> {
50        Some(Codefresh {
51            repo_owner: env("CF_REPO_OWNER")?,
52            repo_name: env("CF_REPO_NAME")?,
53            branch: env("CF_BRANCH")?,
54            commit_author: env("CF_COMMIT_AUTHOR")?,
55            commit_url: env("CF_COMMIT_URL")?,
56            commit_message: env("CF_COMMIT_MESSAGE")?,
57            revision: env("CF_REVISION")?,
58            short_revision: env("CF_SHORT_REVISION")?,
59            volume_name: env("CF_VOLUME_NAME")?,
60            volume_path: PathBuf::from(env("CF_VOLUME_PATH")?),
61            build_trigger: env("CF_BUILD_TRIGGER")?.parse().ok()?,
62            build_id: env("CF_BUILD_ID")?,
63            build_timestamp: env("CF_BUILD_TIMESTAMP")?,
64            build_url: env("CF_BUILD_URL")?,
65            kubeconfig_path: env("CF_KUBECONFIG_PATH").map(PathBuf::from),
66            non_exhaustive: (),
67        })
68    }
69}
70
71/// How the current build was triggered.
72#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
73#[cfg_attr(feature = "nightly", non_exhaustive)]
74pub enum BuildTrigger {
75    /// The build was triggered from the build button.
76    Build,
77    /// The build was triggered from a control version webhook.
78    Webhook,
79    #[doc(hidden)] __NonExhaustive,
80}
81
82impl FromStr for BuildTrigger {
83    type Err = ();
84    fn from_str(s: &str) -> Result<Self, Self::Err> {
85        match s {
86            "build" => Ok(BuildTrigger::Build),
87            "webhook" => Ok(BuildTrigger::Webhook),
88            _ => Err(()),
89        }
90    }
91}