From c59dbdf121abcfba25fce471291a59ce1610be2e Mon Sep 17 00:00:00 2001 From: Alexander Fisher Date: Thu, 19 Jan 2023 17:05:26 +0000 Subject: [PATCH] WIP: codeclimate output support --- lib/puppet-lint.rb | 2 +- lib/puppet-lint/bin.rb | 47 ++++++++++++++++++++++++++++++++ lib/puppet-lint/configuration.rb | 1 + lib/puppet-lint/optparser.rb | 4 +++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/puppet-lint.rb b/lib/puppet-lint.rb index a45e31da..0f6be897 100644 --- a/lib/puppet-lint.rb +++ b/lib/puppet-lint.rb @@ -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 diff --git a/lib/puppet-lint/bin.rb b/lib/puppet-lint/bin.rb index 7457d51c..527b7c95 100644 --- a/lib/puppet-lint/bin.rb +++ b/lib/puppet-lint/bin.rb @@ -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. @@ -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| @@ -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 diff --git a/lib/puppet-lint/configuration.rb b/lib/puppet-lint/configuration.rb index 21bca77f..0254fb27 100644 --- a/lib/puppet-lint/configuration.rb +++ b/lib/puppet-lint/configuration.rb @@ -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') diff --git a/lib/puppet-lint/optparser.rb b/lib/puppet-lint/optparser.rb index e4d3c76c..09a4c42c 100644 --- a/lib/puppet-lint/optparser.rb +++ b/lib/puppet-lint/optparser.rb @@ -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