Skip to content

Commit 3f8feaf

Browse files
authored
Merge pull request #34 from ekohl/add-github-actions-annotations
Add GitHub Actions annotations
2 parents c362aa6 + b70093d commit 3f8feaf

File tree

5 files changed

+74
-9
lines changed

5 files changed

+74
-9
lines changed

lib/puppet-lint.rb

Lines changed: 21 additions & 8 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.
@@ -158,15 +169,17 @@ def report(problems)
158169

159170
message[:KIND] = message[:kind].to_s.upcase
160171

161-
if message[:kind] == :fixed || [message[:kind], :all].include?(configuration.error_level)
162-
if configuration.json
163-
message['context'] = get_context(message) if configuration.with_context
164-
json << message
165-
else
166-
format_message(message)
167-
print_context(message) if configuration.with_context
168-
end
172+
next unless message[:kind] == :fixed || [message[:kind], :all].include?(configuration.error_level)
173+
174+
if configuration.json
175+
message['context'] = get_context(message) if configuration.with_context
176+
json << message
177+
else
178+
format_message(message)
179+
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)