Class: PullRequestAi::GitHub::Client

Inherits:
Repo::Client show all
Defined in:
lib/pull_request_ai/github/client.rb

Overview

A client to communicate with the GitHub API.

Instance Attribute Summary collapse

Attributes inherited from Repo::Client

#api_endpoint, #http_timeout

Instance Method Summary collapse

Methods inherited from Repo::Client

client_from_host

Constructor Details

#initialize(http_timeout: nil, api_endpoint: nil, access_token: nil) ⇒ Client

Initializes the client.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pull_request_ai/github/client.rb', line 11

def initialize(
  http_timeout: nil,
  api_endpoint: nil,
  access_token: nil
)
  super(
    http_timeout || PullRequestAi.http_timeout,
    api_endpoint || PullRequestAi.github_api_endpoint
  )
  @access_token = access_token || PullRequestAi.github_access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/pull_request_ai/github/client.rb', line 7

def access_token
  @access_token
end

Instance Method Details

#open_pull_request(slug, head, base, title, description) ⇒ Object

Request to open a new Pull Request using the GitHub API. The slug combines the repository owner name and the repository name. It requires the head (destination branch), the base (current branch), the title, and a optional description. docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#create-a-pull-request



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pull_request_ai/github/client.rb', line 73

def open_pull_request(slug, head, base, title, description)
  body = {
    title: title,
    body: description,
    head: head,
    base: base
  }.to_json
  url = build_url(slug)
  request(:post, url, {}, body).bind do |pr|
    Dry::Monads::Success(parsed_pr_details(pr))
  end
end

#opened_pull_requests(slug, head, base) ⇒ Object

Requests the list of Open Pull Requests using the GitHub API. The slug combines the repository owner name and the repository name. The query contains the head and base to filter the results. Notice: On GitHub it is only possible to have one PR open with the same head and base, despite the result being a list. docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pull_request_ai/github/client.rb', line 30

def opened_pull_requests(slug, head, base)
  query = {
    head: "#{slug.split(":").first}:#{head}",
    base: base
  }
  url = build_url(slug)
  request(:get, url, query, {}).bind do |open_prs|
    if open_prs.empty?
      Dry::Monads::Success([])
    else
      result = open_prs.map do |pr|
        parsed_pr_details(pr)
      end
      Dry::Monads::Success(result)
    end
  end
end

#update_pull_request(slug, number, base, title, description) ⇒ Object

Request to update the existing Pull Request using the GitHub API. The slug combines the repository owner name and the repository name. It requires the Pull Request number to modify it. The base, title, and description can be modified. Notice: We don’t have logic to change the base on the UI. docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#update-a-pull-request



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pull_request_ai/github/client.rb', line 55

def update_pull_request(slug, number, base, title, description)
  body = {
    title: title,
    body: description,
    state: 'open',
    base: base
  }.to_json
  url = build_url(slug, "/#{number}")
  request(:patch, url, {}, body).bind do |pr|
    Dry::Monads::Success(parsed_pr_details(pr))
  end
end