Class: Henitai::Reporter::DashboardMetadataProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/henitai/reporter/dashboard_metadata_provider.rb,
sig/henitai.rbs

Overview

Resolves the Stryker Dashboard project/version/api-key coordinates for Dashboard. Isolated behind injectable +env+/+git_executor+ seams so callers (and specs) never have to touch real ENV or shell out to git.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dashboard_config:, env: ENV, git_executor: Open3) ⇒ DashboardMetadataProvider

Returns a new instance of DashboardMetadataProvider.

Parameters:

  • dashboard_config: (Hash[Symbol, untyped], nil)
  • env: (Object) (defaults to: ENV)
  • git_executor: (Object) (defaults to: Open3)


12
13
14
15
16
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 12

def initialize(dashboard_config:, env: ENV, git_executor: Open3)
  @dashboard_config = dashboard_config || {}
  @env = env
  @git_executor = git_executor
end

Class Method Details

.normalize_git_url(url) ⇒ String?

Parameters:

  • (String, nil)

Returns:

  • (String, nil)


43
44
45
46
47
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 43

def normalize_git_url(url)
  return nil if url.nil? || url.strip.empty?

  url.strip.sub(/\.git\z/, "")
end

.project_from_git_url(url) ⇒ String?

Parameters:

  • (String, nil)

Returns:

  • (String, nil)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 31

def project_from_git_url(url)
  normalized = normalize_git_url(url)
  return nil if normalized.nil?

  return project_from_uri_url(normalized) if normalized.include?("://")
  return project_from_ssh_url(normalized) if normalized.include?("@")

  normalized
rescue URI::InvalidURIError
  nil
end

.project_from_ssh_url(normalized) ⇒ String?

Parameters:

  • (String)

Returns:

  • (String, nil)


55
56
57
58
59
60
61
62
63
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 55

def project_from_ssh_url(normalized)
  _, host_and_path = normalized.split("@", 2)
  return nil if host_and_path.nil?

  host, path = host_and_path.split(":", 2)
  return nil unless host && path

  "#{host}/#{path}"
end

.project_from_uri_url(normalized) ⇒ String?

Parameters:

  • (String)

Returns:

  • (String, nil)


49
50
51
52
53
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 49

def project_from_uri_url(normalized)
  uri = URI.parse(normalized)
  path = uri.path.to_s.sub(%r{^/}, "")
  [uri.host, path].compact.reject(&:empty?).join("/")
end

Instance Method Details

#api_keyString?

Returns:

  • (String, nil)


26
27
28
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 26

def api_key
  env.fetch("STRYKER_DASHBOARD_API_KEY", nil)
end

#blank?(value) ⇒ Boolean

Parameters:

  • (String, nil)

Returns:

  • (Boolean)


108
109
110
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 108

def blank?(value)
  value.nil? || value.strip.empty?
end

#env_versionString?

Returns:

  • (String, nil)


70
71
72
73
74
75
76
77
78
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 70

def env_version
  ref_name = env.fetch("GITHUB_REF_NAME", nil)
  return ref_name unless blank?(ref_name)

  ref = env.fetch("GITHUB_REF", nil)
  return ref_without_prefix(ref) unless ref.nil? || blank?(ref)

  env.fetch("GITHUB_SHA", nil)
end

#git_branch_nameString?

Returns:

  • (String, nil)


99
100
101
102
103
104
105
106
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 99

def git_branch_name
  stdout, status = git_executor.capture2("git", "rev-parse", "--abbrev-ref", "HEAD")
  return stdout.strip if status.success? && !stdout.strip.empty?

  nil
rescue Errno::ENOENT
  nil
end

#git_remote_urlString?

Returns:

  • (String, nil)


90
91
92
93
94
95
96
97
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 90

def git_remote_url
  stdout, status = git_executor.capture2("git", "remote", "get-url", "origin")
  return stdout.strip if status.success?

  nil
rescue Errno::ENOENT
  nil
end

#projectString?

Returns:

  • (String, nil)


18
19
20
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 18

def project
  @project ||= dashboard_config[:project] || project_from_git_remote
end

#project_from_git_remoteString?

Returns:

  • (String, nil)


86
87
88
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 86

def project_from_git_remote
  self.class.project_from_git_url(git_remote_url)
end

#ref_without_prefix(ref) ⇒ String?

Parameters:

  • (String, nil)

Returns:

  • (String, nil)


80
81
82
83
84
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 80

def ref_without_prefix(ref)
  return nil if blank?(ref)

  ref.to_s.sub(%r{^refs/(heads|tags|pull)/}, "")
end

#versionString?

Returns:

  • (String, nil)


22
23
24
# File 'lib/henitai/reporter/dashboard_metadata_provider.rb', line 22

def version
  @version ||= env_version || git_branch_name
end