Class: GitHubRequester::PullRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/diff-suggester/client/requester.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo:, pr_number:, access_token:, base_url: nil) ⇒ PullRequest

Returns a new instance of PullRequest.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/diff-suggester/client/requester.rb', line 8

def initialize(repo:, pr_number:, access_token:, base_url: nil)
  octokit_client = base_url.nil? ? Octokit::Client.new(access_token: access_token) :
    Octokit::Client.new(access_token: access_token, api_endpoint: base_url)
  @pr = octokit_client.pull_request(repo, pr_number)

  @uri = URI.parse("#{base_url || default_base_url}/repos/#{repo}/pulls/#{@pr.number}/comments")

  @request = Net::HTTP::Post.new(@uri)
  @request['Accept'] = 'application/vnd.github+json'
  @request['Authorization'] = "Bearer #{access_token}"
  @request['X-GitHub-Api-Version'] = '2022-11-28'
end

Instance Method Details

#create_comment(path:, body:, line:, start_line:, side:, start_side:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/diff-suggester/client/requester.rb', line 21

def create_comment(path:, body:, line:, start_line:, side:, start_side:)
  @request.body = {
    'commit_id': @pr.head.sha,
    'path': path,
    'body': body,
    'line': line,
    'start_line': start_line,
    'side': side,
    'start_side': start_side
  }.to_json

  response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') do |http|
    http.request(@request)
  end
  return response
end