Class: Geet::Github::PR

Inherits:
AbstractIssue show all
Defined in:
lib/geet/github/pr.rb

Instance Attribute Summary

Attributes inherited from AbstractIssue

#link, #number, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractIssue

#add_labels, #assign_users, #comment, #edit, #initialize

Constructor Details

This class inherits a constructor from Geet::Github::AbstractIssue

Class Method Details

.create(title, description, head, api_interface, base, draft: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/geet/github/pr.rb', line 11

def self.create(title, description, head, api_interface, base, draft: false)
  api_path = 'pulls'

  if api_interface.upstream?
    authenticated_user = Geet::Github::User.authenticated(api_interface).username
    head = "#{authenticated_user}:#{head}"
  end

  request_data = { title: title, body: description, head: head, base: base, draft: draft }

  response = api_interface.send_request(api_path, data: request_data)

  number, title, link = response.fetch_values('number', 'title', 'html_url')

  new(number, api_interface, title, link)
end

.list(api_interface, milestone: nil, assignee: nil, owner: nil, head: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/geet/github/pr.rb', line 30

def self.list(api_interface, milestone: nil, assignee: nil, owner: nil, head: nil)
  check_list_params!(milestone, assignee, head)

  if head
    api_path = 'pulls'

    # Technically, the upstream approach could be used for both, but it's actually good to have
    # both of them as reference.
    #
    # For upstream pulls, the owner is the authenticated user, otherwise, the repository owner.
    #
    response = if api_interface.upstream?
      unfiltered_response = api_interface.send_request(api_path, multipage: true)

      # VERY weird. From the docs, it's not clear if the user/org is required in the `head` parameter,
      # but:
      #
      # - if it isn't included (eg. `anything`), the parameter is ignored
      # - if it's included (eg. `saveriomiroddi:local_branch_name`), an empty resultset is returned.
      #
      # For this reason, we can't use that param, and have to filter manually.
      #
      unfiltered_response.select { |pr_data| pr_data.fetch('head').fetch('label') == "#{owner}:#{head}" }
    else
      request_params = { head: "#{owner}:#{head}" }

      api_interface.send_request(api_path, params: request_params, multipage: true)
    end

    response.map do |pr_data|
      number = pr_data.fetch('number')
      title = pr_data.fetch('title')
      link = pr_data.fetch('html_url')

      new(number, api_interface, title, link)
    end
  else
    super(api_interface, milestone: milestone, assignee: assignee) do |issue_data|
      issue_data.key?('pull_request')
    end
  end
end

Instance Method Details

#mergeObject



75
76
77
78
79
# File 'lib/geet/github/pr.rb', line 75

def merge
  api_path = "pulls/#{number}/merge"

  @api_interface.send_request(api_path, http_method: :put)
end

#request_review(reviewers) ⇒ Object



81
82
83
84
85
86
# File 'lib/geet/github/pr.rb', line 81

def request_review(reviewers)
  api_path = "pulls/#{number}/requested_reviewers"
  request_data = { reviewers: reviewers }

  @api_interface.send_request(api_path, data: request_data)
end