Class: Geet::Github::PR

Inherits:
AbstractIssue show all
Extended by:
T::Sig
Defined in:
lib/geet/github/pr.rb

Instance Attribute Summary collapse

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

Constructor Details

#initialize(number, api_interface, title, link, node_id: nil) ⇒ PR

Returns a new instance of PR.



21
22
23
24
# File 'lib/geet/github/pr.rb', line 21

def initialize(number, api_interface, title, link, node_id: nil)
  super(number, api_interface, title, link)
  @node_id = node_id
end

Instance Attribute Details

#node_idObject (readonly)

Returns the value of attribute node_id.



10
11
12
# File 'lib/geet/github/pr.rb', line 10

def node_id
  @node_id
end

Class Method Details

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/geet/github/pr.rb', line 38

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:, body: description, head:, base:, draft:}

  response = T.cast(api_interface.send_request(api_path, data: request_data), T::Hash[String, T.untyped])

  number = T.cast(response.fetch("number"), Integer)
  title = T.cast(response.fetch("title"), String)
  link = T.cast(response.fetch("html_url"), String)
  node_id = T.cast(response["node_id"], T.nilable(String))

  new(number, api_interface, title, link, node_id:)
end

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/geet/github/pr.rb', line 73

def self.list(api_interface, milestone: nil, assignee: nil, owner: nil, head: nil, &type_filter)
  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 = T.cast(
        api_interface.send_request(api_path, multipage: true),
        T::Array[T::Hash[String, T.untyped]]
      )

      # 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 do |pr_data|
        pr_head = T.cast(pr_data.fetch("head"), T::Hash[String, T.untyped])
        label = T.cast(pr_head.fetch("label"), String)

        label == "#{owner}:#{head}"
      end
    else
      request_params = {head: "#{owner}:#{head}"}

      T.cast(
        api_interface.send_request(api_path, params: request_params, multipage: true),
        T::Array[T::Hash[String, T.untyped]]
      )
    end

    response.map do |pr_data|
      number = T.cast(pr_data.fetch("number"), Integer)
      title = T.cast(pr_data.fetch("title"), String)
      link = T.cast(pr_data.fetch("html_url"), String)

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

    T.cast(result, T::Array[Geet::Github::PR])
  end
end

Instance Method Details

#enable_automergeObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/geet/github/pr.rb', line 161

def enable_automerge
  merge_method = fetch_available_merge_method

  query = <<~GRAPHQL
    mutation($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!) {
      enablePullRequestAutoMerge(input: {pullRequestId: $pullRequestId, mergeMethod: $mergeMethod}) {
        pullRequest {
          id
          autoMergeRequest {
            enabledAt
            mergeMethod
          }
        }
      }
    }
  GRAPHQL

  variables = {pullRequestId: @node_id, mergeMethod: merge_method}

  @api_interface.send_graphql_request(query, variables:)

  merge_method
end

#merge(merge_method: nil) ⇒ Object



136
137
138
139
140
141
# File 'lib/geet/github/pr.rb', line 136

def merge(merge_method: nil)
  api_path = "pulls/#{number}/merge"
  request_data = {merge_method:} if merge_method

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

#request_review(reviewers) ⇒ Object



148
149
150
151
152
153
# File 'lib/geet/github/pr.rb', line 148

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

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