ci_manager/
ci_provider.rs

1use self::commands::locate_failure_log;
2
3use super::*;
4
5pub mod github;
6pub mod gitlab;
7pub mod util;
8
9// Which CI provider is being used, determined from the environment.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumString, Display, ValueEnum)]
11pub enum CIProvider {
12    #[value(name = "GitHub", alias = "github")]
13    GitHub,
14    #[value(name = "GitLab", alias = "gitlab")]
15    GitLab,
16}
17
18impl CIProvider {
19    fn env_is_github() -> bool {
20        // Check if the GITHUB_ENV environment variable is set
21        // might be a more appropriate one to check.. Or check several?
22        // The dilemma is that it should return ok on GitHub runners, self-hosted or not
23        // but also not trigger false positives by checking a variable some projects might set
24        env::var("GITHUB_ENV").is_ok()
25    }
26    fn env_is_gitlab() -> bool {
27        env::var("GITLAB_CI").is_ok()
28    }
29
30    pub fn from_enviroment() -> Result<Self> {
31        if Self::env_is_gitlab() {
32            Ok(Self::GitLab)
33        } else if Self::env_is_github() {
34            Ok(Self::GitHub)
35        } else {
36            bail!("Could not determine CI provider from environment")
37        }
38    }
39
40    pub async fn handle(&self, command: &commands::Command) -> Result<()> {
41        use commands::Command;
42        match command {
43            // This is a command that is not specific to a CI provider
44            Command::LocateFailureLog { kind, input_file } => {
45                locate_failure_log::locate_failure_log(*kind, input_file.as_ref())
46            }
47            Command::CreateIssueFromRun {
48                repo,
49                run_id,
50                label,
51                kind,
52                title,
53                no_duplicate,
54            } => match self {
55                Self::GitHub => {
56                    github::GitHub::get()
57                        .create_issue_from_run(repo, run_id, label, kind, *no_duplicate, title)
58                        .await
59                }
60                Self::GitLab => gitlab::GitLab::get().handle(command),
61            },
62        }
63    }
64}