Skip to content

Commit b70093d

Browse files
committed
Add GitHub Actions annotations
It is possible to get inline annotations with GitHub Actions. This is based on Rubocop's formatter. It's implemented as a configuration option that's dynamic by default. This means it will show up in actions, but not in other environments. Overriding is still possible.
1 parent 954bafb commit b70093d

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

lib/puppet-lint.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'puppet-lint/configuration'
88
require 'puppet-lint/data'
99
require 'puppet-lint/checks'
10+
require 'puppet-lint/report/github'
1011
require 'puppet-lint/bin'
1112
require 'puppet-lint/monkeypatches'
1213

@@ -121,6 +122,16 @@ def format_message(message)
121122
puts " #{message[:reason]}" if message[:kind] == :ignored && !message[:reason].nil?
122123
end
123124

125+
# Internal: Format a problem message and print it to STDOUT so GitHub Actions
126+
# recognizes it as an annotation.
127+
#
128+
# message - A Hash containing all the information about a problem.
129+
#
130+
# Returns nothing.
131+
def print_github_annotation(message)
132+
puts PuppetLint::Report::GitHubActionsReporter.format_problem(path, message)
133+
end
134+
124135
# Internal: Get the line of the manifest on which the problem was found
125136
#
126137
# message - A Hash containing all the information about a problem.
@@ -167,6 +178,8 @@ def report(problems)
167178
format_message(message)
168179
print_context(message) if configuration.with_context
169180
end
181+
182+
print_github_annotation(message) if configuration.github_actions
170183
end
171184
puts JSON.pretty_generate(json) if configuration.json
172185

lib/puppet-lint/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def defaults
152152
self.json = false
153153
self.show_ignored = false
154154
self.ignore_paths = ['vendor/**/*.pp']
155+
self.github_actions = ENV.key?('GITHUB_ACTION')
155156
end
156157
end
157158
end

lib/puppet-lint/report/github.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
class PuppetLint
4+
module Report
5+
# This formatter formats report data as GitHub Workflow commands resulting
6+
# in GitHub check annotations when run within GitHub Actions.
7+
class GitHubActionsReporter
8+
ESCAPE_MAP = { '%' => '%25', "\n" => '%0A', "\r" => '%0D' }.freeze
9+
10+
def self.format_problem(file, problem)
11+
format(
12+
"\n::%<severity>s file=%<file>s,line=%<line>d,col=%<column>d::%<message>s (check: %<check>s)\n",
13+
:severity => problem[:kind],
14+
:file => file,
15+
:line => problem[:line],
16+
:column => problem[:column],
17+
:message => github_escape(problem[:message]),
18+
:check => problem[:check]
19+
)
20+
end
21+
22+
def self.github_escape(string)
23+
string.gsub(Regexp.union(ESCAPE_MAP.keys), ESCAPE_MAP)
24+
end
25+
end
26+
end
27+
end

spec/puppet-lint/configuration_spec.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
end
5050

5151
it 'should be able to set sane defaults' do
52-
subject.defaults
52+
override_env do
53+
ENV.delete('GITHUB_ACTION')
54+
subject.defaults
55+
end
5356

5457
expect(subject.settings).to eq(
5558
'with_filename' => false,
@@ -58,9 +61,27 @@
5861
'log_format' => '',
5962
'with_context' => false,
6063
'fix' => false,
64+
'github_actions' => false,
6165
'show_ignored' => false,
6266
'json' => false,
6367
'ignore_paths' => ['vendor/**/*.pp']
6468
)
6569
end
70+
71+
it 'detects github actions' do
72+
override_env do
73+
ENV['GITHUB_ACTION'] = 'action'
74+
subject.defaults
75+
end
76+
77+
expect(subject.settings['github_actions']).to be(true)
78+
end
79+
80+
def override_env
81+
old_env = ENV.clone
82+
yield
83+
ensure
84+
ENV.clear
85+
ENV.update(old_env)
86+
end
6687
end

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Disable GitHub Actions reporting since it breaks the test suite
2+
ENV.delete('GITHUB_ACTION')
3+
14
if ENV['COVERAGE'] == 'yes' && RUBY_VERSION.start_with?('2.6.')
25
require 'simplecov'
36
SimpleCov.start do

0 commit comments

Comments
 (0)