Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIVIS-3172] use repository name as default test service name #104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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