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, #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



39
40
41
# File 'lib/github_bot/github/client.rb', line 39

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



189
190
191
192
193
194
195
# File 'lib/github_bot/github/client.rb', line 189

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



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

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



29
30
31
32
33
# File 'lib/github_bot/github/client.rb', line 29

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



118
119
120
# File 'lib/github_bot/github/client.rb', line 118

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



81
82
83
# File 'lib/github_bot/github/client.rb', line 81

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:



88
89
90
91
92
93
94
95
96
# File 'lib/github_bot/github/client.rb', line 88

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



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/github_bot/github/client.rb', line 46

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



71
72
73
74
75
# File 'lib/github_bot/github/client.rb', line 71

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



62
63
64
65
66
# File 'lib/github_bot/github/client.rb', line 62

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



125
126
127
128
129
130
# File 'lib/github_bot/github/client.rb', line 125

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



101
102
103
# File 'lib/github_bot/github/client.rb', line 101

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



108
109
110
111
112
113
# File 'lib/github_bot/github/client.rb', line 108

def pull_request_reviewers
  @pull_request_reviewers ||= client.pull_request_reviews(
    repository[:full_name],
    pull_request[:number]
  ).sort_by(&:submitted_at)
end