Skip to content

WIP: codeclimate output support #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/puppet-lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def report(problems)

next unless message[:kind] == :fixed || [message[:kind], :all].include?(configuration.error_level)

if configuration.json || configuration.sarif
if configuration.json || configuration.sarif || configuration.codeclimate
message['context'] = get_context(message) if configuration.with_context
json << message
else
Expand Down
47 changes: 47 additions & 0 deletions lib/puppet-lint/bin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pathname'
require 'uri'
require 'puppet-lint/optparser'
require 'digest'

# Internal: The logic of the puppet-lint bin script, contained in a class for
# ease of testing.
Expand Down Expand Up @@ -95,6 +96,8 @@ def run

if PuppetLint.configuration.sarif
report_sarif(all_problems, full_base_path, full_base_path_uri)
elsif PuppetLint.configuration.codeclimate
report_codeclimate(all_problems)
elsif PuppetLint.configuration.json
all_problems.each do |problems|
problems.each do |problem|
Expand Down Expand Up @@ -150,4 +153,48 @@ def report_sarif(problems, base_path, base_path_uri)
end
puts JSON.pretty_generate(sarif)
end

# Internal: Print the reported problems in code climate format to stdout.
#
# problems - An Array of problem Hashes as returned by
# PuppetLint::Checks#run.
#
# Returns nothing.
def report_codeclimate(problems)
report = []
problems.each do |messages|
messages.each do |message|
case message[:kind].downcase
when :warning
severity = 'minor'
when :error
severity = 'major'
else
next
end

issue = {
type: :issue,
check_name: message[:check],
description: message[:message],
categories: [:Style],
severity: severity,
location: {
path: message[:path],
lines: {
begin: message[:line],
end: message[:line],
}
},
fingerprint: Digest::MD5.hexdigest(Marshal.dump(message))
}

if message.key?(:description) && message.key?(:help_uri)
issue[:content] = "#{message[:description].chomp('.')}. See [this page](#{message[:help_uri]}) for more information about the `#{message[:check]}` check."
end
report << issue
end
end
puts JSON.pretty_generate(report)
end
end
1 change: 1 addition & 0 deletions lib/puppet-lint/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def defaults
self.fix = false
self.json = false
self.sarif = false
self.codeclimate = false
self.show_ignored = false
self.ignore_paths = ['vendor/**/*.pp']
self.github_actions = ENV.key?('GITHUB_ACTION')
Expand Down
4 changes: 4 additions & 0 deletions lib/puppet-lint/optparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def self.build(args = [])
PuppetLint.configuration.sarif = true
end

opts.on('--codeclimate', 'Log output in code climate compatible format') do
PuppetLint.configuration.codeclimate = true
end

opts.on('--list-checks', 'List available check names.') do
PuppetLint.configuration.list_checks = true
end
Expand Down