Skip to content

Commit

Permalink
Add --format github option
Browse files Browse the repository at this point in the history
  • Loading branch information
r7kamura committed Oct 28, 2024
1 parent abbb7e2 commit 4eb2157
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 12 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,14 @@ e.g., `codeclimate analyze -e duplication`

## Output formats

Reek supports 5 output formats:
Reek supports 6 output formats:

* plain text (default)
* HTML (`--format html`)
* YAML (`--format yaml`, see also [YAML Reports](docs/YAML-Reports.md))
* JSON (`--format json`)
* XML (`--format xml`)
* HTML (`--format html`)
* YAML (`--format yaml`, see also [YAML Reports](docs/YAML-Reports.md))
* JSON (`--format json`)
* XML (`--format xml`)
* GitHub (`--format github`)

## Working with Rails

Expand Down
4 changes: 2 additions & 2 deletions lib/reek/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def set_generate_todo_list_options
def set_alternative_formatter_options
parser.separator "\nReport format:"
parser.on(
'-f', '--format FORMAT', [:html, :text, :yaml, :json, :xml],
'-f', '--format FORMAT', [:html, :text, :yaml, :json, :xml, :github],
'Report smells in the given format:',
' html', ' text (default)', ' yaml', ' json', ' xml') do |opt|
' html', ' text (default)', ' yaml', ' json', ' xml', ' github') do |opt|
self.report_format = opt
end
end
Expand Down
12 changes: 7 additions & 5 deletions lib/reek/report.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative 'report/github_report'
require_relative 'report/html_report'
require_relative 'report/json_report'
require_relative 'report/text_report'
Expand All @@ -16,11 +17,12 @@ module Reek
# Reek reporting functionality.
module Report
REPORT_CLASSES = {
yaml: YAMLReport,
json: JSONReport,
html: HTMLReport,
xml: XMLReport,
text: TextReport
yaml: YAMLReport,
json: JSONReport,
html: HTMLReport,
xml: XMLReport,
text: TextReport,
github: GithubReport
}.freeze

LOCATION_FORMATTERS = {
Expand Down
55 changes: 55 additions & 0 deletions lib/reek/report/github_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require_relative 'base_report'

module Reek
module Report
#
# Displays smells as GitHub Workflow commands.
#
# @public
#
class GithubReport < BaseReport
def show(out = $stdout)
out.print(workflow_commands.join)
end

private

def workflow_commands
smells.map do |smell|
WorkflowCommand.new(smell)
end
end

# Represents a smell as a GitHub Workflow command.
class WorkflowCommand
def initialize(smell)
@smell = smell
end

def to_s
format(
"::warning file=%<file>s,line=%<line>d::%<message>s\n",
file: file,
line: line,
message: message)
end

private

def file
@smell.source
end

def line
@smell.lines.first
end

def message
@smell.base_message.gsub('%', '%25').gsub("\r", '%0D').gsub("\n", '%0A')
end
end
end
end
end
44 changes: 44 additions & 0 deletions spec/reek/report/github_report_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative '../../spec_helper'
require_lib 'reek/examiner'
require_lib 'reek/report/json_report'

RSpec.describe Reek::Report::GithubReport do
let(:instance) do
described_class.new
end

let(:examiner) do
Reek::Examiner.new(source)
end

context 'with empty source' do
let(:source) do
''
end

it 'prints empty string' do
instance.add_examiner(examiner)
expect { instance.show }.not_to output.to_stdout
end
end

context 'with smelly source' do
let(:source) do
<<~RUBY
def simple(a)
a[3]
end
RUBY
end

it 'prints smells as GitHub Workflow commands' do
instance.add_examiner(examiner)
expect { instance.show }.to output(
<<~TEXT
::warning file=string,line=1::UncommunicativeParameterName: simple has the parameter name 'a'
::warning file=string,line=1::UtilityFunction: simple doesn't depend on instance state (maybe move it to another class?)
TEXT
).to_stdout
end
end
end

0 comments on commit 4eb2157

Please # to comment.