Skip to content

Commit

Permalink
supply provider class to Extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 6, 2023
1 parent b600726 commit 0837f35
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 12 deletions.
6 changes: 2 additions & 4 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

require_relative "git"
require_relative "environment/extractor"
require_relative "environment/providers/local_git"
require_relative "environment/providers/user_defined_tags"

module Datadog
module CI
Expand Down Expand Up @@ -33,11 +31,11 @@ def tags(env)

# If user defined metadata is defined, overwrite
tags.merge!(
Environment::Extractor.new(env, provider: Providers::UserDefinedTags.new(env)).tags
Environment::Extractor.new(env, provider_klass: Providers::UserDefinedTags).tags
)

# Fill out tags from local git as fallback
local_git_tags = Environment::Extractor.new(env, provider: Providers::LocalGit.new(env)).tags
local_git_tags = Environment::Extractor.new(env, provider_klass: Providers::LocalGit).tags
local_git_tags.each do |key, value|
tags[key] ||= value
end
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module Environment
# 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
def initialize(env, provider: nil)
def initialize(env, provider_klass: nil)
@env = env
@provider = provider || Providers.for_environment(env)
@provider = provider_klass ? provider_klass.new(env) : Providers.for_environment(env)
end

def tags
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/ci/ext/environment/providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
require_relative "providers/teamcity"
require_relative "providers/travis"

require_relative "providers/local_git"
require_relative "providers/user_defined_tags"

module Datadog
module CI
module Ext
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/ext/environment/extractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Datadog
@provider: Providers::Base
@tags: Hash[String, untyped]

def initialize: (Hash[String, String?] env, ?provider: Providers::Base?) -> void
def initialize: (Hash[String, String?] env, ?provider_klass: singleton(Providers::Base)?) -> void

def tags: () -> Hash[String, untyped]

Expand Down
6 changes: 1 addition & 5 deletions spec/support/provider_test_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
shared_context "extract tags from environment with given provider and use a subject" do |git_fixture|
let(:provider) do
described_class.new(env)
end

subject(:extracted_tags) do
ClimateControl.modify(environment_variables) do
::Datadog::CI::Ext::Environment::Extractor.new(env, provider: provider).tags
::Datadog::CI::Ext::Environment::Extractor.new(env, provider_klass: described_class).tags
end
end

Expand Down

0 comments on commit 0837f35

Please sign in to comment.