ci_detective/
travis.rs

1use env;
2use std::path::PathBuf;
3use std::str::FromStr;
4
5/// Travis CI
6///
7/// # References
8///
9/// - <https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables>
10/// - <https://github.com/codecov/codecov-bash/blob/8b76995ad4a95a61cecd4b049a448a402d91d197/codecov#L467-L489>
11#[derive(Clone, Debug)]
12#[cfg_attr(feature = "nightly", non_exhaustive)]
13pub struct Travis {
14    /// - set to `true` if the job is allowed to fail.
15    /// - set to `false` if the job is not allowed to fail.
16    pub allow_failure: bool,
17    /// - for push builds, or builds not triggered by a pull request,
18    ///   this is the name of the branch.
19    /// - for builds triggered by a pull request
20    ///   this is the name of the branch targeted by the pull request.
21    /// - for builds triggered by a tag,
22    ///   this is the same as the name of the tag (`TRAVIS_TAG`).
23    ///
24    /// > _Note that for tags, git does not store the branch from which a commit was tagged._
25    pub branch: String,
26    /// The absolute path to the directory where the repository
27    /// being built has been copied on the worker.
28    pub build_dir: PathBuf,
29    /// The id of the current build that Travis CI uses internally.
30    pub build_id: String,
31    /// The number of the current build (for example, `4`).
32    pub build_number: usize,
33    /// The commit that the current build is testing.
34    pub commit: String,
35    /// The commit subject and body, unwrapped.
36    pub commit_message: String,
37    /// The range of commits that were included in the push or pull request.
38    /// (Note that this is empty for builds triggered by the initial commit of a new branch.)
39    pub commit_range: Option<String>,
40    /// Indicates how the build was triggered.
41    pub event_type: EventType,
42    /// The id of the current job that Travis CI uses internally.
43    pub job_id: String,
44    /// The number of the current job (for example, `4.1`).
45    pub job_number: String,
46    /// On multi-OS builds, this value indicates the platform the job is running on.
47    pub os: Option<OS>,
48    /// The `osx_image` value configured in `.travis.yml`.
49    /// If this is not set in `.travis.yml`, it is emtpy.
50    pub osx_image: Option<String>,
51    /// The pull request number if the current job is a pull request.
52    pub pull_request: Option<String>,
53    /// If the current job is a pull request, the name of the branch from which the PR originated.
54    pub pull_request_branch: Option<String>,
55    /// if the current job is a pull request, the commit SHA of the HEAD commit of the PR.
56    pub pull_request_sha: Option<String>,
57    /// if the current job is a pull request, the slug (in the form `owner_name/repo_name`)
58    /// of the repository from which the PR originated.
59    pub pull_request_slug: Option<String>,
60    /// The slug (in form: `owner_name/repo_name`) of the repository currently being built.
61    pub repo_slug: String,
62    /// - set to `true` if there are any encrypted environment variables.
63    /// - set to `false` if no encrypted environment variables are available.
64    pub secure_env_vars: bool,
65    /// `true` or `false` based on whether `sudo` is enabled.
66    pub sudo: bool,
67    /// If the current build is for a git tag, this variable is set to the tag’s name.
68    pub tag: Option<String>,
69    /// The current version of dart being used to run the build (if any).
70    pub dart_version: Option<String>,
71    /// The current version of go being used to run the build (if any).
72    pub go_version: Option<String>,
73    /// The current version of haxe being used to run the build (if any).
74    pub haxe_version: Option<String>,
75    /// The current version of jdk being used to run the build (if any).
76    pub jdk_version: Option<String>,
77    /// The current version of julia being used to run the build (if any).
78    pub julia_version: Option<String>,
79    /// The current version of node being used to run the build (if any).
80    pub node_version: Option<String>,
81    /// The current version of otp being used to run the build (if any).
82    pub otp_release: Option<String>,
83    /// The current version of perl being used to run the build (if any).
84    pub perl_version: Option<String>,
85    /// The current version of php being used to run the build (if any).
86    pub php_version: Option<String>,
87    /// The current version of python being used to run the build (if any).
88    pub python_version: Option<String>,
89    /// The current version of r being used to run the build (if any).
90    pub r_version: Option<String>,
91    /// The current version of ruby being used to run the build (if any).
92    pub ruby_version: Option<String>,
93    /// The current version of rust being used to run the build (if any).
94    pub rust_version: Option<String>,
95    /// The current version of scala being used to run the build (if any).
96    pub scala_version: Option<String>,
97    /// The current XCode SDK being used to run the build (if any).
98    pub xcode_sdk: Option<String>,
99    /// The current XCode Scheme being used to run the build (if any).
100    pub xcode_scheme: Option<String>,
101    /// The current XCode Project being used to run the build (if any).
102    pub xcode_project: Option<String>,
103    /// The current XCode Workspace being used to run the build (if any).
104    pub xcode_workspace: Option<String>,
105    non_exhaustive: (),
106}
107
108impl Travis {
109    /// Construct this provider's information from the environment.
110    pub fn from_env() -> Option<Self> {
111        if !(env("CI")? == "true" && env("TRAVIS")? == "true"
112            && env("CONTINUOUS_INTEGRATION")? == "true"
113            && env("DEBIAN_FRONTEND")? == "noninteractive"
114            && env("HAS_JOSH_K_SEAL_OF_APPROVAL")? == "true")
115        {
116            return None;
117        }
118
119        Some(Travis {
120            allow_failure: env("TRAVIS_ALLOW_FAILURE")?.parse().ok()?,
121            branch: env("TRAVIS_BRANCH")?,
122            build_dir: env("TRAVIS_BUILD_DIR")?.into(),
123            build_id: env("TRAVIS_BUILD_ID")?,
124            build_number: env("TRAVIS_BUILD_NUMBER")?.parse().ok()?,
125            commit: env("TRAVIS_COMMIT")?,
126            commit_message: env("TRAVIS_COMMIT_MESSAGE")?,
127            commit_range: env("TRAVIS_COMMIT_RANGE"),
128            event_type: env("TRAVIS_EVENT_TYPE")?.parse().ok()?,
129            job_id: env("TRAVIS_JOB_ID")?,
130            job_number: env("TRAVIS_JOB_NUMBER")?,
131            os: env("TRAVIS_OS_NAME").and_then(|it| it.parse().ok()),
132            osx_image: env("TRAVIS_OSX_IMAGE"),
133            pull_request: if let Some(pr) = env("TRAVIS_PULL_REQUEST") {
134                if pr != "false" {
135                    Some(pr)
136                } else {
137                    None
138                }
139            } else {
140                None
141            },
142            pull_request_branch: env("TRAVIS_PULL_REQUEST_BRANCH"),
143            pull_request_sha: env("TRAVIS_PULL_REQUEST_SHA"),
144            pull_request_slug: env("TRAVIS_PULL_REQUEST_SLUG"),
145            repo_slug: env("TRAVIS_REPO_SLUG")?,
146            secure_env_vars: env("TRAVIS_SECURE_ENV_VARS")?.parse().ok()?,
147            sudo: env("TRAVIS_SUDO")?.parse().ok()?,
148            tag: env("TRAVIS_TAG"),
149            dart_version: env("TRAVIS_DART_VERSION"),
150            go_version: env("TRAVIS_GO_VERSION"),
151            haxe_version: env("TRAVIS_HAXE_VERSION"),
152            jdk_version: env("TRAVIS_JDK_VESRION"),
153            julia_version: env("TRAVIS_JULIA_VERSION"),
154            node_version: env("TRAVIS_NODE_VERSION"),
155            otp_release: env("TRAVIS_OTP_RELEASE"),
156            perl_version: env("TRAVIS_PERL_VERSION"),
157            php_version: env("TRAVIS_PHP_VERSION"),
158            python_version: env("TRAVIS_PYTHON_VERSION"),
159            r_version: env("TRAVIS_R_VERSION"),
160            ruby_version: env("TRAVIS_RUBY_VERSION"),
161            rust_version: env("TRAVIS_RUST_VERSION"),
162            scala_version: env("TRAVIS_SCALA_VERSION"),
163            xcode_sdk: env("TRAVIS_XCODE_SDK"),
164            xcode_scheme: env("TRAVIS_XCODE_SCHEME"),
165            xcode_project: env("TRAVIS_XCODE_PROJECT"),
166            xcode_workspace: env("TRAVIS_XCODE_WORKSPACE"),
167            non_exhaustive: (),
168        })
169    }
170}
171
172/// Indicates how the build was triggered.
173#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
174#[cfg_attr(feature = "nightly", non_exhaustive)]
175#[allow(missing_docs)]
176pub enum EventType {
177    Push,
178    PullRequest,
179    Api,
180    Cron,
181    #[doc(hidden)] __NonExhaustive,
182}
183
184impl FromStr for EventType {
185    type Err = ();
186    fn from_str(s: &str) -> Result<Self, Self::Err> {
187        match s {
188            "push" => Ok(EventType::Push),
189            "pull_request" => Ok(EventType::PullRequest),
190            "api" => Ok(EventType::Api),
191            "cron" => Ok(EventType::Cron),
192            _ => Err(()),
193        }
194    }
195}
196
197/// On multi-OS builds, this value indicates the platform the job is running on.
198/// To be extended in the future.
199#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
200#[cfg_attr(feature = "nightly", non_exhaustive)]
201#[allow(missing_docs)]
202pub enum OS {
203    Linux,
204    MacOS,
205    #[doc(hidden)] __NonExhaustive,
206}
207
208impl FromStr for OS {
209    type Err = ();
210    fn from_str(s: &str) -> Result<Self, Self::Err> {
211        match s {
212            "linux" => Ok(OS::Linux),
213            "osx" => Ok(OS::MacOS),
214            _ => Err(()),
215        }
216    }
217}