Skip to content

Commit

Permalink
extract buildkite 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 d01f763 commit ee39589
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 3 deletions.
1 change: 0 additions & 1 deletion 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 = [
["BUILDKITE", :extract_buildkite],
["CIRCLECI", :extract_circle_ci],
["GITHUB_SHA", :extract_github_actions],
["GITLAB_CI", :extract_gitlab],
Expand Down
12 changes: 10 additions & 2 deletions lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class Extractor
require_relative "providers/azure"
require_relative "providers/bitbucket"
require_relative "providers/buddy"
require_relative "providers/buildkite"

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

def self.for_environment(env)
Expand Down Expand Up @@ -61,7 +63,13 @@ def tags
Git::TAG_COMMIT_COMMITTER_NAME => git_commit_committer_name,
Git::TAG_COMMIT_MESSAGE => git_commit_message,
Git::TAG_COMMIT_SHA => git_commit_sha
}.reject { |_, v| v.nil? }
}.reject do |_, v|
# setting type of v here to untyped because steep does not
# understand `v.nil? || something`

# @type var v: untyped
v.nil? || v.strip.empty?
end
end

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

require_relative "../extractor"

module Datadog
module CI
module Ext
module Environment
module Providers
# Buildkite: https://buildkite.com/
# Environment variables docs: https://buildkite.com/docs/pipelines/environment-variables
class Buildkite < Extractor
private

# overridden methods
def provider_name
"buildkite"
end

def job_url
"#{env["BUILDKITE_BUILD_URL"]}##{env["BUILDKITE_JOB_ID"]}"
end

def pipeline_id
env["BUILDKITE_BUILD_ID"]
end

def pipeline_name
env["BUILDKITE_PIPELINE_SLUG"]
end

def pipeline_number
env["BUILDKITE_BUILD_NUMBER"]
end

def pipeline_url
env["BUILDKITE_BUILD_URL"]
end

def node_name
env["BUILDKITE_AGENT_ID"]
end

def node_labels
labels = env
.select { |key| key.start_with?("BUILDKITE_AGENT_META_DATA_") }
.map { |key, value| "#{key.to_s.sub("BUILDKITE_AGENT_META_DATA_", "").downcase}:#{value}" }
.sort_by(&:length)

labels.to_json unless labels.empty?
end

def workspace_path
env["BUILDKITE_BUILD_CHECKOUT_PATH"]
end

def git_repository_url
env["BUILDKITE_REPO"]
end

def git_commit_sha
env["BUILDKITE_COMMIT"]
end

def git_branch
env["BUILDKITE_BRANCH"]
end

def git_tag
env["BUILDKITE_TAG"]
end

def git_commit_author_name
env["BUILDKITE_BUILD_AUTHOR"]
end

def git_commit_author_email
env["BUILDKITE_BUILD_AUTHOR_EMAIL"]
end

def git_commit_message
env["BUILDKITE_MESSAGE"]
end

def ci_env_vars
{
"BUILDKITE_BUILD_ID" => env["BUILDKITE_BUILD_ID"],
"BUILDKITE_JOB_ID" => env["BUILDKITE_JOB_ID"]
}.to_json
end

# buildkite-specific methods

end
end
end
end
end
end
46 changes: 46 additions & 0 deletions sig/datadog/ci/ext/environment/providers/buildkite.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Datadog
module CI
module Ext
module Environment
module Providers
class Buildkite < Extractor
private
def provider_name: () -> "buildkite"

def job_url: () -> ::String

def pipeline_id: () -> String?

def pipeline_name: () -> String?

def pipeline_number: () -> String?

def pipeline_url: () -> String?

def node_name: () -> String?

def node_labels: () -> 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 ee39589

Please sign in to comment.