From 195b4eacce781104bd97abc0d3e951f325a2d766 Mon Sep 17 00:00:00 2001 From: monalisa Date: Fri, 20 Oct 2023 01:08:09 +0200 Subject: [PATCH 01/10] Add telemetry for CI/CD platform to useragent --- client/client.go | 14 ++++++++++++++ useragent/cicd.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 useragent/cicd.go diff --git a/client/client.go b/client/client.go index 81f7a25c4..52744ba02 100644 --- a/client/client.go +++ b/client/client.go @@ -389,6 +389,19 @@ func (c *DatabricksClient) addAuthHeaderToUserAgent(r *http.Request) error { return nil } +func (c *DatabricksClient) addCiCdProviderToUserAgent(r *http.Request) error { + // Detect if we are running in a CI/CD environment + provider := useragent.CiCdProvider() + if provider == "" { + return nil + } + + // Add the detected CI/CD provider to the user agent + ctx := useragent.InContext(r.Context(), "cicd", provider) + *r = *r.WithContext(ctx) // replace request + return nil +} + func (c *DatabricksClient) perform( ctx context.Context, method, @@ -407,6 +420,7 @@ func (c *DatabricksClient) perform( c.Config.Authenticate, c.addHostToRequestUrl, c.addAuthHeaderToUserAgent, + c.addCiCdProviderToUserAgent, }, visitors...) resp, err := retries.Poll(ctx, c.retryTimeout, c.attempt(ctx, method, requestURL, headers, requestBody, visitors...)) diff --git a/useragent/cicd.go b/useragent/cicd.go new file mode 100644 index 000000000..e463f338d --- /dev/null +++ b/useragent/cicd.go @@ -0,0 +1,49 @@ +package useragent + +import "os" + +type envVar struct { + name string + expectedValue string +} + +type cicdProvider struct { + name string + envVars []envVar +} + +// TODO: add bamboo to this? +var cicdProviders = []cicdProvider{ + {"github", []envVar{{"GITHUB_ACTIONS", "true"}}}, + {"gitlab", []envVar{{"GITLAB_CI", "true"}}}, + {"jenkins", []envVar{{"JENKINS_URL", ""}}}, + {"azure-devops", []envVar{{"TF_BUILD", "True"}}}, + {"circle", []envVar{{"CIRCLECI", "true"}}}, + {"travis", []envVar{{"TRAVIS", "true"}}}, + {"bitbucket", []envVar{{"BITBUCKET_BUILD_NUMBER", ""}}}, + {"google-cloud-build", []envVar{{"PROJECT_ID", ""}, {"BUILD_ID", ""}, {"PROJECT_NUMBER", ""}, {"LOCATION", ""}}}, + {"aws-code-build", []envVar{{"CODEBUILD_BUILD_ARN", ""}}}, + {"tf-cloud", []envVar{{"TFC_RUN_ID", ""}}}, +} + +func (p cicdProvider) detect() bool { + for _, envVar := range p.envVars { + v, ok := os.LookupEnv(envVar.name) + if !ok { + return false + } + if envVar.expectedValue != "" && v != envVar.expectedValue { + return false + } + } + return true +} + +func CiCdProvider() string { + for _, p := range cicdProviders { + if p.detect() { + return p.name + } + } + return "" +} From 019475178518c8b57bead36965c9125a1475b578 Mon Sep 17 00:00:00 2001 From: monalisa Date: Fri, 20 Oct 2023 16:10:59 +0200 Subject: [PATCH 02/10] add tests and comments --- useragent/cicd.go | 10 ++++- useragent/cicd_test.go | 100 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 useragent/cicd_test.go diff --git a/useragent/cicd.go b/useragent/cicd.go index e463f338d..dda0da1f9 100644 --- a/useragent/cicd.go +++ b/useragent/cicd.go @@ -8,11 +8,16 @@ type envVar struct { } type cicdProvider struct { + // The name of the CI/CD provider. This is the name included in the user + // agent string. name string + + // The env vars that are expected to be set in the CI/CD provider's runner. envVars []envVar } -// TODO: add bamboo to this? +// cicdProviders is a list of CI/CD providers and their env vars we can rely on +// to detect them. var cicdProviders = []cicdProvider{ {"github", []envVar{{"GITHUB_ACTIONS", "true"}}}, {"gitlab", []envVar{{"GITLAB_CI", "true"}}}, @@ -26,6 +31,7 @@ var cicdProviders = []cicdProvider{ {"tf-cloud", []envVar{{"TFC_RUN_ID", ""}}}, } +// detect returns true if all env vars are set and have expected values. func (p cicdProvider) detect() bool { for _, envVar := range p.envVars { v, ok := os.LookupEnv(envVar.name) @@ -39,6 +45,8 @@ func (p cicdProvider) detect() bool { return true } +// CiCdProvider returns the name of the CI/CD provider if detected. Returns the +// first one, if multiple are detected. func CiCdProvider() string { for _, p := range cicdProviders { if p.detect() { diff --git a/useragent/cicd_test.go b/useragent/cicd_test.go new file mode 100644 index 000000000..d3b8eece2 --- /dev/null +++ b/useragent/cicd_test.go @@ -0,0 +1,100 @@ +package useragent + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCiCdProviderDetect(t *testing.T) { + cicdProvider := cicdProvider{ + name: "foo", + envVars: []envVar{ + {"APPLE", "123"}, + {"BANANA", "456"}, + {"CHERRY", ""}, + }, + } + + // Set some of the env vars. + t.Setenv("APPLE", "123") + t.Setenv("CHERRY", "000") + assert.False(t, cicdProvider.detect(), "should not detect when not all env vars are set") + + // Set the rest of the env vars. + t.Setenv("BANANA", "456") + assert.True(t, cicdProvider.detect(), "should detect when all env vars are set") +} + +func TestCiCdProviderGithubActions(t *testing.T) { + // No provider detected. + assert.Equal(t, "", CiCdProvider()) + + // Github Actions detected. + t.Setenv("GITHUB_ACTIONS", "true") + assert.Equal(t, "github", CiCdProvider()) +} + +func TestCiCdProviderGitlab(t *testing.T) { + // Gitlab detected. + t.Setenv("GITLAB_CI", "true") + assert.Equal(t, "gitlab", CiCdProvider()) +} + +func TestCiCdProviderJenkins(t *testing.T) { + // Jenkins detected. + t.Setenv("JENKINS_URL", "https://jenkins.example.com") + assert.Equal(t, "jenkins", CiCdProvider()) +} + +func TestCiCdProviderAzureDevops(t *testing.T) { + // Azure Devops detected. + t.Setenv("TF_BUILD", "True") + assert.Equal(t, "azure-devops", CiCdProvider()) +} + +func TestCiCdProviderCircle(t *testing.T) { + // Circle detected. + t.Setenv("CIRCLECI", "true") + assert.Equal(t, "circle", CiCdProvider()) +} + +func TestCiCdProviderTravis(t *testing.T) { + // Travis detected. + t.Setenv("TRAVIS", "true") + assert.Equal(t, "travis", CiCdProvider()) +} + +func TestCiCdProviderBitbucket(t *testing.T) { + // Bitbucket detected. + t.Setenv("BITBUCKET_BUILD_NUMBER", "123") + assert.Equal(t, "bitbucket", CiCdProvider()) +} + +func TestCiCdProviderGoogleCloudBuild(t *testing.T) { + // Google Cloud Build detected. + t.Setenv("PROJECT_ID", "foo") + t.Setenv("BUILD_ID", "bar") + t.Setenv("PROJECT_NUMBER", "baz") + t.Setenv("LOCATION", "") + assert.Equal(t, "google-cloud-build", CiCdProvider()) +} + +func TestCiCdProviderAwsCodeBuild(t *testing.T) { + // AWS Code Build detected. + t.Setenv("CODEBUILD_BUILD_ARN", "arn:aws:codebuild:us-east-1:123456789012:build/my-demo-project:b1e6deae-e4f2-4151-be79-3cc4e82a0bf0") + assert.Equal(t, "aws-code-build", CiCdProvider()) +} + +func TestCiCdProviderTfCloud(t *testing.T) { + // Terraform Cloud detected. + t.Setenv("TFC_RUN_ID", "run-123") + assert.Equal(t, "tf-cloud", CiCdProvider()) +} + +func TestCiCdProviderMultiple(t *testing.T) { + // Multiple providers detected. The first one detected is set. + t.Setenv("GITHUB_ACTIONS", "true") + t.Setenv("GITLAB_CI", "true") + assert.Equal(t, "github", CiCdProvider()) +} From d2a15066ae0e6259af87e741ac8d80cc293e79c8 Mon Sep 17 00:00:00 2001 From: monalisa Date: Fri, 20 Oct 2023 16:29:28 +0200 Subject: [PATCH 03/10] add clear env --- useragent/cicd_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/useragent/cicd_test.go b/useragent/cicd_test.go index d3b8eece2..5eb275c64 100644 --- a/useragent/cicd_test.go +++ b/useragent/cicd_test.go @@ -1,6 +1,8 @@ package useragent import ( + "os" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -26,7 +28,27 @@ func TestCiCdProviderDetect(t *testing.T) { assert.True(t, cicdProvider.detect(), "should detect when all env vars are set") } +func cleanupEnv(t *testing.T) { + // We use set dummy env here to ensure this function is not called from a + // test that runs in parallel. + t.Setenv("FOO_BAR", "") + + environ := os.Environ() + t.Cleanup(func() { + // Restore original environment once test is done. + for _, kv := range environ { + kvs := strings.SplitN(kv, "=", 2) + os.Setenv(kvs[0], kvs[1]) + } + }) + + // Clear out all existing environment variables. + os.Clearenv() +} + func TestCiCdProviderGithubActions(t *testing.T) { + cleanupEnv(t) + // No provider detected. assert.Equal(t, "", CiCdProvider()) @@ -36,42 +58,56 @@ func TestCiCdProviderGithubActions(t *testing.T) { } func TestCiCdProviderGitlab(t *testing.T) { + cleanupEnv(t) + // Gitlab detected. t.Setenv("GITLAB_CI", "true") assert.Equal(t, "gitlab", CiCdProvider()) } func TestCiCdProviderJenkins(t *testing.T) { + cleanupEnv(t) + // Jenkins detected. t.Setenv("JENKINS_URL", "https://jenkins.example.com") assert.Equal(t, "jenkins", CiCdProvider()) } func TestCiCdProviderAzureDevops(t *testing.T) { + cleanupEnv(t) + // Azure Devops detected. t.Setenv("TF_BUILD", "True") assert.Equal(t, "azure-devops", CiCdProvider()) } func TestCiCdProviderCircle(t *testing.T) { + cleanupEnv(t) + // Circle detected. t.Setenv("CIRCLECI", "true") assert.Equal(t, "circle", CiCdProvider()) } func TestCiCdProviderTravis(t *testing.T) { + cleanupEnv(t) + // Travis detected. t.Setenv("TRAVIS", "true") assert.Equal(t, "travis", CiCdProvider()) } func TestCiCdProviderBitbucket(t *testing.T) { + cleanupEnv(t) + // Bitbucket detected. t.Setenv("BITBUCKET_BUILD_NUMBER", "123") assert.Equal(t, "bitbucket", CiCdProvider()) } func TestCiCdProviderGoogleCloudBuild(t *testing.T) { + cleanupEnv(t) + // Google Cloud Build detected. t.Setenv("PROJECT_ID", "foo") t.Setenv("BUILD_ID", "bar") @@ -81,18 +117,24 @@ func TestCiCdProviderGoogleCloudBuild(t *testing.T) { } func TestCiCdProviderAwsCodeBuild(t *testing.T) { + cleanupEnv(t) + // AWS Code Build detected. t.Setenv("CODEBUILD_BUILD_ARN", "arn:aws:codebuild:us-east-1:123456789012:build/my-demo-project:b1e6deae-e4f2-4151-be79-3cc4e82a0bf0") assert.Equal(t, "aws-code-build", CiCdProvider()) } func TestCiCdProviderTfCloud(t *testing.T) { + cleanupEnv(t) + // Terraform Cloud detected. t.Setenv("TFC_RUN_ID", "run-123") assert.Equal(t, "tf-cloud", CiCdProvider()) } func TestCiCdProviderMultiple(t *testing.T) { + cleanupEnv(t) + // Multiple providers detected. The first one detected is set. t.Setenv("GITHUB_ACTIONS", "true") t.Setenv("GITLAB_CI", "true") From 02a5be1120dd46a7b2a4f6c9c513f5472ba5f6ec Mon Sep 17 00:00:00 2001 From: monalisa Date: Fri, 20 Oct 2023 16:30:36 +0200 Subject: [PATCH 04/10] fmt --- useragent/cicd.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/useragent/cicd.go b/useragent/cicd.go index dda0da1f9..0507418e2 100644 --- a/useragent/cicd.go +++ b/useragent/cicd.go @@ -8,9 +8,9 @@ type envVar struct { } type cicdProvider struct { - // The name of the CI/CD provider. This is the name included in the user + // The name of the CI/CD provider. This is the name included in the user // agent string. - name string + name string // The env vars that are expected to be set in the CI/CD provider's runner. envVars []envVar From 96f6f19603787d10dcd71dcb61904d3c914d5ec9 Mon Sep 17 00:00:00 2001 From: monalisa Date: Fri, 20 Oct 2023 16:33:02 +0200 Subject: [PATCH 05/10] - --- useragent/cicd_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/useragent/cicd_test.go b/useragent/cicd_test.go index 5eb275c64..c019690d2 100644 --- a/useragent/cicd_test.go +++ b/useragent/cicd_test.go @@ -30,7 +30,8 @@ func TestCiCdProviderDetect(t *testing.T) { func cleanupEnv(t *testing.T) { // We use set dummy env here to ensure this function is not called from a - // test that runs in parallel. + // test that runs in parallel. The t.Setenv() calls below will fail if this + // function is called from a parallel test. t.Setenv("FOO_BAR", "") environ := os.Environ() From d8bd75de93f07310f09257f93659a4396ad4c575 Mon Sep 17 00:00:00 2001 From: monalisa Date: Mon, 23 Oct 2023 13:50:00 +0200 Subject: [PATCH 06/10] add function for env detect fixtureS --- useragent/cicd.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/useragent/cicd.go b/useragent/cicd.go index 0507418e2..be8f6a458 100644 --- a/useragent/cicd.go +++ b/useragent/cicd.go @@ -16,19 +16,20 @@ type cicdProvider struct { envVars []envVar } -// cicdProviders is a list of CI/CD providers and their env vars we can rely on -// to detect them. -var cicdProviders = []cicdProvider{ - {"github", []envVar{{"GITHUB_ACTIONS", "true"}}}, - {"gitlab", []envVar{{"GITLAB_CI", "true"}}}, - {"jenkins", []envVar{{"JENKINS_URL", ""}}}, - {"azure-devops", []envVar{{"TF_BUILD", "True"}}}, - {"circle", []envVar{{"CIRCLECI", "true"}}}, - {"travis", []envVar{{"TRAVIS", "true"}}}, - {"bitbucket", []envVar{{"BITBUCKET_BUILD_NUMBER", ""}}}, - {"google-cloud-build", []envVar{{"PROJECT_ID", ""}, {"BUILD_ID", ""}, {"PROJECT_NUMBER", ""}, {"LOCATION", ""}}}, - {"aws-code-build", []envVar{{"CODEBUILD_BUILD_ARN", ""}}}, - {"tf-cloud", []envVar{{"TFC_RUN_ID", ""}}}, +// List of CI/CD providers and their env vars we can rely on to detect them. +func listCiCdProviders() []cicdProvider { + return []cicdProvider{ + {"github", []envVar{{"GITHUB_ACTIONS", "true"}}}, + {"gitlab", []envVar{{"GITLAB_CI", "true"}}}, + {"jenkins", []envVar{{"JENKINS_URL", ""}}}, + {"azure-devops", []envVar{{"TF_BUILD", "True"}}}, + {"circle", []envVar{{"CIRCLECI", "true"}}}, + {"travis", []envVar{{"TRAVIS", "true"}}}, + {"bitbucket", []envVar{{"BITBUCKET_BUILD_NUMBER", ""}}}, + {"google-cloud-build", []envVar{{"PROJECT_ID", ""}, {"BUILD_ID", ""}, {"PROJECT_NUMBER", ""}, {"LOCATION", ""}}}, + {"aws-code-build", []envVar{{"CODEBUILD_BUILD_ARN", ""}}}, + {"tf-cloud", []envVar{{"TFC_RUN_ID", ""}}}, + } } // detect returns true if all env vars are set and have expected values. @@ -48,7 +49,7 @@ func (p cicdProvider) detect() bool { // CiCdProvider returns the name of the CI/CD provider if detected. Returns the // first one, if multiple are detected. func CiCdProvider() string { - for _, p := range cicdProviders { + for _, p := range listCiCdProviders() { if p.detect() { return p.name } From 7aa1a81433635f1f0a3da659be654242073d7569 Mon Sep 17 00:00:00 2001 From: monalisa Date: Mon, 23 Oct 2023 13:52:49 +0200 Subject: [PATCH 07/10] use env.CleanupEnv --- useragent/cicd_test.go | 44 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/useragent/cicd_test.go b/useragent/cicd_test.go index c019690d2..3a17c7e6c 100644 --- a/useragent/cicd_test.go +++ b/useragent/cicd_test.go @@ -1,10 +1,9 @@ package useragent import ( - "os" - "strings" "testing" + "github.com/databricks/databricks-sdk-go/internal/env" "github.com/stretchr/testify/assert" ) @@ -28,27 +27,8 @@ func TestCiCdProviderDetect(t *testing.T) { assert.True(t, cicdProvider.detect(), "should detect when all env vars are set") } -func cleanupEnv(t *testing.T) { - // We use set dummy env here to ensure this function is not called from a - // test that runs in parallel. The t.Setenv() calls below will fail if this - // function is called from a parallel test. - t.Setenv("FOO_BAR", "") - - environ := os.Environ() - t.Cleanup(func() { - // Restore original environment once test is done. - for _, kv := range environ { - kvs := strings.SplitN(kv, "=", 2) - os.Setenv(kvs[0], kvs[1]) - } - }) - - // Clear out all existing environment variables. - os.Clearenv() -} - func TestCiCdProviderGithubActions(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // No provider detected. assert.Equal(t, "", CiCdProvider()) @@ -59,7 +39,7 @@ func TestCiCdProviderGithubActions(t *testing.T) { } func TestCiCdProviderGitlab(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Gitlab detected. t.Setenv("GITLAB_CI", "true") @@ -67,7 +47,7 @@ func TestCiCdProviderGitlab(t *testing.T) { } func TestCiCdProviderJenkins(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Jenkins detected. t.Setenv("JENKINS_URL", "https://jenkins.example.com") @@ -75,7 +55,7 @@ func TestCiCdProviderJenkins(t *testing.T) { } func TestCiCdProviderAzureDevops(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Azure Devops detected. t.Setenv("TF_BUILD", "True") @@ -83,7 +63,7 @@ func TestCiCdProviderAzureDevops(t *testing.T) { } func TestCiCdProviderCircle(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Circle detected. t.Setenv("CIRCLECI", "true") @@ -91,7 +71,7 @@ func TestCiCdProviderCircle(t *testing.T) { } func TestCiCdProviderTravis(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Travis detected. t.Setenv("TRAVIS", "true") @@ -99,7 +79,7 @@ func TestCiCdProviderTravis(t *testing.T) { } func TestCiCdProviderBitbucket(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Bitbucket detected. t.Setenv("BITBUCKET_BUILD_NUMBER", "123") @@ -107,7 +87,7 @@ func TestCiCdProviderBitbucket(t *testing.T) { } func TestCiCdProviderGoogleCloudBuild(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Google Cloud Build detected. t.Setenv("PROJECT_ID", "foo") @@ -118,7 +98,7 @@ func TestCiCdProviderGoogleCloudBuild(t *testing.T) { } func TestCiCdProviderAwsCodeBuild(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // AWS Code Build detected. t.Setenv("CODEBUILD_BUILD_ARN", "arn:aws:codebuild:us-east-1:123456789012:build/my-demo-project:b1e6deae-e4f2-4151-be79-3cc4e82a0bf0") @@ -126,7 +106,7 @@ func TestCiCdProviderAwsCodeBuild(t *testing.T) { } func TestCiCdProviderTfCloud(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Terraform Cloud detected. t.Setenv("TFC_RUN_ID", "run-123") @@ -134,7 +114,7 @@ func TestCiCdProviderTfCloud(t *testing.T) { } func TestCiCdProviderMultiple(t *testing.T) { - cleanupEnv(t) + env.CleanupEnvironment(t) // Multiple providers detected. The first one detected is set. t.Setenv("GITHUB_ACTIONS", "true") From 4f16a70373167f69bc4d9436a9649f20632b3b64 Mon Sep 17 00:00:00 2001 From: monalisa Date: Mon, 23 Oct 2023 14:27:13 +0200 Subject: [PATCH 08/10] lookup cicd provider once --- useragent/cicd.go | 21 ++++++++++++++++++--- useragent/cicd_test.go | 24 ++++++++++++------------ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/useragent/cicd.go b/useragent/cicd.go index be8f6a458..6d97ab90e 100644 --- a/useragent/cicd.go +++ b/useragent/cicd.go @@ -1,6 +1,9 @@ package useragent -import "os" +import ( + "os" + "sync" +) type envVar struct { name string @@ -34,6 +37,7 @@ func listCiCdProviders() []cicdProvider { // detect returns true if all env vars are set and have expected values. func (p cicdProvider) detect() bool { + os.Environ() for _, envVar := range p.envVars { v, ok := os.LookupEnv(envVar.name) if !ok { @@ -46,9 +50,9 @@ func (p cicdProvider) detect() bool { return true } -// CiCdProvider returns the name of the CI/CD provider if detected. Returns the +// lookupCiCdProvider returns the name of the CI/CD provider if detected. Returns the // first one, if multiple are detected. -func CiCdProvider() string { +func lookupCiCdProvider() string { for _, p := range listCiCdProviders() { if p.detect() { return p.name @@ -56,3 +60,14 @@ func CiCdProvider() string { } return "" } + +var provider string + +var providerOnce sync.Once + +func CiCdProvider() string { + providerOnce.Do(func() { + provider = lookupCiCdProvider() + }) + return provider +} diff --git a/useragent/cicd_test.go b/useragent/cicd_test.go index 3a17c7e6c..6c4898641 100644 --- a/useragent/cicd_test.go +++ b/useragent/cicd_test.go @@ -31,11 +31,11 @@ func TestCiCdProviderGithubActions(t *testing.T) { env.CleanupEnvironment(t) // No provider detected. - assert.Equal(t, "", CiCdProvider()) + assert.Equal(t, "", lookupCiCdProvider()) // Github Actions detected. t.Setenv("GITHUB_ACTIONS", "true") - assert.Equal(t, "github", CiCdProvider()) + assert.Equal(t, "github", lookupCiCdProvider()) } func TestCiCdProviderGitlab(t *testing.T) { @@ -43,7 +43,7 @@ func TestCiCdProviderGitlab(t *testing.T) { // Gitlab detected. t.Setenv("GITLAB_CI", "true") - assert.Equal(t, "gitlab", CiCdProvider()) + assert.Equal(t, "gitlab", lookupCiCdProvider()) } func TestCiCdProviderJenkins(t *testing.T) { @@ -51,7 +51,7 @@ func TestCiCdProviderJenkins(t *testing.T) { // Jenkins detected. t.Setenv("JENKINS_URL", "https://jenkins.example.com") - assert.Equal(t, "jenkins", CiCdProvider()) + assert.Equal(t, "jenkins", lookupCiCdProvider()) } func TestCiCdProviderAzureDevops(t *testing.T) { @@ -59,7 +59,7 @@ func TestCiCdProviderAzureDevops(t *testing.T) { // Azure Devops detected. t.Setenv("TF_BUILD", "True") - assert.Equal(t, "azure-devops", CiCdProvider()) + assert.Equal(t, "azure-devops", lookupCiCdProvider()) } func TestCiCdProviderCircle(t *testing.T) { @@ -67,7 +67,7 @@ func TestCiCdProviderCircle(t *testing.T) { // Circle detected. t.Setenv("CIRCLECI", "true") - assert.Equal(t, "circle", CiCdProvider()) + assert.Equal(t, "circle", lookupCiCdProvider()) } func TestCiCdProviderTravis(t *testing.T) { @@ -75,7 +75,7 @@ func TestCiCdProviderTravis(t *testing.T) { // Travis detected. t.Setenv("TRAVIS", "true") - assert.Equal(t, "travis", CiCdProvider()) + assert.Equal(t, "travis", lookupCiCdProvider()) } func TestCiCdProviderBitbucket(t *testing.T) { @@ -83,7 +83,7 @@ func TestCiCdProviderBitbucket(t *testing.T) { // Bitbucket detected. t.Setenv("BITBUCKET_BUILD_NUMBER", "123") - assert.Equal(t, "bitbucket", CiCdProvider()) + assert.Equal(t, "bitbucket", lookupCiCdProvider()) } func TestCiCdProviderGoogleCloudBuild(t *testing.T) { @@ -94,7 +94,7 @@ func TestCiCdProviderGoogleCloudBuild(t *testing.T) { t.Setenv("BUILD_ID", "bar") t.Setenv("PROJECT_NUMBER", "baz") t.Setenv("LOCATION", "") - assert.Equal(t, "google-cloud-build", CiCdProvider()) + assert.Equal(t, "google-cloud-build", lookupCiCdProvider()) } func TestCiCdProviderAwsCodeBuild(t *testing.T) { @@ -102,7 +102,7 @@ func TestCiCdProviderAwsCodeBuild(t *testing.T) { // AWS Code Build detected. t.Setenv("CODEBUILD_BUILD_ARN", "arn:aws:codebuild:us-east-1:123456789012:build/my-demo-project:b1e6deae-e4f2-4151-be79-3cc4e82a0bf0") - assert.Equal(t, "aws-code-build", CiCdProvider()) + assert.Equal(t, "aws-code-build", lookupCiCdProvider()) } func TestCiCdProviderTfCloud(t *testing.T) { @@ -110,7 +110,7 @@ func TestCiCdProviderTfCloud(t *testing.T) { // Terraform Cloud detected. t.Setenv("TFC_RUN_ID", "run-123") - assert.Equal(t, "tf-cloud", CiCdProvider()) + assert.Equal(t, "tf-cloud", lookupCiCdProvider()) } func TestCiCdProviderMultiple(t *testing.T) { @@ -119,5 +119,5 @@ func TestCiCdProviderMultiple(t *testing.T) { // Multiple providers detected. The first one detected is set. t.Setenv("GITHUB_ACTIONS", "true") t.Setenv("GITLAB_CI", "true") - assert.Equal(t, "github", CiCdProvider()) + assert.Equal(t, "github", lookupCiCdProvider()) } From e338933a6147b730ac7cf760e392454b224ccec7 Mon Sep 17 00:00:00 2001 From: monalisa Date: Mon, 23 Oct 2023 14:29:17 +0200 Subject: [PATCH 09/10] - --- useragent/cicd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/useragent/cicd.go b/useragent/cicd.go index 6d97ab90e..3559baa8b 100644 --- a/useragent/cicd.go +++ b/useragent/cicd.go @@ -37,7 +37,6 @@ func listCiCdProviders() []cicdProvider { // detect returns true if all env vars are set and have expected values. func (p cicdProvider) detect() bool { - os.Environ() for _, envVar := range p.envVars { v, ok := os.LookupEnv(envVar.name) if !ok { From ee6fb914cfd7aa173b5d17d59ea72cf2f034a925 Mon Sep 17 00:00:00 2001 From: monalisa Date: Tue, 31 Oct 2023 16:58:59 +0100 Subject: [PATCH 10/10] add comments --- useragent/cicd.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/useragent/cicd.go b/useragent/cicd.go index 3559baa8b..04ca17677 100644 --- a/useragent/cicd.go +++ b/useragent/cicd.go @@ -6,7 +6,12 @@ import ( ) type envVar struct { - name string + // Name of the environment variable. + name string + + // Expected value of the environment variable. If empty, only the presence + // of the environment variable is checked. If non-empty, the value must + // match exactly. expectedValue string }