Skip to content
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

Update how formatters are registered, allowing the addition of custom formatter #450

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/pronto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

require 'pronto/formatter/colorizable'
require 'pronto/formatter/base'
require 'pronto/formatter/formatter'
require 'pronto/formatter/text_formatter'
require 'pronto/formatter/json_formatter'
require 'pronto/formatter/git_formatter'
Expand All @@ -52,7 +53,6 @@
require 'pronto/formatter/bitbucket_server_pull_request_formatter'
require 'pronto/formatter/checkstyle_formatter'
require 'pronto/formatter/null_formatter'
require 'pronto/formatter/formatter'

module Pronto
def self.run(commit = nil, repo_path = '.',
Expand Down
2 changes: 1 addition & 1 deletion lib/pronto/formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Pronto
module Formatter
class Base
def self.name
Formatter::FORMATTERS.invert[self]
Formatter::formatters.invert[self]
end

def config
Expand Down
2 changes: 2 additions & 0 deletions lib/pronto/formatter/bitbucket_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.add('bitbucket', Pronto::Formatter::BitbucketFormatter)
4 changes: 3 additions & 1 deletion lib/pronto/formatter/bitbucket_pull_request_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ def approve_pull_request(comments_count, additions_count, client)
return if config.bitbucket_auto_approve == false

if comments_count > 0 && additions_count > 0
client.unapprove_pull_request
client.unapprove_pull_request
elsif comments_count == 0
client.approve_pull_request
end
end
end
end
end

Pronto::Formatter.add('bitbucket_pr', Pronto::Formatter::BitbucketPullRequestFormatter)
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.add('bitbucket_server_pr', Pronto::Formatter::BitbucketServerPullRequestFormatter)
2 changes: 2 additions & 0 deletions lib/pronto/formatter/checkstyle_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ def to_checkstyle_severity(pronto_level)
end
end
end

Pronto::Formatter.add('checkstyle', Pronto::Formatter::CheckstyleFormatter)
50 changes: 26 additions & 24 deletions lib/pronto/formatter/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
module Pronto
module Formatter
def self.get(names)
names ||= 'text'
Array(names).map { |name| FORMATTERS[name.to_s] || TextFormatter }
.uniq.map(&:new)
end
class << self
def add(label, formatter_klass = nil)
ashkulz marked this conversation as resolved.
Show resolved Hide resolved
unless formatter_klass.method_defined?(:format)
raise NoMethodError, "format method is not declared in the #{label} class."
end

def self.names
FORMATTERS.keys
end
base = Pronto::Formatter::Base
raise "#{label.inspect} is not a #{base}" unless formatter_klass.ancestors.include?(base)

raise ArgumentError, "formatter #{label.inspect} has already been added." if formatters.key?(label)

ashkulz marked this conversation as resolved.
Show resolved Hide resolved
formatters[label] = formatter_klass
end

FORMATTERS = {
'github' => GithubFormatter,
'github_status' => GithubStatusFormatter,
'github_combined_status' => GithubCombinedStatusFormatter,
'github_pr' => GithubPullRequestFormatter,
'github_pr_review' => GithubPullRequestReviewFormatter,
'gitlab' => GitlabFormatter,
'gitlab_mr' => GitlabMergeRequestReviewFormatter,
'bitbucket' => BitbucketFormatter,
'bitbucket_pr' => BitbucketPullRequestFormatter,
'bitbucket_server_pr' => BitbucketServerPullRequestFormatter,
'json' => JsonFormatter,
'checkstyle' => CheckstyleFormatter,
'text' => TextFormatter,
'null' => NullFormatter
}.freeze
def get(names)
names ||= 'text'
Array(names).map { |name| formatters[name.to_s] || TextFormatter }
.uniq.map(&:new)
end

def names
formatters.keys
end

def formatters
@formatters ||= {}
end
ashkulz marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
4 changes: 3 additions & 1 deletion lib/pronto/formatter/github_combined_status_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pronto
module Formatter
class GithubCombinedStatusFormatter
class GithubCombinedStatusFormatter < Base
def format(messages, repo, _)
client = Github.new(repo)
head = repo.head_commit_sha
Expand All @@ -22,3 +22,5 @@ def create_status(client, sha, messages)
end
end
end

Pronto::Formatter.add('github_combined_status', Pronto::Formatter::GithubCombinedStatusFormatter)
2 changes: 2 additions & 0 deletions lib/pronto/formatter/github_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.add('github', Pronto::Formatter::GithubFormatter)
2 changes: 2 additions & 0 deletions lib/pronto/formatter/github_pull_request_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ def line_number(message, patches)
end
end
end

Pronto::Formatter.add('github_pr', Pronto::Formatter::GithubPullRequestFormatter)
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ def line_number(message, patches)
end
end
end

Pronto::Formatter.add('github_pr_review', Pronto::Formatter::GithubPullRequestReviewFormatter)
4 changes: 3 additions & 1 deletion lib/pronto/formatter/github_status_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pronto
module Formatter
class GithubStatusFormatter
class GithubStatusFormatter < Base
def format(messages, repo, _)
client = Github.new(repo)
head = repo.head_commit_sha
Expand All @@ -26,3 +26,5 @@ def create_status(client, sha, runner, messages)
end
end
end

Pronto::Formatter.add('github_status', Pronto::Formatter::GithubStatusFormatter)
2 changes: 1 addition & 1 deletion lib/pronto/formatter/github_status_formatter/sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Pronto
module Formatter
class GithubStatusFormatter
class GithubStatusFormatter < Base
class Sentence
def initialize(words)
@words = words
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pronto
module Formatter
class GithubStatusFormatter
class GithubStatusFormatter < Base
class StatusBuilder
def initialize(runner, messages)
@runner = runner
Expand Down
2 changes: 2 additions & 0 deletions lib/pronto/formatter/gitlab_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.add('gitlab', Pronto::Formatter::GitlabFormatter)
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.add('gitlab_mr', Pronto::Formatter::GitlabMergeRequestReviewFormatter)
2 changes: 2 additions & 0 deletions lib/pronto/formatter/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ def format(messages, _repo, _patches)
end
end
end

Pronto::Formatter.add('json', Pronto::Formatter::JsonFormatter)
2 changes: 2 additions & 0 deletions lib/pronto/formatter/null_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ def format(_messages, _repo, _patches); end
end
end
end

Pronto::Formatter.add('null', Pronto::Formatter::NullFormatter)
2 changes: 2 additions & 0 deletions lib/pronto/formatter/text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ def format(messages, _repo, _patches)
end
end
end

Pronto::Formatter.add('text', Pronto::Formatter::TextFormatter)
45 changes: 45 additions & 0 deletions spec/pronto/formatter/formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
module Pronto
module Formatter
describe '.add' do
context 'format method not implementend' do
subject { Formatter.add(formatter_label, formatter) }

let(:formatter_label) { 'formatter_without_method' }
let(:formatter) { Class.new(Pronto::Formatter::Base) }

specify do
-> { subject }.should raise_error(
NoMethodError, "format method is not declared in the #{formatter_label} class."
)
end
end

context 'formatter class is not Formatter::Base' do
subject { Formatter.add(formatter_label, formatter) }

let(:formatter_label) { 'formatter_without_base_class' }
let(:formatter) do
Class.new do
def format(_messages, _repo, _patches); end
end
end

specify do
-> { subject }.should raise_error("#{formatter_label.inspect} is not a #{Pronto::Formatter::Base}")
end
end

context 'there is a formater with same name' do
subject { Formatter.add(formatter_label, formatter) }

let(:formatter_label) { 'json' }
let(:formatter) do
Class.new(Pronto::Formatter::Base) do
def format(_messages, _repo, _patches); end
end
end

specify do
-> { subject }.should raise_error("formatter #{formatter_label.inspect} has already been added.")
end
end
end

describe '.get' do
context 'single' do
subject { Formatter.get(name).first }
Expand Down