Module: Mergify::RSpec::Resources::GitHubActions

Defined in:
lib/mergify/rspec/resources/github_actions.rb

Overview

Detects OpenTelemetry Resource attributes for GitHub Actions.

Constant Summary collapse

GHA_MAPPING =
{
  'cicd.pipeline.name' => [:to_s, 'GITHUB_WORKFLOW'],
  'cicd.pipeline.task.name' => [:to_s, 'GITHUB_JOB'],
  'cicd.pipeline.run.id' => [:to_i, 'GITHUB_RUN_ID'],
  'cicd.pipeline.run.attempt' => [:to_i, 'GITHUB_RUN_ATTEMPT'],
  'cicd.pipeline.runner.name' => [:to_s, 'RUNNER_NAME'],
  'vcs.ref.head.name' => [:to_s, -> { head_ref_name }],
  'vcs.ref.head.type' => [:to_s, 'GITHUB_REF_TYPE'],
  'vcs.ref.base.name' => [:to_s, 'GITHUB_BASE_REF'],
  'vcs.repository.name' => [:to_s, 'GITHUB_REPOSITORY'],
  'vcs.repository.id' => [:to_i, 'GITHUB_REPOSITORY_ID'],
  'vcs.repository.url.full' => [:to_s, -> { repository_url }],
  'vcs.ref.head.revision' => [:to_s, -> { head_sha }]
}.freeze

Class Method Summary collapse

Class Method Details

.detectObject



14
15
16
17
18
19
# File 'lib/mergify/rspec/resources/github_actions.rb', line 14

def detect
  return OpenTelemetry::SDK::Resources::Resource.create({}) if Utils.ci_provider != :github_actions

  attributes = Utils.get_attributes(GHA_MAPPING)
  OpenTelemetry::SDK::Resources::Resource.create(attributes)
end

.head_ref_nameObject



36
37
38
39
# File 'lib/mergify/rspec/resources/github_actions.rb', line 36

def head_ref_name
  ref = ENV.fetch('GITHUB_HEAD_REF', '')
  ref.empty? ? ENV.fetch('GITHUB_REF_NAME', nil) : ref
end

.head_shaObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/mergify/rspec/resources/github_actions.rb', line 47

def head_sha
  if ENV.fetch('GITHUB_EVENT_NAME', nil) == 'pull_request'
    event_path = ENV.fetch('GITHUB_EVENT_PATH', nil)
    if event_path && File.file?(event_path)
      event = JSON.parse(File.read(event_path))
      return event.dig('pull_request', 'head', 'sha').to_s
    end
  end
  ENV.fetch('GITHUB_SHA', nil)
end

.repository_urlObject



41
42
43
44
45
# File 'lib/mergify/rspec/resources/github_actions.rb', line 41

def repository_url
  server = ENV.fetch('GITHUB_SERVER_URL', nil)
  repo = ENV.fetch('GITHUB_REPOSITORY', nil)
  "#{server}/#{repo}" if server && repo
end