Skip to content

Commit

Permalink
add @tags class var and remove some nil checks
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 4, 2023
1 parent 57c5809 commit e3e0900
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
54 changes: 31 additions & 23 deletions lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -79,20 +81,23 @@ 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`

# @type var v: untyped
v.nil? || v.strip.empty?
end

# Normalize Git references and filter sensitive data
normalize_git!(tags)
# Expand ~
expand_workspace!(tags)

tags
@tags
end

private
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions sig/datadog/ci/ext/environment/extractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Datadog
class Extractor

PROVIDERS: ::Array[[String, singleton(Extractor)]]
@tags: Hash[String, untyped]
@branch: String?
@tag: String?

Expand Down Expand Up @@ -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?

Expand Down

0 comments on commit e3e0900

Please sign in to comment.