Skip to content

Commit

Permalink
Merge pull request #104 from DataDog/anmarchenko/use_repository_name_…
Browse files Browse the repository at this point in the history
…as_default_service_name

[CIVIS-3172] use repository name as default test service name
  • Loading branch information
anmarchenko authored Jan 16, 2024
2 parents cff4f36 + cc7ae6d commit eb4dfad
Show file tree
Hide file tree
Showing 10 changed files with 650 additions and 492 deletions.
5 changes: 4 additions & 1 deletion lib/datadog/ci/contrib/cucumber/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

module Datadog
module CI
Expand All @@ -21,7 +22,9 @@ class Settings < Datadog::CI::Contrib::Settings

option :service_name do |o|
o.type :string
o.default { Datadog.configuration.service_without_fallback || Ext::DEFAULT_SERVICE_NAME }
o.default do
Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
end
end

# @deprecated Will be removed in 1.0
Expand Down
5 changes: 4 additions & 1 deletion lib/datadog/ci/contrib/minitest/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

module Datadog
module CI
Expand All @@ -19,7 +20,9 @@ class Settings < Datadog::CI::Contrib::Settings

option :service_name do |o|
o.type :string
o.default { Datadog.configuration.service_without_fallback || Ext::DEFAULT_SERVICE_NAME }
o.default do
Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
end
end

# @deprecated Will be removed in 1.0
Expand Down
5 changes: 4 additions & 1 deletion lib/datadog/ci/contrib/rspec/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

module Datadog
module CI
Expand All @@ -19,7 +20,9 @@ class Settings < Datadog::CI::Contrib::Settings

option :service_name do |o|
o.type :string
o.default { Datadog.configuration.service_without_fallback || Ext::DEFAULT_SERVICE_NAME }
o.default do
Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
end
end

# @deprecated Will be removed in 1.0
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
25 changes: 25 additions & 0 deletions lib/datadog/ci/utils/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ def self.relative_to_root(path)
path.relative_path_from(git_root).to_s
end

def self.repository_name
return @@repository_name if defined?(@@repository_name)

git_remote_url = exec_git_command("git ls-remote --get-url origin")

# return git repository name from remote url without .git extension
last_path_segment = git_remote_url.split("/").last if git_remote_url
@@repository_name = last_path_segment.gsub(".git", "") if last_path_segment
@@repository_name ||= current_folder_name
rescue => e
Datadog.logger.debug(
"Unable to get git remote: #{e.class.name} #{e.message} at #{Array(e.backtrace).first}"
)
@@repository_name = current_folder_name
end

def self.current_folder_name
root_folder = root
if root_folder.nil?
File.basename(Dir.pwd)
else
File.basename(root_folder)
end
end

def self.exec_git_command(cmd)
out, status = Open3.capture2e(cmd)

Expand Down
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
4 changes: 4 additions & 0 deletions sig/datadog/ci/utils/git.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Datadog

def self.root: -> String?

def self.repository_name: -> String

def self.current_folder_name: -> String

def self.relative_to_root: (String? path) -> String?
end
end
Expand Down
Loading

0 comments on commit eb4dfad

Please sign in to comment.