Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CI environment variables parsing #1763

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,34 @@ module Environment
module_function

def tags(env)
# Extract metadata from CI provider environment variables
_, extractor = PROVIDERS.find { |provider_env_var, _| env.key?(provider_env_var) }
if extractor
tags = public_send(extractor, env)

tags[Datadog::Ext::Git::TAG_TAG] = normalize_ref(tags[Datadog::Ext::Git::TAG_TAG])
tags.delete(Datadog::Ext::Git::TAG_BRANCH) unless tags[Datadog::Ext::Git::TAG_TAG].nil?
tags[Datadog::Ext::Git::TAG_BRANCH] = normalize_ref(tags[Datadog::Ext::Git::TAG_BRANCH])
tags[Datadog::Ext::Git::TAG_REPOSITORY_URL] = filter_sensitive_info(tags[Datadog::Ext::Git::TAG_REPOSITORY_URL])

# Expand ~
workspace_path = tags[TAG_WORKSPACE_PATH]
if !workspace_path.nil? && (workspace_path == '~' || workspace_path.start_with?('~/'))
tags[TAG_WORKSPACE_PATH] = File.expand_path(workspace_path)
end
else
tags = {}
tags = extractor ? public_send(extractor, env).reject { |_, v| v.nil? || v.strip.empty? } : {}
tags.delete(Datadog::Ext::Git::TAG_BRANCH) unless tags[Datadog::Ext::Git::TAG_TAG].nil?

# If user defined metadata is defined, overwrite
tags.merge!(extract_user_defined_git(env))
if !tags[Datadog::Ext::Git::TAG_BRANCH].nil? && tags[Datadog::Ext::Git::TAG_BRANCH].include?('tags/')
tags[Datadog::Ext::Git::TAG_TAG] = tags[Datadog::Ext::Git::TAG_BRANCH]
tags.delete(Datadog::Ext::Git::TAG_BRANCH)
end

# Normalize Git references
tags[Datadog::Ext::Git::TAG_TAG] = normalize_ref(tags[Datadog::Ext::Git::TAG_TAG])
tags[Datadog::Ext::Git::TAG_BRANCH] = normalize_ref(tags[Datadog::Ext::Git::TAG_BRANCH])
tags[Datadog::Ext::Git::TAG_REPOSITORY_URL] = filter_sensitive_info(tags[Datadog::Ext::Git::TAG_REPOSITORY_URL])

# Expand ~
workspace_path = tags[TAG_WORKSPACE_PATH]
if !workspace_path.nil? && (workspace_path == '~' || workspace_path.start_with?('~/'))
tags[TAG_WORKSPACE_PATH] = File.expand_path(workspace_path)
end

# Fill out tags from local git as fallback
extract_local_git.each do |key, value|
tags[key] ||= value
end

# If user defined metadata is defined, overwrite
tags.merge!(extract_user_defined_git(env))

tags.reject { |_, v| v.nil? }
end

Expand Down
43 changes: 32 additions & 11 deletions spec/datadog/ci/ext/fixtures/ci/buildkite.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git"
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -45,7 +46,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git"
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -77,7 +79,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git"
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -109,7 +112,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git"
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -142,6 +146,7 @@
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": "",
"HOME": "/not-my-home",
"USERPROFILE": "/not-my-home"
},
Expand Down Expand Up @@ -176,6 +181,7 @@
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": "",
"HOME": "/not-my-home",
"USERPROFILE": "/not-my-home"
},
Expand Down Expand Up @@ -210,6 +216,7 @@
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": "",
"HOME": "/not-my-home",
"USERPROFILE": "/not-my-home"
},
Expand Down Expand Up @@ -243,7 +250,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://user@hostname.com/repo.git"
"BUILDKITE_REPO": "http://user@hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -275,7 +283,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://user%E2%82%AC@hostname.com/repo.git"
"BUILDKITE_REPO": "http://user%E2%82%AC@hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -307,7 +316,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://user:pwd@hostname.com/repo.git"
"BUILDKITE_REPO": "http://user:pwd@hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -339,7 +349,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "git@hostname.com:org/repo.git"
"BUILDKITE_REPO": "git@hostname.com:org/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -371,7 +382,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git"
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -403,7 +415,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git"
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand Down Expand Up @@ -435,7 +448,8 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_REPO": "http://hostname.com/repo.git"
"BUILDKITE_REPO": "http://hostname.com/repo.git",
"BUILDKITE_TAG": ""
},
{
"ci.job.url": "buildkite-build-url#buildkite-job-id",
Expand All @@ -456,6 +470,7 @@
[
{
"BUILDKITE": "true",
"BUILDKITE_BRANCH": "",
"BUILDKITE_BUILD_AUTHOR": "buildkite-git-commit-author-name",
"BUILDKITE_BUILD_AUTHOR_EMAIL": "buildkite-git-commit-author-email@datadoghq.com",
"BUILDKITE_BUILD_CHECKOUT_PATH": "/foo/bar",
Expand Down Expand Up @@ -488,6 +503,7 @@
[
{
"BUILDKITE": "true",
"BUILDKITE_BRANCH": "",
"BUILDKITE_BUILD_AUTHOR": "buildkite-git-commit-author-name",
"BUILDKITE_BUILD_AUTHOR_EMAIL": "buildkite-git-commit-author-email@datadoghq.com",
"BUILDKITE_BUILD_CHECKOUT_PATH": "/foo/bar",
Expand Down Expand Up @@ -520,6 +536,7 @@
[
{
"BUILDKITE": "true",
"BUILDKITE_BRANCH": "",
"BUILDKITE_BUILD_AUTHOR": "buildkite-git-commit-author-name",
"BUILDKITE_BUILD_AUTHOR_EMAIL": "buildkite-git-commit-author-email@datadoghq.com",
"BUILDKITE_BUILD_CHECKOUT_PATH": "/foo/bar",
Expand Down Expand Up @@ -552,6 +569,7 @@
[
{
"BUILDKITE": "true",
"BUILDKITE_BRANCH": "",
"BUILDKITE_BUILD_AUTHOR": "buildkite-git-commit-author-name",
"BUILDKITE_BUILD_AUTHOR_EMAIL": "buildkite-git-commit-author-email@datadoghq.com",
"BUILDKITE_BUILD_ID": "buildkite-pipeline-id",
Expand All @@ -561,6 +579,7 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_TAG": "",
"DD_GIT_BRANCH": "user-supplied-branch",
"DD_GIT_COMMIT_AUTHOR_DATE": "usersupplied-authordate",
"DD_GIT_COMMIT_AUTHOR_EMAIL": "usersupplied-authoremail",
Expand Down Expand Up @@ -594,6 +613,7 @@
[
{
"BUILDKITE": "true",
"BUILDKITE_BRANCH": "",
"BUILDKITE_BUILD_AUTHOR": "buildkite-git-commit-author-name",
"BUILDKITE_BUILD_AUTHOR_EMAIL": "buildkite-git-commit-author-email@datadoghq.com",
"BUILDKITE_BUILD_ID": "buildkite-pipeline-id",
Expand All @@ -603,6 +623,7 @@
"BUILDKITE_JOB_ID": "buildkite-job-id",
"BUILDKITE_MESSAGE": "buildkite-git-commit-message",
"BUILDKITE_PIPELINE_SLUG": "buildkite-pipeline-name",
"BUILDKITE_TAG": "",
"DD_GIT_COMMIT_AUTHOR_DATE": "usersupplied-authordate",
"DD_GIT_COMMIT_AUTHOR_EMAIL": "usersupplied-authoremail",
"DD_GIT_COMMIT_AUTHOR_NAME": "usersupplied-authorname",
Expand Down
Loading