diff --git a/lib/datadog/ci/ext/environment/extractor.rb b/lib/datadog/ci/ext/environment/extractor.rb index 4bb5ef70..354093fb 100644 --- a/lib/datadog/ci/ext/environment/extractor.rb +++ b/lib/datadog/ci/ext/environment/extractor.rb @@ -54,7 +54,9 @@ def initialize(env) end def tags - tags = { + return @tags if defined?(@tags) + + @tags = { Environment::TAG_JOB_NAME => job_name, Environment::TAG_JOB_URL => job_url, Environment::TAG_PIPELINE_ID => pipeline_id, @@ -79,7 +81,15 @@ def tags Git::TAG_COMMIT_COMMITTER_NAME => git_commit_committer_name, Git::TAG_COMMIT_MESSAGE => git_commit_message, Git::TAG_COMMIT_SHA => git_commit_sha - }.reject do |_, v| + } + + # Normalize Git references and filter sensitive data + normalize_git! + # Expand ~ + expand_workspace! + + # remove empty tags + @tags.reject! do |_, v| # setting type of v here to untyped because steep does not # understand `v.nil? || something` @@ -87,12 +97,7 @@ def tags v.nil? || v.strip.empty? end - # Normalize Git references and filter sensitive data - normalize_git!(tags) - # Expand ~ - expand_workspace!(tags) - - tags + @tags end private @@ -179,33 +184,36 @@ def git_commit_message def git_commit_sha end - def normalize_git!(tags) - if !tags[Git::TAG_BRANCH].nil? && tags[Git::TAG_BRANCH].include?("tags/") - tags[Git::TAG_TAG] = tags[Git::TAG_BRANCH] - tags.delete(Git::TAG_BRANCH) + def normalize_git! + branch_ref = @tags[Git::TAG_BRANCH] + if is_git_tag?(branch_ref) + @tags[Git::TAG_TAG] = branch_ref + @tags.delete(Git::TAG_BRANCH) end - tags[Git::TAG_TAG] = normalize_ref(tags[Git::TAG_TAG]) if tags[Git::TAG_TAG] - tags[Git::TAG_BRANCH] = normalize_ref(tags[Git::TAG_BRANCH]) if tags[Git::TAG_BRANCH] - - if tags[Git::TAG_REPOSITORY_URL] - tags[Git::TAG_REPOSITORY_URL] = filter_sensitive_info( - tags[Git::TAG_REPOSITORY_URL] - ) - end + @tags[Git::TAG_TAG] = normalize_ref(@tags[Git::TAG_TAG]) + @tags[Git::TAG_BRANCH] = normalize_ref(@tags[Git::TAG_BRANCH]) + @tags[Git::TAG_REPOSITORY_URL] = filter_sensitive_info( + @tags[Git::TAG_REPOSITORY_URL] + ) end - def expand_workspace!(tags) - workspace_path = tags[TAG_WORKSPACE_PATH] + def expand_workspace! + 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) + @tags[TAG_WORKSPACE_PATH] = File.expand_path(workspace_path) end end + def is_git_tag?(ref) + !ref.nil? && ref.include?("tags/") + end + def set_branch_and_tag branch_or_tag_string = git_branch_or_tag @branch = @tag = nil + if branch_or_tag_string && branch_or_tag_string.include?("tags/") @tag = branch_or_tag_string else diff --git a/sig/datadog/ci/ext/environment/extractor.rbs b/sig/datadog/ci/ext/environment/extractor.rbs index cb71c65a..b9a0f256 100644 --- a/sig/datadog/ci/ext/environment/extractor.rbs +++ b/sig/datadog/ci/ext/environment/extractor.rbs @@ -5,6 +5,7 @@ module Datadog class Extractor PROVIDERS: ::Array[[String, singleton(Extractor)]] + @tags: Hash[String, untyped] @branch: String? @tag: String? @@ -68,9 +69,11 @@ module Datadog def set_branch_and_tag: () -> [String?, String?] - def normalize_git!: (Hash[String, untyped] tags) -> void + def normalize_git!: () -> void - def expand_workspace!: (Hash[String, untyped] tags) -> void + def expand_workspace!: () -> void + + def is_git_tag?: (String? ref) -> bool def normalize_ref: (String? name) -> String?