Module: GithubBot::Github::Payload

Included in:
Client
Defined in:
lib/github_bot/github/payload.rb

Overview

The GitHub webhook payload information

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



201
202
203
# File 'lib/github_bot/github/payload.rb', line 201

def method_missing(method_name, *args)
  payload[method_name] || super
end

Instance Method Details

#base_branchString

Public: Returns the base branch that the head was based on

Returns:

  • (String)

    The base branch that the head was based on



103
104
105
# File 'lib/github_bot/github/payload.rb', line 103

def base_branch
  pull_request[:base][:ref]
end

#check_run?Boolean

Public: Return <true> if the payload event type is of type ‘check_run’; otherwise, <false>

Returns:

  • (Boolean)


25
26
27
# File 'lib/github_bot/github/payload.rb', line 25

def check_run?
  payload_type == 'check_run'
end

#head_branchString

Public: Returns the head branch that the changes are on

Returns:

  • (String)

    The head branch that the changes are on



96
97
98
# File 'lib/github_bot/github/payload.rb', line 96

def head_branch
  pull_request[:head][:ref]
end

#head_shaString

Public: Returns the SHA of the most recent commit for this pull request

Returns:

  • (String)

    The SHA of the most recent commit for this pull request



85
86
87
88
89
90
91
# File 'lib/github_bot/github/payload.rb', line 85

def head_sha
  if issue_comment?
    'HEAD'
  else
    pull_request[:head][:sha]
  end
end

#installation_idInteger

Public: Returns the installation identifier associated to the event

Returns:

  • (Integer)

    identifier of the GitHub App installation



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

def installation_id
  payload[:installation][:id]
end

#issue_comment?Boolean

Public: Return <true> if the action type is of type ‘issue_comment’; otherwise, <false> This is used for all other comment triggered issue_comment events

Returns:

  • (Boolean)


41
42
43
# File 'lib/github_bot/github/payload.rb', line 41

def issue_comment?
  payload_type == 'issue_comment'
end

#labeled?Boolean

Public: Return <true> if the payload event type is of type ‘labeled’; otherwise, <false>

Returns:

  • (Boolean)


46
47
48
# File 'lib/github_bot/github/payload.rb', line 46

def labeled?
  payload_type == 'labeled'
end

#pull_requestHash

Public: Return the activity related to the pull request

Returns:

  • (Hash)

    The activity related to the pull request



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/github_bot/github/payload.rb', line 58

def pull_request
  if pull_request?
    payload[:pull_request]
  elsif check_run?
    payload[:check_run][:pull_requests].first
  elsif issue_comment?
    payload[:issue]
  else
    payload.key?(:pull_request) ? payload[:pull_request] : {}
  end
end

#pull_request?Boolean

Public: Return <true> if the payload event type is of type ‘pull_request’; otherwise, <false>

Returns:

  • (Boolean)


20
21
22
# File 'lib/github_bot/github/payload.rb', line 20

def pull_request?
  payload_type == 'pull_request'
end

#pull_request_bodyString

Public: Returns the pull request body content

Returns:

  • (String)

    The pull request body content



110
111
112
# File 'lib/github_bot/github/payload.rb', line 110

def pull_request_body
  pull_request[:body]
end

#pull_request_numberInteger

Public: Returns the pull request number from the payload

Returns:

  • (Integer)

    The pull request number from the payload



78
79
80
# File 'lib/github_bot/github/payload.rb', line 78

def pull_request_number
  pull_request[:number]
end

#repository_clone_urlString

Public: Returns the repository URL utilized for performing a ‘git clone’

Returns:

  • (String)

    The repository URL utilized for performing a ‘git clone’



131
132
133
# File 'lib/github_bot/github/payload.rb', line 131

def repository_clone_url
  repository[:clone_url]
end

#repository_default_branchString

Public: Returns the repository default branch

Returns:

  • (String)

    The repository default branch



174
175
176
# File 'lib/github_bot/github/payload.rb', line 174

def repository_default_branch
  repository[:default_branch]
end

#repository_fork_urlsArray

Public: Returns the repository fork URL from the original project with the most recent updated forked instance first

Utilizing API: docs.github.com/en/rest/reference/repos#forks Example: api.github.com/repos/octocat/Hello-World/forks?page=1

Returns:

  • (Array)

    The array of [String] URLs associated to the forked repositories



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/github_bot/github/payload.rb', line 142

def repository_fork_urls
  return @repository_fork_urls if @repository_fork_urls

  @repository_fork_urls =
    [].tap do |ar|
      # iterate over pages of forks
      page_count = 1
      forks_url = repository[:forks_url]
      loop do
        uri = URI.parse(forks_url)
        new_query_ar = URI.decode_www_form(String(uri.query)) << ['page', page_count]
        uri.query = URI.encode_www_form(new_query_ar)

        Rails.logger.info "#{self.class}##{__method__} retrieving #{uri}"

        json = uri.open.read
        json_response = JSON.parse(json)
        break if json_response.empty?

        # iterate over each fork and capture the clone_url
        json_response.each do |fork|
          ar << fork['clone_url']
        end

        page_count += 1
      end
    end
end

#repository_full_nameString

Public: Returns the organization and repository name

Returns:

  • (String)

    The organization and repository name



124
125
126
# File 'lib/github_bot/github/payload.rb', line 124

def repository_full_name
  repository[:full_name]
end

#repository_nameString

Public: Returns the name of the repository where the event was triggered

Returns:

  • (String)

    The name of the repository where the event was triggered



117
118
119
# File 'lib/github_bot/github/payload.rb', line 117

def repository_name
  repository[:name]
end

#repository_pull_request_botsArray<String>

Public: Returns a list of class object names to create for validation

Returns:

  • (Array<String>)

    A list of strings associated to the class object validators



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/github_bot/github/payload.rb', line 181

def repository_pull_request_bots
  return @repository_pull_request_bots if @repository_pull_request_bots

  file = raw_file_url('.github-bots')
  @repository_pull_request_bots = [].tap do |ar|
    if file
      resp = YAML.safe_load(URI.parse(file).open.read)
      ar << resp['pull_request'] if resp['pull_request']
    end
  end.flatten
rescue SyntaxError => e
  Rails.logger.error message: "Error parsing file '#{file}'", exception: e

  # Allow continuation of process just won't utilize any bot validations until
  # parsing error of file is corrected
  {}
end

#review_request_removed?Boolean

Public: Return <true> if the payload event type is of type ‘review_request_removed’; otherwise, <false>

Returns:

  • (Boolean)


35
36
37
# File 'lib/github_bot/github/payload.rb', line 35

def review_request_removed?
  payload_type == 'review_request_removed'
end

#review_requested?Boolean

Public: Return <true> if the payload event type is of type ‘review_requested’; otherwise, <false>

Returns:

  • (Boolean)


30
31
32
# File 'lib/github_bot/github/payload.rb', line 30

def review_requested?
  payload_type == 'review_requested'
end

#sender_type_bot?Boolean

Public: Returns <true> if the sender is of type ‘Bot’; otherwise, <false>

Returns:

  • (Boolean)


71
72
73
# File 'lib/github_bot/github/payload.rb', line 71

def sender_type_bot?
  payload[:sender][:type].downcase == 'bot' # rubocop:disable Performance/Casecmp
end

#unlabeled?Boolean

Public: Return <true> if the payload event type is of type ‘unlabeled’; otherwise, <false>

Returns:

  • (Boolean)


51
52
53
# File 'lib/github_bot/github/payload.rb', line 51

def unlabeled?
  payload_type == 'unlabeled'
end