diff --git a/lib/datadog/ci/ext/environment.rb b/lib/datadog/ci/ext/environment.rb index ee7bca6c..391d7a27 100644 --- a/lib/datadog/ci/ext/environment.rb +++ b/lib/datadog/ci/ext/environment.rb @@ -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)) @@ -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], diff --git a/lib/datadog/ci/ext/environment/extractor.rb b/lib/datadog/ci/ext/environment/extractor.rb index 8cf05459..4ae87489 100644 --- a/lib/datadog/ci/ext/environment/extractor.rb +++ b/lib/datadog/ci/ext/environment/extractor.rb @@ -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) @@ -135,6 +137,9 @@ def git_repository_url def git_tag end + def git_branch_or_tag + end + def git_commit_author_date end diff --git a/lib/datadog/ci/ext/environment/providers/codefresh.rb b/lib/datadog/ci/ext/environment/providers/codefresh.rb new file mode 100644 index 00000000..0f08ab81 --- /dev/null +++ b/lib/datadog/ci/ext/environment/providers/codefresh.rb @@ -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 diff --git a/sig/datadog/ci/ext/environment/extractor.rbs b/sig/datadog/ci/ext/environment/extractor.rbs index b79fd1c2..9a48dd5a 100644 --- a/sig/datadog/ci/ext/environment/extractor.rbs +++ b/sig/datadog/ci/ext/environment/extractor.rbs @@ -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 diff --git a/sig/datadog/ci/ext/environment/providers/codefresh.rbs b/sig/datadog/ci/ext/environment/providers/codefresh.rbs new file mode 100644 index 00000000..153c591e --- /dev/null +++ b/sig/datadog/ci/ext/environment/providers/codefresh.rbs @@ -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