Class: GithubBot::Github::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • request (Object)

    The incoming request payload



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

Parameters:

  • request (Object)

    The incoming request payload



21
22
23
# File 'lib/github_bot/github/client.rb', line 21

def initialize(request)
  @instance = new(request)
end

.instanceObject

Public: Returns the current instance of the Client

Raises:

  • (StandardError)

    Raises error if instance has not been initialized before usage



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_reviewersArray<Sawyer::Resource>

Public: Returns the current list of approving pull request reviewers

Returns:

  • (Array<Sawyer::Resource>)

    The 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

Parameters:

  • opts (Hash)

    The parameter options for adding a comment

Options Hash (**opts):

  • :message (:symbol)

    The message to add to the 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, message, **opts)
end

#create_check_run(name:, **opts) ⇒ GithubBot::Github::CheckRun

Public: Creates a GitHub check run for execution

Returns:



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

Parameters:

  • file (Sawyer::Resource)

    The file for retrieving contents



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

#filesArray<Sawyer::Resource>

Public: Returns an array of all the files impacted with the current pull request

Returns:

  • (Array<Sawyer::Resource>)

    A list of all 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_filesArray<Sawyer::Resource>

Public: Return the modified files, excluding those that have been removed, from the pull request

Returns:

  • (Array<Sawyer::Resource>)

    A list of modified files impacted with the current 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_commentsArray<Sawyer::Resource>

Public: Returns the current list of request comments

Returns:

  • (Array<Sawyer::Resource>)

    The list of pull 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_detailsSawyer::Resource

Public: Returns a GitHub pull request object with the details of the current request

Returns:

  • (Sawyer::Resource)

    The pull request details associated to 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_reviewersArray<Sawyer::Resource>

Public: Returns the current list of pull request reviewers

Returns:

  • (Array<Sawyer::Resource>)

    The list of current 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