Skip to content

Commit

Permalink
extract service name logic
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jan 16, 2024
1 parent f8e5691 commit cc7ae6d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/cucumber/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require_relative "../ext"
require_relative "../../settings"
require_relative "../../../utils/git"
require_relative "../../../utils/configuration"

module Datadog
module CI
Expand All @@ -23,7 +23,7 @@ class Settings < Datadog::CI::Contrib::Settings
option :service_name do |o|
o.type :string
o.default do
Datadog.configuration.service_without_fallback || Utils::Git.repository_name || Ext::DEFAULT_SERVICE_NAME
Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/minitest/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative "../ext"
require_relative "../../settings"
require_relative "../../../utils/git"
require_relative "../../../utils/configuration"

module Datadog
module CI
Expand All @@ -21,7 +21,7 @@ class Settings < Datadog::CI::Contrib::Settings
option :service_name do |o|
o.type :string
o.default do
Datadog.configuration.service_without_fallback || Utils::Git.repository_name || Ext::DEFAULT_SERVICE_NAME
Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/rspec/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative "../ext"
require_relative "../../settings"
require_relative "../../../utils/git"
require_relative "../../../utils/configuration"

module Datadog
module CI
Expand All @@ -21,7 +21,7 @@ class Settings < Datadog::CI::Contrib::Settings
option :service_name do |o|
o.type :string
o.default do
Datadog.configuration.service_without_fallback || Utils::Git.repository_name || Ext::DEFAULT_SERVICE_NAME
Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
end
end

Expand Down
15 changes: 15 additions & 0 deletions lib/datadog/ci/utils/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require_relative "git"

module Datadog
module CI
module Utils
module Configuration
def self.fetch_service_name(default)
Datadog.configuration.service_without_fallback || Git.repository_name || default
end
end
end
end
end
9 changes: 9 additions & 0 deletions sig/datadog/ci/utils/configuration.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Datadog
module CI
module Utils
module Configuration
def self.fetch_service_name: (String default) -> String
end
end
end
end
37 changes: 37 additions & 0 deletions spec/datadog/ci/utils/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec.describe ::Datadog::CI::Utils::Configuration do
describe ".fetch_service_name" do
subject { described_class.fetch_service_name(default) }

let(:default) { "default" }

before do
allow(::Datadog.configuration).to receive(:service_without_fallback).and_return(service)
end

context "when service is set in Datadog config" do
let(:service) { "service_without_fallback" }

it { is_expected.to eq(service) }
end

context "when service is not set" do
let(:service) { nil }

before do
expect(::Datadog::CI::Utils::Git).to receive(:repository_name).and_return(repository_name)
end

context "when repository_name can be fetched" do
let(:repository_name) { "repository_name" }

it { is_expected.to eq(repository_name) }
end

context "when repository_name can not be fetched" do
let(:repository_name) { nil }

it { is_expected.to eq(default) }
end
end
end
end

0 comments on commit cc7ae6d

Please sign in to comment.