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
use self::commands::locate_failure_log;

use super::*;

pub mod github;
pub mod gitlab;
pub mod util;

// Which CI provider is being used, determined from the environment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumString, Display, ValueEnum)]
pub enum CIProvider {
    #[value(name = "GitHub", alias = "github")]
    GitHub,
    #[value(name = "GitLab", alias = "gitlab")]
    GitLab,
}

impl CIProvider {
    fn env_is_github() -> bool {
        // Check if the GITHUB_ENV environment variable is set
        // might be a more appropriate one to check.. Or check several?
        // The dilemma is that it should return ok on GitHub runners, self-hosted or not
        // but also not trigger false positives by checking a variable some projects might set
        env::var("GITHUB_ENV").is_ok()
    }
    fn env_is_gitlab() -> bool {
        env::var("GITLAB_CI").is_ok()
    }

    pub fn from_enviroment() -> Result<Self> {
        if Self::env_is_gitlab() {
            Ok(Self::GitLab)
        } else if Self::env_is_github() {
            Ok(Self::GitHub)
        } else {
            bail!("Could not determine CI provider from environment")
        }
    }

    pub async fn handle(&self, command: &commands::Command) -> Result<()> {
        use commands::Command;
        match command {
            // This is a command that is not specific to a CI provider
            Command::LocateFailureLog { kind, input_file } => {
                locate_failure_log::locate_failure_log(*kind, input_file.as_ref())
            }
            Command::CreateIssueFromRun {
                repo,
                run_id,
                label,
                kind,
                title,
                no_duplicate,
            } => match self {
                Self::GitHub => {
                    github::GitHub::get()
                        .create_issue_from_run(repo, run_id, label, kind, *no_duplicate, title)
                        .await
                }
                Self::GitLab => gitlab::GitLab::get().handle(command),
            },
        }
    }
}