Skip to content

Commit

Permalink
extract Circle CI to a separate extractor class
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 4, 2023
1 parent c6d0401 commit 852bd81
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module Environment
TAG_CI_ENV_VARS = "_dd.ci.env_vars"

PROVIDERS = [
["CIRCLECI", :extract_circle_ci],
["GITHUB_SHA", :extract_github_actions],
["GITLAB_CI", :extract_gitlab],
["JENKINS_URL", :extract_jenkins],
Expand All @@ -44,12 +43,12 @@ def tags(env)

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

# Normalize Git references
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)
end

# Normalize Git references
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(
Expand Down
4 changes: 3 additions & 1 deletion lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ class Extractor
require_relative "providers/bitbucket"
require_relative "providers/buddy"
require_relative "providers/buildkite"
require_relative "providers/circleci"

PROVIDERS = [
["APPVEYOR", Providers::Appveyor],
["TF_BUILD", Providers::Azure],
["BITBUCKET_COMMIT", Providers::Bitbucket],
["BUDDY", Providers::Buddy],
["BUILDKITE", Providers::Buildkite]
["BUILDKITE", Providers::Buildkite],
["CIRCLECI", Providers::Circleci]
]

def self.for_environment(env)
Expand Down
2 changes: 0 additions & 2 deletions lib/datadog/ci/ext/environment/providers/buildkite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ def ci_env_vars
"BUILDKITE_JOB_ID" => env["BUILDKITE_JOB_ID"]
}.to_json
end

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

require_relative "../extractor"

module Datadog
module CI
module Ext
module Environment
module Providers
# Circle CI: https://circleci.com/
# Environment variables docs: https://circleci.com/docs/variables/#built-in-environment-variables
class Circleci < Extractor
private

# overridden methods
def provider_name
"circleci"
end

def job_url
env["CIRCLE_BUILD_URL"]
end

def job_name
env["CIRCLE_JOB"]
end

def pipeline_id
env["CIRCLE_WORKFLOW_ID"]
end

def pipeline_name
env["CIRCLE_PROJECT_REPONAME"]
end

def pipeline_url
"https://app.circleci.com/pipelines/workflows/#{env["CIRCLE_WORKFLOW_ID"]}"
end

def workspace_path
env["CIRCLE_WORKING_DIRECTORY"]
end

def git_repository_url
env["CIRCLE_REPOSITORY_URL"]
end

def git_commit_sha
env["CIRCLE_SHA1"]
end

def git_branch
env["CIRCLE_BRANCH"]
end

def git_tag
env["CIRCLE_TAG"]
end

def git_commit_author_name
env["BUILD_REQUESTEDFORID"]
end

def git_commit_author_email
env["BUILD_REQUESTEDFOREMAIL"]
end

def git_commit_message
env["BUILD_SOURCEVERSIONMESSAGE"]
end

def ci_env_vars
{
"CIRCLE_WORKFLOW_ID" => env["CIRCLE_WORKFLOW_ID"],
"CIRCLE_BUILD_NUM" => env["CIRCLE_BUILD_NUM"]
}.to_json
end
end
end
end
end
end
end
42 changes: 42 additions & 0 deletions sig/datadog/ci/ext/environment/providers/circleci.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Datadog
module CI
module Ext
module Environment
module Providers
class Circleci < Extractor
private
def provider_name: () -> "circleci"

def job_url: () -> String?

def job_name: () -> String?

def pipeline_id: () -> String?

def pipeline_name: () -> String?

def pipeline_url: () -> ::String

def workspace_path: () -> String?

def git_repository_url: () -> String?

def git_commit_sha: () -> String?

def git_branch: () -> String?

def git_tag: () -> String?

def git_commit_author_name: () -> String?

def git_commit_author_email: () -> String?

def git_commit_message: () -> String?

def ci_env_vars: () -> ::String
end
end
end
end
end
end

0 comments on commit 852bd81

Please sign in to comment.