Skip to content

Commit

Permalink
reshuffle code locations, extract Buddy extractor to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 1, 2023
1 parent 1f70294 commit d01f763
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 492 deletions.
23 changes: 2 additions & 21 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "json"

require_relative "git"
require_relative "providers/extractor"
require_relative "environment/extractor"

module Datadog
module CI
Expand All @@ -25,7 +25,6 @@ module Environment
TAG_CI_ENV_VARS = "_dd.ci.env_vars"

PROVIDERS = [
["BUDDY", :extract_buddy],
["BUILDKITE", :extract_buildkite],
["CIRCLECI", :extract_circle_ci],
["GITHUB_SHA", :extract_github_actions],
Expand All @@ -42,7 +41,7 @@ module Environment
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? } : Providers::Extractor.for_environment(env).tags
tags = extractor ? public_send(extractor, env).reject { |_, v| v.nil? || v.strip.empty? } : Environment::Extractor.for_environment(env).tags

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

# CI providers
def extract_buddy(env)
{
TAG_PROVIDER_NAME => "buddy",
TAG_PIPELINE_ID => "#{env["BUDDY_PIPELINE_ID"]}/#{env["BUDDY_EXECUTION_ID"]}",
TAG_PIPELINE_NAME => env["BUDDY_PIPELINE_NAME"],
TAG_PIPELINE_NUMBER => env["BUDDY_EXECUTION_ID"],
TAG_PIPELINE_URL => env["BUDDY_EXECUTION_URL"],
TAG_WORKSPACE_PATH => env["CI_WORKSPACE_PATH"],
Git::TAG_REPOSITORY_URL => env["BUDDY_SCM_URL"],
Git::TAG_COMMIT_SHA => env["BUDDY_EXECUTION_REVISION"],
Git::TAG_BRANCH => env["BUDDY_EXECUTION_BRANCH"],
Git::TAG_TAG => env["BUDDY_EXECUTION_TAG"],
Git::TAG_COMMIT_MESSAGE => env["BUDDY_EXECUTION_REVISION_MESSAGE"],
Git::TAG_COMMIT_COMMITTER_NAME => env["BUDDY_EXECUTION_REVISION_COMMITTER_NAME"],
Git::TAG_COMMIT_COMMITTER_EMAIL => env["BUDDY_EXECUTION_REVISION_COMMITTER_EMAIL"]
}
end

def extract_buildkite(env)
tags = {
Git::TAG_BRANCH => env["BUILDKITE_BRANCH"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
module Datadog
module CI
module Ext
module Providers
module Environment
# Provider is a specific CI provider like Azure Pipelines, Github Actions, Gitlab CI, etc
# Providers::Extractor is responsible for detecting where pipeline is being executed based on environment vars
# 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 "default"
require_relative "appveyor"
require_relative "azure"
require_relative "bitbucket"
require_relative "providers/default"
require_relative "providers/appveyor"
require_relative "providers/azure"
require_relative "providers/bitbucket"
require_relative "providers/buddy"

EXTRACTORS = [
["APPVEYOR", Appveyor],
["TF_BUILD", Azure],
["BITBUCKET_COMMIT", Bitbucket]
["APPVEYOR", Providers::Appveyor],
["TF_BUILD", Providers::Azure],
["BITBUCKET_COMMIT", Providers::Bitbucket],
["BUDDY", Providers::Buddy]
]

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

extractor_klass.new(env)
end
Expand Down
101 changes: 101 additions & 0 deletions lib/datadog/ci/ext/environment/providers/appveyor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# frozen_string_literal: true

require_relative "../extractor"

module Datadog
module CI
module Ext
module Environment
module Providers
# Appveyor: https://www.appveyor.com/
# Environment variables docs: https://www.appveyor.com/docs/environment-variables/
class Appveyor < Extractor
private

# overridden methods
def provider_name
"appveyor"
end

def pipeline_url
url
end

def job_url
url
end

def workspace_path
env["APPVEYOR_BUILD_FOLDER"]
end

def pipeline_id
env["APPVEYOR_BUILD_ID"]
end

def pipeline_name
env["APPVEYOR_REPO_NAME"]
end

def pipeline_number
env["APPVEYOR_BUILD_NUMBER"]
end

def git_repository_url
return nil unless github_repo_provider?

"https://github.com/#{env["APPVEYOR_REPO_NAME"]}.git"
end

def git_commit_sha
return nil unless github_repo_provider?

env["APPVEYOR_REPO_COMMIT"]
end

def git_branch
return nil unless github_repo_provider?

env["APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH"] || env["APPVEYOR_REPO_BRANCH"]
end

def git_tag
return nil unless github_repo_provider?

env["APPVEYOR_REPO_TAG_NAME"]
end

def git_commit_author_name
env["APPVEYOR_REPO_COMMIT_AUTHOR"]
end

def git_commit_author_email
env["APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL"]
end

def git_commit_message
commit_message = env["APPVEYOR_REPO_COMMIT_MESSAGE"]
if commit_message
extended = env["APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED"]
commit_message = "#{commit_message}\n#{extended}" if extended
end
commit_message
end

# appveyor-specific methods

def github_repo_provider?
return @github_repo_provider if defined?(@github_repo_provider)

@github_repo_provider = env["APPVEYOR_REPO_PROVIDER"] == "github"
end

def url
@url ||= "https://ci.appveyor.com/project/#{env["APPVEYOR_REPO_NAME"]}/builds/#{env["APPVEYOR_BUILD_ID"]}"
end
end
end
end
end
end
end
126 changes: 126 additions & 0 deletions lib/datadog/ci/ext/environment/providers/azure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

require_relative "../extractor"

module Datadog
module CI
module Ext
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
private

# overridden methods
def provider_name
"azurepipelines"
end

def pipeline_url
return unless url_defined?

@pipeline_url ||= "#{team_foundation_server_uri}#{team_project_id}/_build/results?buildId=#{build_id}"
end

def job_url
return unless url_defined?

@job_url ||= "#{pipeline_url}&view=logs&j=#{env["SYSTEM_JOBID"]}&t=#{env["SYSTEM_TASKINSTANCEID"]}"
end

def workspace_path
env["BUILD_SOURCESDIRECTORY"]
end

def pipeline_id
build_id
end

def pipeline_number
build_id
end

def pipeline_name
env["BUILD_DEFINITIONNAME"]
end

def stage_name
env["SYSTEM_STAGEDISPLAYNAME"]
end

def job_name
env["SYSTEM_JOBDISPLAYNAME"]
end

def git_repository_url
env["SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI"] || env["BUILD_REPOSITORY_URI"]
end

def git_commit_sha
env["SYSTEM_PULLREQUEST_SOURCECOMMITID"] || env["BUILD_SOURCEVERSION"]
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 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
{
"SYSTEM_TEAMPROJECTID" => env["SYSTEM_TEAMPROJECTID"],
"BUILD_BUILDID" => env["BUILD_BUILDID"],
"SYSTEM_JOBID" => env["SYSTEM_JOBID"]
}.to_json
end

# azure-specific methods

def build_id
env["BUILD_BUILDID"]
end

def team_foundation_server_uri
env["SYSTEM_TEAMFOUNDATIONSERVERURI"]
end

def team_project_id
env["SYSTEM_TEAMPROJECTID"]
end

def url_defined?
!(build_id && team_foundation_server_uri && team_project_id).nil?
end

def set_branch_and_tag
@branch, @tag = branch_or_tag(
env["SYSTEM_PULLREQUEST_SOURCEBRANCH"] || env["BUILD_SOURCEBRANCH"] || env["BUILD_SOURCEBRANCHNAME"]
)
end
end
end
end
end
end
end
71 changes: 71 additions & 0 deletions lib/datadog/ci/ext/environment/providers/bitbucket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

require_relative "../extractor"

module Datadog
module CI
module Ext
module Environment
module Providers
# Bitbucket Pipelines: https://bitbucket.org/product/features/pipelines
# Environment variables docs: https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/
class Bitbucket < Extractor
private

# overridden methods
def provider_name
"bitbucket"
end

def pipeline_id
env["BITBUCKET_PIPELINE_UUID"] ? env["BITBUCKET_PIPELINE_UUID"].tr("{}", "") : nil
end

def pipeline_name
env["BITBUCKET_REPO_FULL_NAME"]
end

def pipeline_number
env["BITBUCKET_BUILD_NUMBER"]
end

def pipeline_url
url
end

def job_url
url
end

def workspace_path
env["BITBUCKET_CLONE_DIR"]
end

def git_repository_url
env["BITBUCKET_GIT_SSH_ORIGIN"] || env["BITBUCKET_GIT_HTTP_ORIGIN"]
end

def git_commit_sha
env["BITBUCKET_COMMIT"]
end

def git_branch
env["BITBUCKET_BRANCH"]
end

def git_tag
env["BITBUCKET_TAG"]
end

# bitbucket-specific methods

def url
"https://bitbucket.org/#{env["BITBUCKET_REPO_FULL_NAME"]}/addon/pipelines/home#" \
"!/results/#{env["BITBUCKET_BUILD_NUMBER"]}"
end
end
end
end
end
end
end
Loading

0 comments on commit d01f763

Please sign in to comment.