|  | 
| 1 | 1 | // Licensed to the .NET Foundation under one or more agreements. | 
| 2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. | 
| 3 | 3 | 
 | 
| 4 |  | -#nullable disable | 
|  | 4 | +using System; | 
|  | 5 | +using System.Linq; | 
| 5 | 6 | 
 | 
| 6 | 7 | namespace Microsoft.DotNet.Cli.Telemetry; | 
| 7 | 8 | 
 | 
| 8 | 9 | internal class CIEnvironmentDetectorForTelemetry : ICIEnvironmentDetector | 
| 9 | 10 | { | 
| 10 |  | -    // Systems that provide boolean values only, so we can simply parse and check for true | 
| 11 |  | -    private static readonly string[] _booleanVariables = [ | 
| 12 |  | -        // Azure Pipelines - https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables#system-variables-devops-services | 
| 13 |  | -        "TF_BUILD", | 
| 14 |  | -        // GitHub Actions - https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables | 
| 15 |  | -        "GITHUB_ACTIONS", | 
| 16 |  | -        // AppVeyor - https://www.appveyor.com/docs/environment-variables/ | 
| 17 |  | -        "APPVEYOR", | 
| 18 |  | -        // A general-use flag - Many of the major players support this: AzDo, GitHub, GitLab, AppVeyor, Travis CI, CircleCI. | 
| 19 |  | -        // Given this, we could potentially remove all of these other options? | 
| 20 |  | -        "CI", | 
| 21 |  | -        // Travis CI - https://docs.travis-ci.com/user/environment-variables/#default-environment-variables | 
| 22 |  | -        "TRAVIS", | 
| 23 |  | -        // CircleCI - https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables | 
| 24 |  | -        "CIRCLECI", | 
| 25 |  | -    ]; | 
| 26 |  | - | 
| 27 |  | -    // Systems where every variable must be present and not-null before returning true | 
| 28 |  | -    private static readonly string[][] _allNotNullVariables = [ | 
|  | 11 | +    private static readonly EnvironmentDetectionRule[] _detectionRules = [ | 
|  | 12 | +        // Systems that provide boolean values only, so we can simply parse and check for true | 
|  | 13 | +        new BooleanEnvironmentRule( | 
|  | 14 | +            // Azure Pipelines - https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables#system-variables-devops-services | 
|  | 15 | +            "TF_BUILD", | 
|  | 16 | +            // GitHub Actions - https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables | 
|  | 17 | +            "GITHUB_ACTIONS", | 
|  | 18 | +            // AppVeyor - https://www.appveyor.com/docs/environment-variables/ | 
|  | 19 | +            "APPVEYOR", | 
|  | 20 | +            // A general-use flag - Many of the major players support this: AzDo, GitHub, GitLab, AppVeyor, Travis CI, CircleCI. | 
|  | 21 | +            // Given this, we could potentially remove all of these other options? | 
|  | 22 | +            "CI", | 
|  | 23 | +            // Travis CI - https://docs.travis-ci.com/user/environment-variables/#default-environment-variables | 
|  | 24 | +            "TRAVIS", | 
|  | 25 | +            // CircleCI - https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables | 
|  | 26 | +            "CIRCLECI" | 
|  | 27 | +        ), | 
|  | 28 | + | 
|  | 29 | +        // Systems where every variable must be present and not-null before returning true | 
| 29 | 30 |         // AWS CodeBuild - https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html | 
| 30 |  | -        ["CODEBUILD_BUILD_ID", "AWS_REGION"], | 
|  | 31 | +        new AllPresentEnvironmentRule("CODEBUILD_BUILD_ID", "AWS_REGION"), | 
| 31 | 32 |         // Jenkins - https://github.com/jenkinsci/jenkins/blob/master/core/src/main/resources/jenkins/model/CoreEnvironmentContributor/buildEnv.groovy | 
| 32 |  | -        ["BUILD_ID", "BUILD_URL"], | 
|  | 33 | +        new AllPresentEnvironmentRule("BUILD_ID", "BUILD_URL"), | 
| 33 | 34 |         // Google Cloud Build - https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values#using_default_substitutions | 
| 34 |  | -        ["BUILD_ID", "PROJECT_ID"] | 
| 35 |  | -    ]; | 
| 36 |  | - | 
| 37 |  | -    // Systems where the variable must be present and not-null | 
| 38 |  | -    private static readonly string[] _ifNonNullVariables = [ | 
| 39 |  | -        // TeamCity - https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Predefined+Server+Build+Parameters | 
| 40 |  | -        "TEAMCITY_VERSION", | 
| 41 |  | -        // JetBrains Space - https://www.jetbrains.com/help/space/automation-environment-variables.html#general | 
| 42 |  | -        "JB_SPACE_API_URL" | 
|  | 35 | +        new AllPresentEnvironmentRule("BUILD_ID", "PROJECT_ID"), | 
|  | 36 | + | 
|  | 37 | +        // Systems where the variable must be present and not-null | 
|  | 38 | +        new AnyPresentEnvironmentRule( | 
|  | 39 | +            // TeamCity - https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Predefined+Server+Build+Parameters | 
|  | 40 | +            "TEAMCITY_VERSION", | 
|  | 41 | +            // JetBrains Space - https://www.jetbrains.com/help/space/automation-environment-variables.html#general | 
|  | 42 | +            "JB_SPACE_API_URL" | 
|  | 43 | +        ) | 
| 43 | 44 |     ]; | 
| 44 | 45 | 
 | 
| 45 | 46 |     public bool IsCIEnvironment() | 
| 46 | 47 |     { | 
| 47 |  | -        foreach (var booleanVariable in _booleanVariables) | 
| 48 |  | -        { | 
| 49 |  | -            if (bool.TryParse(Environment.GetEnvironmentVariable(booleanVariable), out bool envVar) && envVar) | 
| 50 |  | -            { | 
| 51 |  | -                return true; | 
| 52 |  | -            } | 
| 53 |  | -        } | 
| 54 |  | - | 
| 55 |  | -        foreach (var variables in _allNotNullVariables) | 
| 56 |  | -        { | 
| 57 |  | -            if (variables.All((variable) => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable)))) | 
| 58 |  | -            { | 
| 59 |  | -                return true; | 
| 60 |  | -            } | 
| 61 |  | -        } | 
| 62 |  | - | 
| 63 |  | -        foreach (var variable in _ifNonNullVariables) | 
| 64 |  | -        { | 
| 65 |  | -            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable))) | 
| 66 |  | -            { | 
| 67 |  | -                return true; | 
| 68 |  | -            } | 
| 69 |  | -        } | 
| 70 |  | - | 
| 71 |  | -        return false; | 
|  | 48 | +        return _detectionRules.Any(rule => rule.IsMatch()); | 
| 72 | 49 |     } | 
| 73 | 50 | } | 
0 commit comments