Skip to content

Commit

Permalink
show alternative code organization around provider and extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Sep 6, 2023
1 parent 99b2e08 commit 4618739
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 256 deletions.
8 changes: 3 additions & 5 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

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

module Datadog
module CI
Expand All @@ -29,15 +27,15 @@ module Environment

def tags(env)
# Extract metadata from CI provider environment variables
tags = Environment::Extractor.for_environment(env).tags
tags = Environment::Extractor.new(env).tags

# If user defined metadata is defined, overwrite
tags.merge!(
UserDefinedTags.new(env).tags
Environment::Extractor.new(env, provider: Providers::UserDefinedTags).tags
)

# Fill out tags from local git as fallback
local_git_tags = LocalGit.new(env).tags
local_git_tags = Environment::Extractor.new(env, provider: LocalGit).tags
local_git_tags.each do |key, value|
tags[key] ||= value
end
Expand Down
185 changes: 27 additions & 158 deletions lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "../environment"
require_relative "../git"
require_relative "providers"

module Datadog
module CI
Expand All @@ -11,76 +12,39 @@ module Environment
# Extractor is responsible for detecting where pipeline is being executed based on environment vars
# and return the specific extractor that is able to return environment- and git-specific tags
class Extractor
require_relative "providers/default"
require_relative "providers/appveyor"
require_relative "providers/azure"
require_relative "providers/bitbucket"
require_relative "providers/bitrise"
require_relative "providers/buddy"
require_relative "providers/buildkite"
require_relative "providers/circleci"
require_relative "providers/codefresh"
require_relative "providers/github_actions"
require_relative "providers/gitlab"
require_relative "providers/jenkins"
require_relative "providers/teamcity"
require_relative "providers/travis"

PROVIDERS = [
["APPVEYOR", Providers::Appveyor],
["TF_BUILD", Providers::Azure],
["BITBUCKET_COMMIT", Providers::Bitbucket],
["BITRISE_BUILD_SLUG", Providers::Bitrise],
["BUDDY", Providers::Buddy],
["BUILDKITE", Providers::Buildkite],
["CIRCLECI", Providers::Circleci],
["CF_BUILD_ID", Providers::Codefresh],
["GITHUB_SHA", Providers::GithubActions],
["GITLAB_CI", Providers::Gitlab],
["JENKINS_URL", Providers::Jenkins],
["TEAMCITY_VERSION", Providers::Teamcity],
["TRAVIS", Providers::Travis]
]

def self.for_environment(env)
_, extractor_klass = PROVIDERS.find { |provider_env_var, _| env.key?(provider_env_var) }
extractor_klass = Providers::Default if extractor_klass.nil?

extractor_klass.new(env)
end

def initialize(env)
def initialize(env, provider: nil)
@env = env
@provider = provider || Providers.for_environment(env)
end

def 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,
Environment::TAG_PIPELINE_NAME => pipeline_name,
Environment::TAG_PIPELINE_NUMBER => pipeline_number,
Environment::TAG_PIPELINE_URL => pipeline_url,
Environment::TAG_PROVIDER_NAME => provider_name,
Environment::TAG_STAGE_NAME => stage_name,
Environment::TAG_WORKSPACE_PATH => workspace_path,
Environment::TAG_NODE_LABELS => node_labels,
Environment::TAG_NODE_NAME => node_name,
Environment::TAG_CI_ENV_VARS => ci_env_vars,

Git::TAG_BRANCH => git_branch,
Git::TAG_REPOSITORY_URL => git_repository_url,
Git::TAG_TAG => git_tag,
Git::TAG_COMMIT_AUTHOR_DATE => git_commit_author_date,
Git::TAG_COMMIT_AUTHOR_EMAIL => git_commit_author_email,
Git::TAG_COMMIT_AUTHOR_NAME => git_commit_author_name,
Git::TAG_COMMIT_COMMITTER_DATE => git_commit_committer_date,
Git::TAG_COMMIT_COMMITTER_EMAIL => git_commit_committer_email,
Git::TAG_COMMIT_COMMITTER_NAME => git_commit_committer_name,
Git::TAG_COMMIT_MESSAGE => git_commit_message,
Git::TAG_COMMIT_SHA => git_commit_sha
Environment::TAG_JOB_NAME => @provider.job_name,
Environment::TAG_JOB_URL => @provider.job_url,
Environment::TAG_PIPELINE_ID => @provider.pipeline_id,
Environment::TAG_PIPELINE_NAME => @provider.pipeline_name,
Environment::TAG_PIPELINE_NUMBER => @provider.pipeline_number,
Environment::TAG_PIPELINE_URL => @provider.pipeline_url,
Environment::TAG_PROVIDER_NAME => @provider.provider_name,
Environment::TAG_STAGE_NAME => @provider.stage_name,
Environment::TAG_WORKSPACE_PATH => @provider.workspace_path,
Environment::TAG_NODE_LABELS => @provider.node_labels,
Environment::TAG_NODE_NAME => @provider.node_name,
Environment::TAG_CI_ENV_VARS => @provider.ci_env_vars,

Git::TAG_BRANCH => @provider.git_branch,
Git::TAG_REPOSITORY_URL => @provider.git_repository_url,
Git::TAG_TAG => @provider.git_tag,
Git::TAG_COMMIT_AUTHOR_DATE => @provider.git_commit_author_date,
Git::TAG_COMMIT_AUTHOR_EMAIL => @provider.git_commit_author_email,
Git::TAG_COMMIT_AUTHOR_NAME => @provider.git_commit_author_name,
Git::TAG_COMMIT_COMMITTER_DATE => @provider.git_commit_committer_date,
Git::TAG_COMMIT_COMMITTER_EMAIL => @provider.git_commit_committer_email,
Git::TAG_COMMIT_COMMITTER_NAME => @provider.git_commit_committer_name,
Git::TAG_COMMIT_MESSAGE => @provider.git_commit_message,
Git::TAG_COMMIT_SHA => @provider.git_commit_sha
}

# Normalize Git references and filter sensitive data
Expand All @@ -102,88 +66,6 @@ def tags

private

attr_reader :env

def job_name
end

def job_url
end

def pipeline_id
end

def pipeline_name
end

def pipeline_number
end

def pipeline_url
end

def provider_name
end

def stage_name
end

def workspace_path
end

def node_labels
end

def node_name
end

def ci_env_vars
end

def git_branch
return @branch if defined?(@branch)

set_branch_and_tag
@branch
end

def git_repository_url
end

def git_tag
return @tag if defined?(@tag)

set_branch_and_tag
@tag
end

def git_branch_or_tag
end

def git_commit_author_date
end

def git_commit_author_email
end

def git_commit_author_name
end

def git_commit_committer_date
end

def git_commit_committer_email
end

def git_commit_committer_name
end

def git_commit_message
end

def git_commit_sha
end

def normalize_git!
branch_ref = @tags[Git::TAG_BRANCH]
if is_git_tag?(branch_ref)
Expand All @@ -210,19 +92,6 @@ 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
@branch = branch_or_tag_string
end

[@branch, @tag]
end

def normalize_ref(name)
return nil if name.nil?

Expand Down
49 changes: 49 additions & 0 deletions lib/datadog/ci/ext/environment/providers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require_relative "providers/default"
require_relative "providers/appveyor"
require_relative "providers/azure"
require_relative "providers/bitbucket"
require_relative "providers/bitrise"
require_relative "providers/buddy"
require_relative "providers/buildkite"
require_relative "providers/circleci"
require_relative "providers/codefresh"
require_relative "providers/github_actions"
require_relative "providers/gitlab"
require_relative "providers/jenkins"
require_relative "providers/teamcity"
require_relative "providers/travis"

module Datadog
module CI
module Ext
module Environment
module Providers
PROVIDERS = [
["APPVEYOR", Providers::Appveyor],
["TF_BUILD", Providers::Azure],
["BITBUCKET_COMMIT", Providers::Bitbucket],
["BITRISE_BUILD_SLUG", Providers::Bitrise],
["BUDDY", Providers::Buddy],
["BUILDKITE", Providers::Buildkite],
["CIRCLECI", Providers::Circleci],
["CF_BUILD_ID", Providers::Codefresh],
["GITHUB_SHA", Providers::GithubActions],
["GITLAB_CI", Providers::Gitlab],
["JENKINS_URL", Providers::Jenkins],
["TEAMCITY_VERSION", Providers::Teamcity],
["TRAVIS", Providers::Travis]
]

def self.for_environment(env)
_, provider_klass = PROVIDERS.find { |provider_env_var, _| env.key?(provider_env_var) }
provider_klass = Providers::Default if provider_klass.nil?

provider_klass.new(env)
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/datadog/ci/ext/environment/providers/appveyor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "../extractor"
require_relative "base"

module Datadog
module CI
Expand All @@ -9,7 +9,7 @@ module Environment
module Providers
# Appveyor: https://www.appveyor.com/
# Environment variables docs: https://www.appveyor.com/docs/environment-variables/
class Appveyor < Extractor
class Appveyor < Base
private

# overridden methods
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/ext/environment/providers/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "json"

require_relative "../extractor"
require_relative "base"

module Datadog
module CI
Expand All @@ -11,7 +11,7 @@ module Environment
module Providers
# Azure Pipelines: https://azure.microsoft.com/en-us/products/devops/pipelines
# Environment variables docs: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
class Azure < Extractor
class Azure < Base
private

# overridden methods
Expand Down
Loading

0 comments on commit 4618739

Please sign in to comment.