module GithubBot::Github::Payload
The GitHub webhook payload information @see developer.github.com/webhooks/event-payloads/#webhook-payload-object-common-properties
Public Instance Methods
Public: Returns the base branch that the head was based on
@return [String] The base branch that the head was based on
# File lib/github_bot/github/payload.rb, line 103 def base_branch pull_request[:base][:ref] end
Public: Return <true> if the payload event type is of type 'check_run'; otherwise, <false>
# File lib/github_bot/github/payload.rb, line 25 def check_run? payload_type == 'check_run' end
Public: Returns the head branch that the changes are on
@return [String] The head branch that the changes are on
# File lib/github_bot/github/payload.rb, line 96 def head_branch pull_request[:head][:ref] end
Public: Returns the SHA of the most recent commit for this pull request
@return [String] The SHA of the most recent commit for this pull request
# File lib/github_bot/github/payload.rb, line 85 def head_sha if issue_comment? 'HEAD' else pull_request[:head][:sha] end end
Public: Returns the installation identifier associated to the event
@return [Integer] identifier of the GitHub App installation
# File lib/github_bot/github/payload.rb, line 15 def installation_id payload[:installation][:id] end
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
# File lib/github_bot/github/payload.rb, line 41 def issue_comment? payload_type == 'issue_comment' end
Public: Return <true> if the payload event type is of type 'labeled'; otherwise, <false>
# File lib/github_bot/github/payload.rb, line 46 def labeled? payload_type == 'labeled' end
Public: Return the activity related to the pull request
@return [Hash] The activity related to the pull request
# 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
Public: Return <true> if the payload event type is of type 'pull_request'; otherwise, <false>
# File lib/github_bot/github/payload.rb, line 20 def pull_request? payload_type == 'pull_request' end
Public: Returns the pull request body content
@return [String] The pull request body content
# File lib/github_bot/github/payload.rb, line 110 def pull_request_body pull_request[:body] end
Public: Returns the pull request number from the payload
@return [Integer] The pull request number from the payload
# File lib/github_bot/github/payload.rb, line 78 def pull_request_number pull_request[:number] end
Public: Returns the repository URL utilized for performing a 'git clone'
@return [String] The repository URL utilized for performing a 'git clone'
# File lib/github_bot/github/payload.rb, line 131 def repository_clone_url repository[:clone_url] end
Public: Returns the repository default branch
@return [String] The repository default branch
# File lib/github_bot/github/payload.rb, line 174 def repository_default_branch repository[:default_branch] end
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
@return [Array] The array of [String] URLs associated to the forked repositories
# 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
Public: Returns the organization and repository name
@return [String] The organization and repository name
# File lib/github_bot/github/payload.rb, line 124 def repository_full_name repository[:full_name] end
Public: Returns the name of the repository where the event was triggered
@return [String] The name of the repository where the event was triggered
# File lib/github_bot/github/payload.rb, line 117 def repository_name repository[:name] end
Public: Returns a list of class object names to create for validation
@return [Array<String>] A list of strings associated to the class object validators
# 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
Public: Return <true> if the payload event type is of type 'review_request_removed'; otherwise, <false>
# File lib/github_bot/github/payload.rb, line 35 def review_request_removed? payload_type == 'review_request_removed' end
Public: Return <true> if the payload event type is of type 'review_requested'; otherwise, <false>
# File lib/github_bot/github/payload.rb, line 30 def review_requested? payload_type == 'review_requested' end
Public: Returns <true> if the sender is of type 'Bot'; otherwise, <false>
# File lib/github_bot/github/payload.rb, line 71 def sender_type_bot? payload[:sender][:type].downcase == 'bot' # rubocop:disable Performance/Casecmp end
Public: Return <true> if the payload event type is of type 'unlabeled'; otherwise, <false>
# File lib/github_bot/github/payload.rb, line 51 def unlabeled? payload_type == 'unlabeled' end
Private Instance Methods
# File lib/github_bot/github/payload.rb, line 201 def method_missing(method_name, *args) payload[method_name] || super end
# File lib/github_bot/github/payload.rb, line 223 def payload @payload ||= verify_webhook_signature(payload_body, @request.env['HTTP_X_HUB_SIGNATURE']) end
# File lib/github_bot/github/payload.rb, line 227 def payload_body @payload_body ||= @request.body.read end
# File lib/github_bot/github/payload.rb, line 231 def payload_type @request.env['HTTP_X_GITHUB_EVENT'] if @request end
# File lib/github_bot/github/payload.rb, line 209 def raw_file_url(path) # https://github.com/api/v3/repos/poloka/foo-config/contents/.github-bot?ref= content_file = File.join(repository_contents_url, "#{path}?ref=#{head_sha}") JSON.parse(URI.parse(content_file).open(&:read))['download_url'] rescue ::OpenURI::HTTPError => e raise StandardError, "file '#{content_file}' not found" unless /404 Not Found/i.match?(e.message) end
# File lib/github_bot/github/payload.rb, line 217 def repository_contents_url # drop the last 'path' portion of the API endpoint # https://github.com/api/v3/repos/poloka/foo-config/contents/{+path} repository[:contents_url].split('/')[0...-1].join('/') end
# File lib/github_bot/github/payload.rb, line 205 def respond_to_missing?(method, *args) payload.key?(method) || super end
# File lib/github_bot/github/payload.rb, line 235 def verify_webhook_signature(payload, payload_signature) signature = 'sha1=' + OpenSSL::HMAC.hexdigest( OpenSSL::Digest.new('sha1'), ENV['GITHUB_WEBHOOK_SECRET'], payload ) Rack::Utils.secure_compare(signature, payload_signature) ? JSON.parse(payload).with_indifferent_access : false end