Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit 8899c8a

Browse files
committed
support mergeable and minimize API calls
1 parent a59f3a6 commit 8899c8a

File tree

7 files changed

+23
-24
lines changed

7 files changed

+23
-24
lines changed

assets/lib/filters/all.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(pull_requests: [], input: Input.instance)
1111

1212
def pull_requests
1313
@pull_requests ||= Octokit.pulls(input.source.repo, pull_options).map do |pr|
14-
PullRequest.new(pr: Octokit.pull_request(input.source.repo, pr['number'], pull_options))
14+
PullRequest.new(pr: pr) # keep this lazy, specific filters should pull data if they need to
1515
end
1616
end
1717

assets/lib/filters/approval.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Filters
24
class Approval
35
def initialize(pull_requests:, input: Input.instance)

assets/lib/filters/mergeable.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Filters
24
class Mergeable
35
def initialize(pull_requests:, input: Input.instance)
@@ -7,7 +9,11 @@ def initialize(pull_requests:, input: Input.instance)
79

810
def pull_requests
911
if @input.source.only_mergeable
10-
@memoized ||= @pull_requests.delete_if { |x| !x.mergeable? }
12+
13+
@memoized ||= @pull_requests.delete_if do |pr|
14+
response = Octokit.pull_request(@input.source.repo, pr.id)
15+
!response['mergeable']
16+
end
1117
else
1218
@pull_requests
1319
end

assets/lib/pull_request.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ def from_fork?
1616
base_repo != head_repo
1717
end
1818

19-
def mergeable?
20-
@pr['mergeable']
21-
end
22-
2319
def review_approved?
2420
Octokit.pull_request_reviews(base_repo, id).any? { |r| r['state'] == 'APPROVED' }
2521
end

spec/commands/check_spec.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,10 @@ def stub_json(uri, body)
1717
.to_return(headers: { 'Content-Type' => 'application/json' }, body: body.to_json)
1818
end
1919

20-
def stub_prs(uri, body)
21-
stub_json(uri, body)
22-
body.each do |pr|
23-
stub_json(uri.sub('pulls', 'pulls/' + pr[:number].to_s).sub('&per_page=100', ''), pr)
24-
end
25-
end
26-
2720
context 'when targetting a base branch other than master' do
2821
before do
2922
stub_json('https://api.github.com/repos/jtarchie/test/statuses/abcdef', [])
30-
stub_prs('https://api.github.com:443/repos/jtarchie/test/pulls?base=my-base-branch&direction=asc&per_page=100&sort=updated&state=open', [{ number: 1, head: { sha: 'abcdef' } }])
23+
stub_json('https://api.github.com:443/repos/jtarchie/test/pulls?base=my-base-branch&direction=asc&per_page=100&sort=updated&state=open', [{ number: 1, head: { sha: 'abcdef' } }])
3124
end
3225

3326
it 'retrieves pull requests for the specified base branch' do
@@ -55,7 +48,7 @@ def stub_prs(uri, body)
5548

5649
context 'when there is an open pull request' do
5750
before do
58-
stub_prs('https://api.github.com:443/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [{ number: 1, head: { sha: 'abcdef' } }])
51+
stub_json('https://api.github.com:443/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [{ number: 1, head: { sha: 'abcdef' } }])
5952
end
6053

6154
it 'returns SHA of the pull request' do
@@ -86,10 +79,10 @@ def stub_prs(uri, body)
8679

8780
context 'when there is more than one open pull request' do
8881
before do
89-
stub_prs('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [
90-
{ number: 1, head: { sha: 'abcdef', repo: { full_name: 'jtarchie/test' } }, base: { repo: { full_name: 'jtarchie/test' } } },
91-
{ number: 2, head: { sha: 'zyxwvu', repo: { full_name: 'someotherowner/repo' } }, base: { repo: { full_name: 'jtarchie/test' } } }
92-
])
82+
stub_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [
83+
{ number: 1, head: { sha: 'abcdef', repo: { full_name: 'jtarchie/test' } }, base: { repo: { full_name: 'jtarchie/test' } } },
84+
{ number: 2, head: { sha: 'zyxwvu', repo: { full_name: 'someotherowner/repo' } }, base: { repo: { full_name: 'jtarchie/test' } } }
85+
])
9386
end
9487

9588
it 'returns all PRs oldest to newest last' do
@@ -126,9 +119,7 @@ def stub_cache_json(uri)
126119
end
127120

128121
stub_body_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', pull_requests[0..49], 'Link' => '<https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open&page=2>; rel="next"')
129-
stub_prs('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&sort=updated&state=open', pull_requests[0..49])
130122
stub_body_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open&page=2', pull_requests[50..99])
131-
stub_prs('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&sort=updated&state=open', pull_requests[50..99])
132123

133124
first_prs = check('source' => { 'repo' => 'jtarchie/test' })
134125
expect(first_prs.length).to eq 100

spec/filters/approval_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative '../../assets/lib/filters/approval'
24
require_relative '../../assets/lib/pull_request'
35
require_relative '../../assets/lib/input'

spec/filters/mergeable_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative '../../assets/lib/filters/mergeable'
24
require_relative '../../assets/lib/pull_request'
35
require_relative '../../assets/lib/input'
@@ -37,11 +39,11 @@ def stub_json(uri, body)
3739

3840
context 'when the mergeable filtering is enabled' do
3941
before do
40-
stub_json(%r{https://api.github.com/repos/user/repo/pulls/1/reviews}, [{ 'state' => 'CHANGES_REQUESTED' }])
41-
stub_json(%r{https://api.github.com/repos/user/repo/pulls/2/reviews}, [{ 'state' => 'APPROVED' }])
42+
stub_json(%r{https://api.github.com/repos/user/repo/pulls/1}, 'mergeable' => false)
43+
stub_json(%r{https://api.github.com/repos/user/repo/pulls/2}, 'mergeable' => true)
4244
end
4345

44-
it 'only returns PRs with that label' do
46+
it 'only returns PRs with that are mergeable' do
4547
payload = { 'source' => { 'repo' => 'user/repo', 'only_mergeable' => true } }
4648
filter = described_class.new(pull_requests: pull_requests, input: Input.instance(payload: payload))
4749

0 commit comments

Comments
 (0)