Skip to content

Commit b0a1a7a

Browse files
committed
Refactor paginated_check_runs to use lazy pagination
1 parent 65e71f5 commit b0a1a7a

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

app/models/shipit/commit.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,26 @@ def create_status_from_github!(github_status)
168168
end
169169
end
170170

171-
def paginated_check_runs
171+
def paginated_check_runs(&block)
172172
response = stack.handle_github_redirections do
173173
stack.github_api.check_runs(github_repo_name, sha, per_page: 100)
174174
end
175175

176-
return response if stack.github_api.last_response.rels[:next].nil?
176+
yield response.check_runs
177177

178-
loop do
178+
until stack.github_api.last_response.rels[:next].nil?
179179
page = stack.handle_github_redirections do
180180
stack.github_api.get(stack.github_api.last_response.rels[:next].href)
181181
end
182-
response.check_runs.concat(page.check_runs)
183-
break if stack.github_api.last_response.rels[:next].nil?
182+
yield page.check_runs
184183
end
185-
186-
response
187184
end
188185

189186
def refresh_check_runs!
190-
response = paginated_check_runs
191-
response.check_runs.each do |check_run|
192-
create_or_update_check_run_from_github!(check_run)
187+
paginated_check_runs do |check_runs|
188+
check_runs.each do |check_run|
189+
create_or_update_check_run_from_github!(check_run)
190+
end
193191
end
194192
end
195193

test/models/commits_test.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,76 @@ class CommitsTest < ActiveSupport::TestCase
367367
assert_equal 'success', @commit.check_runs.first.state
368368
end
369369

370+
test "refresh_check_runs! handles paginated responses from github" do
371+
# First page check runs
372+
check_run_1 = mock(
373+
id: 111_111_111_111_111,
374+
name: 'Test suite 1',
375+
conclusion: 'success',
376+
details_url: 'https://example.com/details/1',
377+
html_url: 'https://example.com/run/1',
378+
output: mock(
379+
title: 'Tests build 1 ran successfully'
380+
),
381+
completed_at: Time.now,
382+
started_at: Time.now - 1.minute
383+
)
384+
check_run_2 = mock(
385+
id: 222_222_222_222_222,
386+
name: 'Test suite 2',
387+
conclusion: 'failure',
388+
details_url: 'https://example.com/details/2',
389+
html_url: 'https://example.com/run/2',
390+
output: mock(
391+
title: 'Tests build 2 failed'
392+
),
393+
completed_at: Time.now,
394+
started_at: Time.now - 2.minutes
395+
)
396+
397+
# Second page check runs
398+
check_run_3 = mock(
399+
id: 333_333_333_333_333,
400+
name: 'Test suite 3',
401+
conclusion: 'neutral',
402+
details_url: 'https://example.com/details/3',
403+
html_url: 'https://example.com/run/3',
404+
output: mock(
405+
title: 'Tests build 3 skipped'
406+
),
407+
completed_at: Time.now,
408+
started_at: Time.now - 3.minutes
409+
)
410+
411+
next_link = mock(href: 'https://api.github.com/repos/test/repo/check-runs?page=2')
412+
first_response = stub(rels: { next: next_link })
413+
first_page_data = mock(check_runs: [check_run_1, check_run_2])
414+
415+
second_response = stub(rels: {})
416+
second_page_data = mock(check_runs: [check_run_3])
417+
418+
Shipit.github.api.expects(:check_runs).with(@stack.github_repo_name, @commit.sha, per_page: 100).returns(first_page_data)
419+
Shipit.github.api.expects(:last_response).returns(first_response).twice
420+
Shipit.github.api.expects(:get).with(next_link.href).returns(second_page_data)
421+
Shipit.github.api.expects(:last_response).returns(second_response)
422+
423+
assert_difference -> { @commit.check_runs.count }, 3 do
424+
@commit.refresh_check_runs!
425+
end
426+
427+
check_1 = @commit.check_runs.find_by(github_id: 111_111_111_111_111)
428+
assert_not_nil check_1
429+
assert_equal 'success', check_1.state
430+
431+
check_2 = @commit.check_runs.find_by(github_id: 222_222_222_222_222)
432+
assert_not_nil check_2
433+
assert_equal 'error', check_2.state
434+
435+
check_3 = @commit.check_runs.find_by(github_id: 333_333_333_333_333)
436+
assert_not_nil check_3
437+
assert_equal 'pending', check_3.state
438+
end
439+
370440
test "#creating a commit update the undeployed_commits_count" do
371441
walrus = shipit_users(:walrus)
372442
assert_equal 2, @stack.undeployed_commits_count

0 commit comments

Comments
 (0)