Skip to content

Commit

Permalink
extract codefresh
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 4, 2023
1 parent ca3b200 commit c7a2845
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 26 deletions.
24 changes: 1 addition & 23 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ module Environment
TAG_NODE_NAME = "ci.node.name"
TAG_CI_ENV_VARS = "_dd.ci.env_vars"

PROVIDERS = [
["CF_BUILD_ID", :extract_codefresh]
].freeze

module_function

def tags(env)
# Extract metadata from CI provider environment variables
_, extractor = PROVIDERS.find { |provider_env_var, _| env.key?(provider_env_var) }
tags = extractor ? public_send(extractor, env).reject { |_, v| v.nil? || v.strip.empty? } : Environment::Extractor.for_environment(env).tags
tags = Environment::Extractor.for_environment(env).tags

# If user defined metadata is defined, overwrite
tags.merge!(extract_user_defined_git(env))
Expand Down Expand Up @@ -75,23 +70,6 @@ def filter_sensitive_info(url)
end

# CI providers
def extract_codefresh(env)
branch, tag = branch_or_tag(env["CF_BRANCH"])

{
TAG_PROVIDER_NAME => "codefresh",
TAG_PIPELINE_ID => env["CF_BUILD_ID"],
TAG_PIPELINE_NAME => env["CF_PIPELINE_NAME"],
TAG_PIPELINE_URL => env["CF_BUILD_URL"],
TAG_JOB_NAME => env["CF_STEP_NAME"],
Git::TAG_BRANCH => branch,
Git::TAG_TAG => tag,
TAG_CI_ENV_VARS => {
"CF_BUILD_ID" => env["CF_BUILD_ID"]
}.to_json
}
end

def extract_user_defined_git(env)
{
Git::TAG_REPOSITORY_URL => env[Git::ENV_REPOSITORY_URL],
Expand Down
11 changes: 8 additions & 3 deletions lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@ class Extractor
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"
require_relative "providers/bitrise"

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],
["BITRISE_BUILD_SLUG", Providers::Bitrise]
["TRAVIS", Providers::Travis]
]

def self.for_environment(env)
Expand Down Expand Up @@ -135,6 +137,9 @@ def git_repository_url
def git_tag
end

def git_branch_or_tag
end

def git_commit_author_date
end

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

require_relative "../extractor"

module Datadog
module CI
module Ext
module Environment
module Providers
# Codefresh: https://codefresh.io/
# Environment variables docs: https://codefresh.io/docs/docs/pipelines/variables/#export-variables-to-all-steps-with-cf_export
class Codefresh < Extractor
private

# overridden methods
def provider_name
"codefresh"
end

def job_name
env["CF_STEP_NAME"]
end

def pipeline_id
env["CF_BUILD_ID"]
end

def pipeline_name
env["CF_PIPELINE_NAME"]
end

def pipeline_url
env["CF_BUILD_URL"]
end

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

set_branch_and_tag
@branch
end

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

set_branch_and_tag
@tag
end

def ci_env_vars
{
"CF_BUILD_ID" => env["CF_BUILD_ID"]
}.to_json
end

# codefresh-specific methods
def set_branch_and_tag
@branch, @tag = branch_or_tag(env["CF_BRANCH"])
end
end
end
end
end
end
end
2 changes: 2 additions & 0 deletions sig/datadog/ci/ext/environment/extractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module Datadog

def git_tag: () -> nil

def git_branch_or_tag: () -> nil

def git_commit_author_date: () -> nil

def git_commit_author_email: () -> nil
Expand Down
30 changes: 30 additions & 0 deletions sig/datadog/ci/ext/environment/providers/codefresh.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Datadog
module CI
module Ext
module Environment
module Providers
class Codefresh < Extractor
private
def provider_name: () -> "codefresh"

def job_name: () -> String?

def pipeline_id: () -> String?

def pipeline_name: () -> String?

def pipeline_url: () -> String?

def git_branch: () -> String?

def git_tag: () -> String?

def ci_env_vars: () -> String

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

0 comments on commit c7a2845

Please sign in to comment.