Class: GithubBot::Github::Client
- Inherits:
-
Object
- Object
- GithubBot::Github::Client
- Includes:
- Payload
- Defined in:
- lib/github_bot/github/client.rb
Overview
Public: The Client class manages the client interactions with GitHub such as file retrieval, pull request comments, and pull request checks
Constant Summary collapse
- FILE_REMOVED_STATUS =
'removed'- RAW_TYPE =
'application/vnd.github.v3.raw'
Class Method Summary collapse
-
.initialize(request) ⇒ Object
Public: Initialize the singleton with the incoming request information.
-
.instance ⇒ Object
Public: Returns the current instance of the Client.
Instance Method Summary collapse
-
#approving_reviewers ⇒ Array<Sawyer::Resource>
Public: Returns the current list of approving pull request reviewers.
-
#comment(message:, **opts) ⇒ Object
Public: Added a comment to the existing pull request.
-
#create_check_run(name:, **opts) ⇒ GithubBot::Github::CheckRun
Public: Creates a GitHub check run for execution.
-
#file_content(file) ⇒ Object
Public: Retrieve the contents of a file.
-
#files ⇒ Array<Sawyer::Resource>
Public: Returns an array of all the files impacted with the current pull request.
-
#initialize(request) ⇒ Client
constructor
Public: Creates a new instance of the Client to manage the GitHub api transactions.
-
#modified_files ⇒ Array<Sawyer::Resource>
Public: Return the modified files, excluding those that have been removed, from the pull request.
-
#pull_request_comments ⇒ Array<Sawyer::Resource>
Public: Returns the current list of request comments.
-
#pull_request_details ⇒ Sawyer::Resource
Public: Returns a GitHub pull request object with the details of the current request.
-
#pull_request_reviewers ⇒ Array<Sawyer::Resource>
Public: Returns the current list of pull request reviewers.
Methods included from Payload
#base_branch, #check_run?, #head_branch, #head_sha, #installation_id, #issue_comment?, #labeled?, #pull_request, #pull_request?, #pull_request_body, #pull_request_number, #repository_clone_url, #repository_default_branch, #repository_fork_urls, #repository_full_name, #repository_name, #repository_pull_request_bots, #review_request_removed?, #review_requested?, #sender_type_bot?, #unlabeled?
Constructor Details
#initialize(request) ⇒ Client
Public: Creates a new instance of the Client to manage the GitHub api transactions
38 39 40 |
# File 'lib/github_bot/github/client.rb', line 38 def initialize(request) @request = request end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
relay messages to Octokit::Client if responds to allow extension of the client and extend/overwrite those concerned with
188 189 190 191 192 193 194 |
# File 'lib/github_bot/github/client.rb', line 188 def method_missing(method, *args, &block) return super unless respond_to_missing?(method) return payload[method] if payload.key?(method) client.send(method, *args, &block) end |
Class Method Details
.initialize(request) ⇒ Object
Public: Initialize the singleton with the incoming request information
21 22 23 |
# File 'lib/github_bot/github/client.rb', line 21 def initialize(request) @instance = new(request) end |
.instance ⇒ Object
Public: Returns the current instance of the Client
28 29 30 31 32 |
# File 'lib/github_bot/github/client.rb', line 28 def instance raise StandardError, 'client not initialize' unless @instance @instance end |
Instance Method Details
#approving_reviewers ⇒ Array<Sawyer::Resource>
Public: Returns the current list of approving pull request reviewers
117 118 119 |
# File 'lib/github_bot/github/client.rb', line 117 def approving_reviewers pull_request_reviewers.select { |r| r.state == 'APPROVED' } end |
#comment(message:, **opts) ⇒ Object
Public: Added a comment to the existing pull request
80 81 82 |
# File 'lib/github_bot/github/client.rb', line 80 def comment(message:, **opts) client.add_comment(repository_full_name, pull_request_number, , **opts) end |
#create_check_run(name:, **opts) ⇒ GithubBot::Github::CheckRun
Public: Creates a GitHub check run for execution
87 88 89 90 91 92 93 94 95 |
# File 'lib/github_bot/github/client.rb', line 87 def create_check_run(name:, **opts) CheckRun.new( name: name, repo: repository_full_name, sha: head_sha, client_api: client, **opts ) end |
#file_content(file) ⇒ Object
Public: Retrieve the contents of a file
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/github_bot/github/client.rb', line 45 def file_content(file) raw_contents file rescue Octokit::NotFound '' rescue Octokit::Forbidden => e if e.errors.any? && e.errors.first[:code] == 'too_large' # revert to using the raw_url for getting the content return URI.parse(file.raw_url).open.read end raise e end |
#files ⇒ Array<Sawyer::Resource>
Public: Returns an array of all the files impacted with the current pull request
70 71 72 73 74 |
# File 'lib/github_bot/github/client.rb', line 70 def files return [] if pull_request.nil? @files ||= client.pull_request_files(repository_full_name, pull_request_number) end |
#modified_files ⇒ Array<Sawyer::Resource>
Public: Return the modified files, excluding those that have been removed, from the pull request
61 62 63 64 65 |
# File 'lib/github_bot/github/client.rb', line 61 def modified_files files.reject do |github_file| github_file.status == FILE_REMOVED_STATUS end end |
#pull_request_comments ⇒ Array<Sawyer::Resource>
Public: Returns the current list of request comments
124 125 126 127 128 129 |
# File 'lib/github_bot/github/client.rb', line 124 def pull_request_comments @pull_request_comments ||= client.issue_comments( repository[:full_name], pull_request[:number] ).sort_by(&:created_at) end |
#pull_request_details ⇒ Sawyer::Resource
Public: Returns a GitHub pull request object with the details of the current request
100 101 102 |
# File 'lib/github_bot/github/client.rb', line 100 def pull_request_details @pull_request_details ||= client.pull_request(repository[:full_name], pull_request[:number]) end |
#pull_request_reviewers ⇒ Array<Sawyer::Resource>
Public: Returns the current list of pull request reviewers
107 108 109 110 111 112 |
# File 'lib/github_bot/github/client.rb', line 107 def pull_request_reviewers @pull_request_reviewers ||= client.pull_request_reviews( repository[:full_name], pull_request[:number] ).sort_by(&:submitted_at) end |