Skip to content

Commit

Permalink
extract user defined tags extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 4, 2023
1 parent d2a1035 commit f3be086
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 41 deletions.
21 changes: 4 additions & 17 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require_relative "git"
require_relative "environment/extractor"
require_relative "environment/user_defined_tags"

module Datadog
module CI
Expand All @@ -31,7 +32,9 @@ def tags(env)
tags = Environment::Extractor.for_environment(env).tags

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

# Normalize Git references
if !tags[Git::TAG_BRANCH].nil? && tags[Git::TAG_BRANCH].include?("tags/")
Expand Down Expand Up @@ -70,22 +73,6 @@ def filter_sensitive_info(url)
end

# CI providers
def extract_user_defined_git(env)
{
Git::TAG_REPOSITORY_URL => env[Git::ENV_REPOSITORY_URL],
Git::TAG_COMMIT_SHA => env[Git::ENV_COMMIT_SHA],
Git::TAG_BRANCH => env[Git::ENV_BRANCH],
Git::TAG_TAG => env[Git::ENV_TAG],
Git::TAG_COMMIT_MESSAGE => env[Git::ENV_COMMIT_MESSAGE],
Git::TAG_COMMIT_AUTHOR_NAME => env[Git::ENV_COMMIT_AUTHOR_NAME],
Git::TAG_COMMIT_AUTHOR_EMAIL => env[Git::ENV_COMMIT_AUTHOR_EMAIL],
Git::TAG_COMMIT_AUTHOR_DATE => env[Git::ENV_COMMIT_AUTHOR_DATE],
Git::TAG_COMMIT_COMMITTER_NAME => env[Git::ENV_COMMIT_COMMITTER_NAME],
Git::TAG_COMMIT_COMMITTER_EMAIL => env[Git::ENV_COMMIT_COMMITTER_EMAIL],
Git::TAG_COMMIT_COMMITTER_DATE => env[Git::ENV_COMMIT_COMMITTER_DATE]
}.reject { |_, v| v.nil? || v.strip.empty? }
end

def git_commit_users
# Get committer and author information in one command.
output = exec_git_command("git show -s --format='%an\t%ae\t%at\t%cn\t%ce\t%ct'")
Expand Down
60 changes: 60 additions & 0 deletions lib/datadog/ci/ext/environment/user_defined_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require_relative "extractor"
require_relative "../git"

module Datadog
module CI
module Ext
module Environment
# Parses user defined git data from the environment variables
# User documentation: https://docs.datadoghq.com/continuous_integration/troubleshooting/#data-appears-in-test-runs-but-not-tests
class UserDefinedTags < Extractor
def git_repository_url
env[Git::ENV_REPOSITORY_URL]
end

def git_commit_sha
env[Git::ENV_COMMIT_SHA]
end

def git_branch
env[Git::ENV_BRANCH]
end

def git_tag
env[Git::ENV_TAG]
end

def git_commit_message
env[Git::ENV_COMMIT_MESSAGE]
end

def git_commit_author_name
env[Git::ENV_COMMIT_AUTHOR_NAME]
end

def git_commit_author_email
env[Git::ENV_COMMIT_AUTHOR_EMAIL]
end

def git_commit_author_date
env[Git::ENV_COMMIT_AUTHOR_DATE]
end

def git_commit_committer_name
env[Git::ENV_COMMIT_COMMITTER_NAME]
end

def git_commit_committer_email
env[Git::ENV_COMMIT_COMMITTER_EMAIL]
end

def git_commit_committer_date
env[Git::ENV_COMMIT_COMMITTER_DATE]
end
end
end
end
end
end
48 changes: 24 additions & 24 deletions sig/datadog/ci/ext/environment/extractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,53 @@ module Datadog

attr_reader env: untyped

def job_name: () -> nil
def job_name: () -> String?

def job_url: () -> nil
def job_url: () -> String?

def pipeline_id: () -> nil
def pipeline_id: () -> String?

def pipeline_name: () -> nil
def pipeline_name: () -> String?

def pipeline_number: () -> nil
def pipeline_number: () -> String?

def pipeline_url: () -> nil
def pipeline_url: () -> String?

def provider_name: () -> nil
def provider_name: () -> String?

def stage_name: () -> nil
def stage_name: () -> String?

def workspace_path: () -> nil
def workspace_path: () -> String?

def node_labels: () -> nil
def node_labels: () -> String?

def node_name: () -> nil
def node_name: () -> String?

def ci_env_vars: () -> nil
def ci_env_vars: () -> String?

def git_branch: () -> nil
def git_branch: () -> String?

def git_repository_url: () -> nil
def git_repository_url: () -> String?

def git_tag: () -> nil
def git_tag: () -> String?

def git_branch_or_tag: () -> nil
def git_branch_or_tag: () -> String?

def git_commit_author_date: () -> nil
def git_commit_author_date: () -> String?

def git_commit_author_email: () -> nil
def git_commit_author_email: () -> String?

def git_commit_author_name: () -> nil
def git_commit_author_name: () -> String?

def git_commit_committer_date: () -> nil
def git_commit_committer_date: () -> String?

def git_commit_committer_email: () -> nil
def git_commit_committer_email: () -> String?

def git_commit_committer_name: () -> nil
def git_commit_committer_name: () -> String?

def git_commit_message: () -> nil
def git_commit_message: () -> String?

def git_commit_sha: () -> nil
def git_commit_sha: () -> String?

def set_branch_and_tag: () -> [String?, String?]

Expand Down
31 changes: 31 additions & 0 deletions sig/datadog/ci/ext/environment/user_defined_tags.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Datadog
module CI
module Ext
module Environment
class UserDefinedTags < Extractor
def git_repository_url: () -> String?

def git_commit_sha: () -> String?

def git_branch: () -> String?

def git_tag: () -> String?

def git_commit_message: () -> String?

def git_commit_author_name: () -> String?

def git_commit_author_email: () -> String?

def git_commit_author_date: () -> String?

def git_commit_committer_name: () -> String?

def git_commit_committer_email: () -> String?

def git_commit_committer_date: () -> String?
end
end
end
end
end

0 comments on commit f3be086

Please sign in to comment.