Skip to content

Commit c294978

Browse files
Try using checks API
1 parent 24277e6 commit c294978

File tree

7 files changed

+142
-7
lines changed

7 files changed

+142
-7
lines changed

.github/workflows/ci.yml

+31-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
with:
1212
github_token: ${{ secrets.github_token }}
1313
reporter: github-check
14+
1415
rspec:
1516
runs-on: ubuntu-latest
1617
strategy:
@@ -41,8 +42,9 @@ jobs:
4142
bundle config path vendor/bundle
4243
bundle install --jobs 4 --retry 3
4344
- name: Run RSpec
44-
run: bundle exec rspec spec/rspec
45-
e2e:
45+
run: bundle exec rspec spec/rspec --format RSpec::Github::Formatter
46+
47+
e2e-formatter:
4648
runs-on: ubuntu-latest
4749
steps:
4850
- name: Check out code
@@ -64,8 +66,34 @@ jobs:
6466
bundle config path vendor/bundle
6567
bundle install --jobs 4 --retry 3
6668
- name: Run RSpec in GITHUB_WORKSPACE
67-
run: '! bundle exec rspec spec/integration/failing_spec.rb'
69+
run: '! bundle exec rspec spec/integration/failing_spec.rb --format RSpec::Github::Formatter'
6870
- name: Run RSpec in sub-directory of GITHUB_WORKSPACE
6971
run: |
7072
cd spec/integration
7173
bundle exec rspec relative_path/pending_spec.rb --require ../spec_helper --format RSpec::Github::Formatter
74+
75+
e2e-checks:
76+
runs-on: ubuntu-latest
77+
steps:
78+
- name: Check out code
79+
uses: actions/checkout@v2
80+
- name: Setup caching for ruby gems
81+
uses: actions/cache@v2
82+
with:
83+
path: vendor/bundle
84+
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
85+
restore-keys: |
86+
${{ runner.os }}-gems-
87+
- name: Setup Ruby
88+
uses: actions/setup-ruby@v1
89+
with:
90+
ruby-version: 2.7
91+
- name: Install gems
92+
run: |
93+
gem install bundler:2.1.4 --no-doc
94+
bundle config path vendor/bundle
95+
bundle install --jobs 4 --retry 3
96+
- name: Run RSpec with Annotator
97+
env:
98+
OCTOKIT_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
99+
run: '! bundle exec rspec spec/integration/failing_spec.rb --format RSpec::Github::Annotator'

.rspec

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
--color
22
--require spec_helper
33
--format documentation
4-
--format RSpec::Github::Formatter

Gemfile.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@ PATH
22
remote: .
33
specs:
44
rspec-github (1.0.0.develop)
5+
octokit (~> 4.0)
56
rspec-core (~> 3.0)
67

78
GEM
89
remote: https://rubygems.org/
910
specs:
11+
addressable (2.7.0)
12+
public_suffix (>= 2.0.2, < 5.0)
1013
ast (2.4.1)
1114
diff-lcs (1.4.4)
15+
faraday (1.1.0)
16+
multipart-post (>= 1.2, < 3)
17+
ruby2_keywords
18+
multipart-post (2.1.1)
19+
octokit (4.19.0)
20+
faraday (>= 0.9)
21+
sawyer (~> 0.8.0, >= 0.5.3)
1222
parallel (1.19.2)
1323
parser (2.7.2.0)
1424
ast (~> 2.4.1)
25+
public_suffix (4.0.6)
1526
rainbow (3.0.0)
1627
regexp_parser (1.8.2)
1728
rexml (3.2.4)
@@ -37,9 +48,13 @@ GEM
3748
rubocop-ast (>= 0.6.0)
3849
ruby-progressbar (~> 1.7)
3950
unicode-display_width (>= 1.4.0, < 2.0)
40-
rubocop-ast (1.0.0)
51+
rubocop-ast (1.0.1)
4152
parser (>= 2.7.1.5)
4253
ruby-progressbar (1.10.1)
54+
ruby2_keywords (0.0.2)
55+
sawyer (0.8.2)
56+
addressable (>= 2.3.5)
57+
faraday (> 0.8, < 2.0)
4358
unicode-display_width (1.7.0)
4459

4560
PLATFORMS

lib/rspec/github/annotator.rb

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
require 'octokit'
4+
require 'rspec/core'
5+
require 'rspec/core/formatters/base_formatter'
6+
7+
module RSpec
8+
module Github
9+
class Annotator < RSpec::Core::Formatters::BaseFormatter
10+
RSpec::Core::Formatters.register self, :start, :dump_failures, :dump_pending, :close
11+
12+
REQUIRED_ENV_VARIABLES = %w[OCTOKIT_ACCESS_TOKEN GITHUB_REPOSITORY GITHUB_SHA]
13+
14+
def start(notification)
15+
missing_env_variables = REQUIRED_ENV_VARIABLES - ENV.keys
16+
raise "Missing environment variables: #{missing_env_variables}" if missing_env_variables.any?
17+
18+
# Call check_run to create the pending check run
19+
check_run
20+
end
21+
22+
def dump_failures(examples_notification)
23+
@failures = examples_notification.failure_notifications
24+
end
25+
26+
def dump_pending(examples_notification)
27+
@pending = examples_notification.pending_notifications
28+
end
29+
30+
def close(null_notification)
31+
annotations.each_slice(50) do |annotations_group|
32+
octokit_client.update_check_run(
33+
ENV.fetch('GITHUB_REPOSITORY'),
34+
check_run.id,
35+
status: 'completed',
36+
conclusion: conclusion,
37+
output: {
38+
title: 'RSpec output',
39+
summary: 'RSpec output',
40+
annotations: annotations_group
41+
},
42+
accept: Octokit::Preview::PREVIEW_TYPES[:checks]
43+
)
44+
end
45+
end
46+
47+
private
48+
49+
def conclusion
50+
return 'failure' if @failures.any?
51+
return 'neutral' if @pending.any?
52+
53+
'success'
54+
end
55+
56+
def annotations
57+
(@failures + @pending).map do |notification|
58+
NotificationDecorator.new(notification)
59+
end
60+
end
61+
62+
def check_run
63+
@check_run ||= octokit_client.create_check_run(
64+
ENV.fetch('GITHUB_REPOSITORY'),
65+
'RSpec',
66+
ENV.fetch('GITHUB_SHA'),
67+
status: 'in_progress',
68+
accept: Octokit::Preview::PREVIEW_TYPES[:checks]
69+
)
70+
end
71+
72+
def octokit_client
73+
@octokit_client ||= Octokit::Client.new
74+
end
75+
end
76+
end
77+
end

lib/rspec/github/notification_decorator.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def initialize(notification)
1515
end
1616

1717
def line
18-
example.location.split(':')[1]
18+
example.location.split(':')[1].to_i
1919
end
2020

2121
def annotation
@@ -28,6 +28,21 @@ def path
2828
File.realpath(raw_path).sub(/\A#{workspace}#{File::SEPARATOR}/, '')
2929
end
3030

31+
def to_h
32+
{
33+
path: path,
34+
start_line: line,
35+
end_line: line,
36+
annotation_level: 'warning',
37+
title: example.full_description,
38+
message: message
39+
}
40+
end
41+
42+
def to_json(*args)
43+
to_h.to_json
44+
end
45+
3146
private
3247

3348
def example

rspec-github.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
2121
# Specify which files should be added to the gem when it is released.
2222
spec.files = Dir['{lib}/**/*']
2323

24+
spec.add_runtime_dependency 'octokit', '~> 4.0'
2425
spec.add_runtime_dependency 'rspec-core', '~> 3.0'
2526
spec.add_development_dependency 'rspec', '~> 3.0'
2627
spec.add_development_dependency 'rubocop', '~> 1.0'

spec/integration/failing_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222

2323
describe 'display all annotations' do
24-
(1..500).each do |number|
24+
(1..125).each do |number|
2525
it "test #{number}" do
2626
expect(true).to eq false
2727
end

0 commit comments

Comments
 (0)