Skip to content

Commit

Permalink
extract Azure environment extractor to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 1, 2023
1 parent b5c77ba commit 895ee04
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 80 deletions.
42 changes: 0 additions & 42 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 = [
["TF_BUILD", :extract_azure_pipelines],
["BITBUCKET_COMMIT", :extract_bitbucket],
["BUDDY", :extract_buddy],
["BUILDKITE", :extract_buildkite],
Expand Down Expand Up @@ -86,47 +85,6 @@ def filter_sensitive_info(url)
end

# CI providers
def extract_azure_pipelines(env)
build_id = env["BUILD_BUILDID"]

if build_id &&
(team_foundation_server_uri = env["SYSTEM_TEAMFOUNDATIONSERVERURI"]) &&
(team_project_id = env["SYSTEM_TEAMPROJECTID"])
pipeline_url = "#{team_foundation_server_uri}#{team_project_id}/_build/results?buildId=#{build_id}"
job_url = "#{pipeline_url}&view=logs&j=#{env["SYSTEM_JOBID"]}&t=#{env["SYSTEM_TASKINSTANCEID"]}"
end

branch, tag = branch_or_tag(
env["SYSTEM_PULLREQUEST_SOURCEBRANCH"] || env["BUILD_SOURCEBRANCH"] || env["BUILD_SOURCEBRANCHNAME"]
)

{
TAG_PROVIDER_NAME => "azurepipelines",
TAG_WORKSPACE_PATH => env["BUILD_SOURCESDIRECTORY"],
TAG_PIPELINE_ID => build_id,
TAG_PIPELINE_NAME => env["BUILD_DEFINITIONNAME"],
TAG_PIPELINE_NUMBER => build_id,
TAG_PIPELINE_URL => pipeline_url,
TAG_JOB_URL => job_url,
TAG_STAGE_NAME => env["SYSTEM_STAGEDISPLAYNAME"],
TAG_JOB_NAME => env["SYSTEM_JOBDISPLAYNAME"],
Git::TAG_REPOSITORY_URL =>
env["SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI"] || env["BUILD_REPOSITORY_URI"],
Git::TAG_COMMIT_SHA => env["SYSTEM_PULLREQUEST_SOURCECOMMITID"] \
|| env["BUILD_SOURCEVERSION"],
Git::TAG_BRANCH => branch,
Git::TAG_TAG => tag,
Git::TAG_COMMIT_AUTHOR_NAME => env["BUILD_REQUESTEDFORID"],
Git::TAG_COMMIT_AUTHOR_EMAIL => env["BUILD_REQUESTEDFOREMAIL"],
Git::TAG_COMMIT_MESSAGE => env["BUILD_SOURCEVERSIONMESSAGE"],
TAG_CI_ENV_VARS => {
"SYSTEM_TEAMPROJECTID" => env["SYSTEM_TEAMPROJECTID"],
"BUILD_BUILDID" => env["BUILD_BUILDID"],
"SYSTEM_JOBID" => env["SYSTEM_JOBID"]
}.to_json
}
end

def extract_bitbucket(env)
pipeline_url = "https://bitbucket.org/#{env["BITBUCKET_REPO_FULL_NAME"]}/addon/pipelines/home#" \
"!/results/#{env["BITBUCKET_BUILD_NUMBER"]}"
Expand Down
21 changes: 0 additions & 21 deletions lib/datadog/ci/ext/providers.rb

This file was deleted.

8 changes: 6 additions & 2 deletions lib/datadog/ci/ext/providers/appveyor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ module Datadog
module CI
module Ext
module Providers
# TODO
# 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
Expand Down Expand Up @@ -79,14 +81,16 @@ def git_commit_message
commit_message
end

# appveyor-specific logic

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

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

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

require_relative "extractor"

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

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
42 changes: 28 additions & 14 deletions lib/datadog/ci/ext/providers/extractor.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# frozen_string_literal: true

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

module Datadog
module CI
module Ext
module Providers
# 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
# 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"

EXTRACTORS = [
["APPVEYOR", Appveyor]
["APPVEYOR", Appveyor],
["TF_BUILD", Azure]
]

def self.for_environment(env)
Expand All @@ -30,18 +33,18 @@ def initialize(env)

def tags
{
Providers::TAG_JOB_NAME => job_name,
Providers::TAG_JOB_URL => job_url,
Providers::TAG_PIPELINE_ID => pipeline_id,
Providers::TAG_PIPELINE_NAME => pipeline_name,
Providers::TAG_PIPELINE_NUMBER => pipeline_number,
Providers::TAG_PIPELINE_URL => pipeline_url,
Providers::TAG_PROVIDER_NAME => provider_name,
Providers::TAG_STAGE_NAME => stage_name,
Providers::TAG_WORKSPACE_PATH => workspace_path,
Providers::TAG_NODE_LABELS => node_labels,
Providers::TAG_NODE_NAME => node_name,
Providers::TAG_CI_ENV_VARS => ci_env_vars,
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,
Expand Down Expand Up @@ -129,6 +132,17 @@ def git_commit_message

def git_commit_sha
end

def branch_or_tag(branch_or_tag_string)
@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
end
end
end
Expand Down
1 change: 1 addition & 0 deletions sig/datadog/ci/ext/providers/appveyor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Datadog
private

@github_repo_provider: bool
@url: String

def provider_name: () -> "appveyor"

Expand Down
58 changes: 58 additions & 0 deletions sig/datadog/ci/ext/providers/azure.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Datadog
module CI
module Ext
module Providers
class Azure < Extractor
private

@pipeline_url: String
@job_url: String

def provider_name: () -> "azurepipelines"

def pipeline_url: () -> String?

def job_url: () -> String?

def workspace_path: () -> String?

def pipeline_id: () -> String?

def pipeline_number: () -> String?

def pipeline_name: () -> String?

def stage_name: () -> String?

def job_name: () -> 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?

def build_id: () -> String?

def team_foundation_server_uri: () -> String?

def team_project_id: () -> String?

def url_defined?: () -> bool

def set_branch_and_tag: () -> [String?, String?]
end
end
end
end
end
7 changes: 6 additions & 1 deletion sig/datadog/ci/ext/providers/extractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module Datadog
module Ext
module Providers
class Extractor
EXTRACTORS: ::Array[::Array["APPVEYOR" | untyped]]

EXTRACTORS: ::Array[::Array[String | untyped]]
@branch: String?
@tag: String?

def self.for_environment: (untyped env) -> Extractor

Expand Down Expand Up @@ -60,6 +63,8 @@ module Datadog
def git_commit_message: () -> nil

def git_commit_sha: () -> nil

def branch_or_tag: (String? branch_or_tag_string) -> [String?, String?]
end
end
end
Expand Down

0 comments on commit 895ee04

Please sign in to comment.