Skip to content

Commit

Permalink
Merge pull request #52 from DataDog/anmarchenko/refactor_env_providers
Browse files Browse the repository at this point in the history
[CIVIS-175] refactor env providers to support custom logic to determine which env they handle
  • Loading branch information
anmarchenko committed Oct 19, 2023
2 parents 9b83a30 + 1afdbdd commit 5707183
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 15 deletions.
28 changes: 14 additions & 14 deletions lib/datadog/ci/ext/environment/providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ module Ext
module Environment
module Providers
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]
Providers::Appveyor,
Providers::Azure,
Providers::Bitbucket,
Providers::Bitrise,
Providers::Buddy,
Providers::Buildkite,
Providers::Circleci,
Providers::Codefresh,
Providers::GithubActions,
Providers::Gitlab,
Providers::Jenkins,
Providers::Teamcity,
Providers::Travis
]

def self.for_environment(env)
_, provider_klass = PROVIDERS.find { |provider_env_var, _| env.key?(provider_env_var) }
provider_klass = PROVIDERS.find { |klass| klass.handles?(env) }
provider_klass = Providers::Base if provider_klass.nil?

provider_klass.new(env)
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/appveyor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Providers
# Appveyor: https://www.appveyor.com/
# Environment variables docs: https://www.appveyor.com/docs/environment-variables/
class Appveyor < Base
def self.handles?(env)
env.key?("APPVEYOR")
end

def provider_name
"appveyor"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ 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 < Base
def self.handles?(env)
env.key?("TF_BUILD")
end

def provider_name
"azurepipelines"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module Providers
class Base
attr_reader :env

def self.handles?(_env)
false
end

def initialize(env)
@env = env
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/bitbucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ 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 < Base
def self.handles?(env)
env.key?("BITBUCKET_COMMIT")
end

# overridden methods
def provider_name
"bitbucket"
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/bitrise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Providers
# Bitrise: https://bitrise.io/
# Environment variables docs: https://devcenter.bitrise.io/en/references/available-environment-variables.html
class Bitrise < Base
def self.handles?(env)
env.key?("BITRISE_BUILD_SLUG")
end

def provider_name
"bitrise"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/buddy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Providers
# Buddy: https://buddy.works/
# Environment variables docs: https://buddy.works/docs/pipelines/environment-variables
class Buddy < Base
def self.handles?(env)
env.key?("BUDDY")
end

def provider_name
"buddy"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/buildkite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Providers
# Buildkite: https://buildkite.com/
# Environment variables docs: https://buildkite.com/docs/pipelines/environment-variables
class Buildkite < Base
def self.handles?(env)
env.key?("BUILDKITE")
end

def provider_name
"buildkite"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/circleci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Providers
# Circle CI: https://circleci.com/
# Environment variables docs: https://circleci.com/docs/variables/#built-in-environment-variables
class Circleci < Base
def self.handles?(env)
env.key?("CIRCLECI")
end

def provider_name
"circleci"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/codefresh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ 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 < Base
def self.handles?(env)
env.key?("CF_BUILD_ID")
end

def provider_name
"codefresh"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/github_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Providers
# Github Actions: https://github.com/features/actions
# Environment variables docs: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
class GithubActions < Base
def self.handles?(env)
env.key?("GITHUB_SHA")
end

def provider_name
"github"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Providers
# Gitlab CI: https://docs.gitlab.com/ee/ci/
# Environment variables docs: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
class Gitlab < Base
def self.handles?(env)
env.key?("GITLAB_CI")
end

def provider_name
"gitlab"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/jenkins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module Providers
# Jenkins: https://www.jenkins.io/
# Environment variables docs: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
class Jenkins < Base
def self.handles?(env)
env.key?("JENKINS_URL")
end

def provider_name
"jenkins"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/teamcity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Providers
# Teamcity: https://www.jetbrains.com/teamcity/
# Environment variables docs: https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html
class Teamcity < Base
def self.handles?(env)
env.key?("TEAMCITY_VERSION")
end

def provider_name
"teamcity"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/travis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Providers
# Travis CI: https://www.travis-ci.com/
# Environment variables docs: https://docs.travis-ci.com/user/environment-variables#default-environment-variables
class Travis < Base
def self.handles?(env)
env.key?("TRAVIS")
end

def provider_name
"travisci"
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/ext/environment/providers.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Datadog
module Ext
module Environment
module Providers
PROVIDERS: ::Array[[String, untyped]]
PROVIDERS: ::Array[untyped]

def self.for_environment: (Hash[String, String?] env) -> Providers::Base
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/ext/environment/providers/base.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Datadog
@branch: String?
@tag: String?

def self.handles?: (Hash[String, String?] env) -> bool

def initialize: (Hash[String, String?] env) -> void

def job_name: () -> nil
Expand Down

0 comments on commit 5707183

Please sign in to comment.