Skip to content

Commit

Permalink
Show check mark when PRs are approved.
Browse files Browse the repository at this point in the history
Many teams are now using Github's new review workflow, where you
explicitly approve a PR rather than marking it with a +1. This is now
supported in their API as a preview function via a specific media type
header.

This change adds a green check mark for PRs that are approved in this
way.
  • Loading branch information
Daniel Roseman committed Jan 24, 2017
1 parent 3913460 commit 6b31447
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/github_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class GithubFetcher
ORGANISATION ||= ENV['SEAL_ORGANISATION']
# TODO: remove media type when review support comes out of preview
Octokit.default_media_type = 'application/vnd.github.black-cat-preview+json'

attr_accessor :people

Expand Down Expand Up @@ -37,6 +39,7 @@ def present_pull_request(pull_request, repo_name)
pr['repo'] = repo_name
pr['comments_count'] = count_comments(pull_request, repo_name)
pr['thumbs_up'] = count_thumbs_up(pull_request, repo_name)
pr['approved'] = approved?(pull_request, repo_name)
pr['updated'] = Date.parse(pull_request.updated_at.to_s)
pr['labels'] = labels(pull_request, repo_name)
pr
Expand All @@ -63,6 +66,11 @@ def count_thumbs_up(pull_request, repo)
thumbs_up = comments_string.scan(/:\+1:/).count.to_s
end

def approved?(pull_request, repo)
reviews = @github.get("repos/#{ORGANISATION}/#{repo}/pulls/#{pull_request.number}/reviews")
reviews.any? { |review| review.state == 'APPROVED' }
end

def labels(pull_request, repo)
return [] unless use_labels
key = "#{ORGANISATION}/#{repo}/#{pull_request.number}".to_sym
Expand Down
3 changes: 2 additions & 1 deletion lib/message_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def present(pull_request, index)
days = age_in_days(pr)
thumbs_up = ''
thumbs_up = " | #{pr["thumbs_up"].to_i} :+1:" if pr["thumbs_up"].to_i > 0
approved = pr["approved"] ? " | :white_check_mark: " : ""
<<-EOF.gsub(/^\s+/, '')
#{index}\) *#{pr["repo"]}* | #{pr["author"]} | updated #{days_plural(days)}#{thumbs_up}
#{index}\) *#{pr["repo"]}* | #{pr["author"]} | updated #{days_plural(days)}#{thumbs_up}#{approved}
#{labels(pr)} <#{pr["link"]}|#{pr["title"]}> - #{pr["comments_count"]}#{comments(pull_request)}
EOF
end
Expand Down
12 changes: 12 additions & 0 deletions spec/github_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'repo' => 'whitehall',
'comments_count' => '1',
'thumbs_up' => '1',
'approved' => false,
'updated' => Date.parse('2015-07-13 ((2457217j,0s,0n),+0s,2299161j)'),
'labels' => []
},
Expand All @@ -51,6 +52,7 @@
'repo' => 'whitehall-rebuild',
'comments_count' => '5',
'thumbs_up' => '0',
'approved' => true,
'updated' => Date.parse('2015-07-17 ((2457221j,0s,0n),+0s,2299161j)'),
'labels' => []
}
Expand Down Expand Up @@ -95,6 +97,14 @@
].map { |body| double(Sawyer::Resource, body: body)}
end

let(:reviews_2248) do
[
double(Sawyer::Resource, state: "APPROVED" )
]
end

let(:reviews_2266) { [] }

before do
expect(Octokit::Client).to receive(:new).and_return(fake_octokit_client)
expect(fake_octokit_client).to receive_message_chain('user.login')
Expand All @@ -106,6 +116,8 @@

allow(fake_octokit_client).to receive(:pull_request).with(whitehall_rebuild_repo_name, 2248).and_return(pull_2248)
allow(fake_octokit_client).to receive(:pull_request).with(whitehall_repo_name, 2266).and_return(pull_2266)
allow(fake_octokit_client).to receive(:get).with(%r"repos/alphagov/[\w-]+/pulls/2248/reviews").and_return(reviews_2248)
allow(fake_octokit_client).to receive(:get).with(%r"repos/alphagov/[\w-]+/pulls/2266/reviews").and_return(reviews_2266)
end

shared_examples_for 'fetching from GitHub' do
Expand Down
6 changes: 5 additions & 1 deletion spec/message_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'repo' => 'whitehall',
'comments_count' => '5',
'thumbs_up' => '0',
'approved' => true,
'updated' => Date.parse('2015-07-17 ((2457221j, 0s, 0n), +0s, 2299161j)'),
'labels' => []
}
Expand All @@ -29,6 +30,7 @@
'repo' => 'whitehall',
'comments_count' => '1',
'thumbs_up' => '1',
'approved' => false,
'updated' => Date.parse('2015-07-13 ((2457217j, 0s, 0n), +0s, 2299161j)'),
'labels' => []
},
Expand All @@ -39,6 +41,7 @@
'repo' => 'whitehall',
'comments_count' => '5',
'thumbs_up' => '0',
'approved' => false,
'updated' => Date.parse('2015-07-17 ((2457221j, 0s, 0n), +0s, 2299161j)'),
'labels' => []
}
Expand All @@ -58,6 +61,7 @@
'repo' => 'repo',
'comments_count' => '0',
'thumbs_up' => '0',
'approved' => false,
'updated' => Date.today,
'labels' => [
{ 'name' => 'wip' },
Expand All @@ -78,7 +82,7 @@
let(:pull_requests) { recent_pull_requests }

it 'builds informative message' do
expect(message_builder.build).to eq("Hello team! \n\n Here are the pull requests that need to be reviewed today:\n\n1) *whitehall* | tekin | updated yesterday\n<https://github.com/alphagov/whitehall/pull/2248|Remove all Import-related code> - 5 comments\n\nMerry reviewing!")
expect(message_builder.build).to eq("Hello team! \n\n Here are the pull requests that need to be reviewed today:\n\n1) *whitehall* | tekin | updated yesterday | :white_check_mark: \n<https://github.com/alphagov/whitehall/pull/2248|Remove all Import-related code> - 5 comments\n\nMerry reviewing!")
end

it 'has an informative poster mood' do
Expand Down

0 comments on commit 6b31447

Please sign in to comment.