Module: EmergeCLI::Github

Defined in:
lib/utils/github.rb

Constant Summary collapse

GITHUB_EVENT_PR =
'pull_request'.freeze
GITHUB_EVENT_PUSH =
'push'.freeze

Class Method Summary collapse

Class Method Details

.base_shaObject



31
32
33
34
# File 'lib/utils/github.rb', line 31

def self.base_sha
  return unless pull_request?
  github_event_data.dig(:pull_request, :base, :sha)
end

.branchObject



40
41
42
# File 'lib/utils/github.rb', line 40

def self.branch
  pull_request? ? github_event_data.dig(:pull_request, :head, :ref) : Git.branch
end

.event_nameObject



6
7
8
# File 'lib/utils/github.rb', line 6

def self.event_name
  ENV.fetch('GITHUB_EVENT_NAME', nil)
end

.github_event_dataObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/utils/github.rb', line 56

def self.github_event_data
  @github_event_data ||= begin
    github_event_path = ENV.fetch('GITHUB_EVENT_PATH', nil)
    Logger.error 'GITHUB_EVENT_PATH is not set' if github_event_path.nil?

    Logger.error "File #{github_event_path} doesn't exist" unless File.exist?(github_event_path)

    file_content = File.read(github_event_path)
    file_json = JSON.parse(file_content, symbolize_names: true)
    Logger.debug "Parsed GitHub event data: #{file_json.inspect}"

    file_json
  end
end

.pr_numberObject



36
37
38
# File 'lib/utils/github.rb', line 36

def self.pr_number
  pull_request? ? github_event_data[:number] : nil
end

.previous_shaObject



52
53
54
# File 'lib/utils/github.rb', line 52

def self.previous_sha
  Git.previous_sha
end

.pull_request?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/utils/github.rb', line 15

def self.pull_request?
  event_name == GITHUB_EVENT_PR
end

.push?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/utils/github.rb', line 19

def self.push?
  event_name == GITHUB_EVENT_PUSH
end

.repo_nameObject



48
49
50
# File 'lib/utils/github.rb', line 48

def self.repo_name
  github_event_data.dig(:repository, :full_name)
end

.repo_ownerObject



44
45
46
# File 'lib/utils/github.rb', line 44

def self.repo_owner
  github_event_data.dig(:repository, :owner, :login)
end

.shaObject



23
24
25
26
27
28
29
# File 'lib/utils/github.rb', line 23

def self.sha
  if push?
    ENV.fetch('GITHUB_SHA', nil)
  elsif pull_request?
    github_event_data.dig(:pull_request, :head, :sha)
  end
end

.supported_github_event?Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/utils/github.rb', line 10

def self.supported_github_event?
  Logger.info "GitHub event name: #{event_name}"
  pull_request? || push?
end