diff --git a/lib/pronto.rb b/lib/pronto.rb index 627e6698..5e85c99e 100644 --- a/lib/pronto.rb +++ b/lib/pronto.rb @@ -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' @@ -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 = '.', diff --git a/lib/pronto/formatter/base.rb b/lib/pronto/formatter/base.rb index 7e72a173..0eee903f 100644 --- a/lib/pronto/formatter/base.rb +++ b/lib/pronto/formatter/base.rb @@ -2,7 +2,7 @@ module Pronto module Formatter class Base def self.name - Formatter::FORMATTERS.invert[self] + raise NoMethodError, 'Must be implemented in subclasses.' end def config diff --git a/lib/pronto/formatter/bitbucket_formatter.rb b/lib/pronto/formatter/bitbucket_formatter.rb index 522cd5cd..129fe1b4 100644 --- a/lib/pronto/formatter/bitbucket_formatter.rb +++ b/lib/pronto/formatter/bitbucket_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class BitbucketFormatter < CommitFormatter + def self.name + 'bitbucket' + end + def client_module Bitbucket end @@ -15,3 +19,5 @@ def line_number(message, _) end end end + +Pronto::Formatter.register(Pronto::Formatter::BitbucketFormatter) diff --git a/lib/pronto/formatter/bitbucket_pull_request_formatter.rb b/lib/pronto/formatter/bitbucket_pull_request_formatter.rb index b9df69c8..303dfd2d 100644 --- a/lib/pronto/formatter/bitbucket_pull_request_formatter.rb +++ b/lib/pronto/formatter/bitbucket_pull_request_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class BitbucketPullRequestFormatter < PullRequestFormatter + def self.name + 'bitbucket_pr' + end + def client_module Bitbucket end @@ -17,7 +21,7 @@ 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 @@ -25,3 +29,5 @@ def approve_pull_request(comments_count, additions_count, client) end end end + +Pronto::Formatter.register(Pronto::Formatter::BitbucketPullRequestFormatter) diff --git a/lib/pronto/formatter/bitbucket_server_pull_request_formatter.rb b/lib/pronto/formatter/bitbucket_server_pull_request_formatter.rb index 7e16b2b4..475cac44 100644 --- a/lib/pronto/formatter/bitbucket_server_pull_request_formatter.rb +++ b/lib/pronto/formatter/bitbucket_server_pull_request_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class BitbucketServerPullRequestFormatter < PullRequestFormatter + def self.name + 'bitbucket_server_pr' + end + def client_module BitbucketServer end @@ -15,3 +19,5 @@ def line_number(message, _) end end end + +Pronto::Formatter.register(Pronto::Formatter::BitbucketServerPullRequestFormatter) diff --git a/lib/pronto/formatter/checkstyle_formatter.rb b/lib/pronto/formatter/checkstyle_formatter.rb index a84e94d7..68256203 100644 --- a/lib/pronto/formatter/checkstyle_formatter.rb +++ b/lib/pronto/formatter/checkstyle_formatter.rb @@ -3,6 +3,10 @@ module Pronto module Formatter class CheckstyleFormatter < Base + def self.name + 'checkstyle' + end + def initialize @output = '' end @@ -57,3 +61,5 @@ def to_checkstyle_severity(pronto_level) end end end + +Pronto::Formatter.register(Pronto::Formatter::CheckstyleFormatter) diff --git a/lib/pronto/formatter/formatter.rb b/lib/pronto/formatter/formatter.rb index 722c8575..1325b210 100644 --- a/lib/pronto/formatter/formatter.rb +++ b/lib/pronto/formatter/formatter.rb @@ -1,30 +1,27 @@ 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 register(formatter_klass) + unless formatter_klass.method_defined?(:format) + raise NoMethodError, "format method is not declared in the #{formatter_klass.name} class." + end - def self.names - FORMATTERS.keys - end + base = Pronto::Formatter::Base + raise "#{formatter_klass.name} is not a #{base}" unless formatter_klass.ancestors.include?(base) + + @formatters ||= {} + @formatters[formatter_klass.name] = 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 + end end end diff --git a/lib/pronto/formatter/github_combined_status_formatter.rb b/lib/pronto/formatter/github_combined_status_formatter.rb index 81e6394e..36d64c69 100644 --- a/lib/pronto/formatter/github_combined_status_formatter.rb +++ b/lib/pronto/formatter/github_combined_status_formatter.rb @@ -2,7 +2,11 @@ module Pronto module Formatter - class GithubCombinedStatusFormatter + class GithubCombinedStatusFormatter < Base + def self.name + 'github_combined_status' + end + def format(messages, repo, _) client = Github.new(repo) head = repo.head_commit_sha @@ -22,3 +26,5 @@ def create_status(client, sha, messages) end end end + +Pronto::Formatter.register(Pronto::Formatter::GithubCombinedStatusFormatter) diff --git a/lib/pronto/formatter/github_formatter.rb b/lib/pronto/formatter/github_formatter.rb index 2706f2af..c2dddb90 100644 --- a/lib/pronto/formatter/github_formatter.rb +++ b/lib/pronto/formatter/github_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class GithubFormatter < CommitFormatter + def self.name + 'github' + end + def client_module Github end @@ -15,3 +19,5 @@ def line_number(message, _) end end end + +Pronto::Formatter.register(Pronto::Formatter::GithubFormatter) diff --git a/lib/pronto/formatter/github_pull_request_formatter.rb b/lib/pronto/formatter/github_pull_request_formatter.rb index e791fc7f..93858845 100644 --- a/lib/pronto/formatter/github_pull_request_formatter.rb +++ b/lib/pronto/formatter/github_pull_request_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class GithubPullRequestFormatter < PullRequestFormatter + def self.name + 'github_pr' + end + def client_module Github end @@ -16,3 +20,5 @@ def line_number(message, patches) end end end + +Pronto::Formatter.register(Pronto::Formatter::GithubPullRequestFormatter) diff --git a/lib/pronto/formatter/github_pull_request_review_formatter.rb b/lib/pronto/formatter/github_pull_request_review_formatter.rb index aa72880b..f510ccf5 100644 --- a/lib/pronto/formatter/github_pull_request_review_formatter.rb +++ b/lib/pronto/formatter/github_pull_request_review_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class GithubPullRequestReviewFormatter < PullRequestFormatter + def self.name + 'github_pr_review' + end + def client_module Github end @@ -22,3 +26,5 @@ def line_number(message, patches) end end end + +Pronto::Formatter.register(Pronto::Formatter::GithubPullRequestReviewFormatter) diff --git a/lib/pronto/formatter/github_status_formatter.rb b/lib/pronto/formatter/github_status_formatter.rb index b717c2fd..218d8170 100644 --- a/lib/pronto/formatter/github_status_formatter.rb +++ b/lib/pronto/formatter/github_status_formatter.rb @@ -2,7 +2,11 @@ module Pronto module Formatter - class GithubStatusFormatter + class GithubStatusFormatter < Base + def self.name + 'github_status' + end + def format(messages, repo, _) client = Github.new(repo) head = repo.head_commit_sha @@ -26,3 +30,5 @@ def create_status(client, sha, runner, messages) end end end + +Pronto::Formatter.register(Pronto::Formatter::GithubStatusFormatter) diff --git a/lib/pronto/formatter/github_status_formatter/sentence.rb b/lib/pronto/formatter/github_status_formatter/sentence.rb index b0f4b927..504ff677 100644 --- a/lib/pronto/formatter/github_status_formatter/sentence.rb +++ b/lib/pronto/formatter/github_status_formatter/sentence.rb @@ -1,6 +1,6 @@ module Pronto module Formatter - class GithubStatusFormatter + class GithubStatusFormatter < Base class Sentence def initialize(words) @words = words diff --git a/lib/pronto/formatter/github_status_formatter/status_builder.rb b/lib/pronto/formatter/github_status_formatter/status_builder.rb index 884c4a91..613fa6ef 100644 --- a/lib/pronto/formatter/github_status_formatter/status_builder.rb +++ b/lib/pronto/formatter/github_status_formatter/status_builder.rb @@ -2,7 +2,7 @@ module Pronto module Formatter - class GithubStatusFormatter + class GithubStatusFormatter < Base class StatusBuilder def initialize(runner, messages) @runner = runner diff --git a/lib/pronto/formatter/gitlab_formatter.rb b/lib/pronto/formatter/gitlab_formatter.rb index be4263d3..2daa4086 100644 --- a/lib/pronto/formatter/gitlab_formatter.rb +++ b/lib/pronto/formatter/gitlab_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class GitlabFormatter < CommitFormatter + def self.name + 'gitlab' + end + def client_module Gitlab end @@ -15,3 +19,5 @@ def line_number(message, _) end end end + +Pronto::Formatter.register(Pronto::Formatter::GitlabFormatter) diff --git a/lib/pronto/formatter/gitlab_merge_request_review_formatter.rb b/lib/pronto/formatter/gitlab_merge_request_review_formatter.rb index 865dc832..6ec993e3 100644 --- a/lib/pronto/formatter/gitlab_merge_request_review_formatter.rb +++ b/lib/pronto/formatter/gitlab_merge_request_review_formatter.rb @@ -1,6 +1,10 @@ module Pronto module Formatter class GitlabMergeRequestReviewFormatter < PullRequestFormatter + def self.name + 'gitlab_mr' + end + def client_module Gitlab end @@ -27,3 +31,5 @@ def line_number(message, _) end end end + +Pronto::Formatter.register(Pronto::Formatter::GitlabMergeRequestReviewFormatter) diff --git a/lib/pronto/formatter/json_formatter.rb b/lib/pronto/formatter/json_formatter.rb index c0e114ac..cc2a775c 100644 --- a/lib/pronto/formatter/json_formatter.rb +++ b/lib/pronto/formatter/json_formatter.rb @@ -3,6 +3,10 @@ module Pronto module Formatter class JsonFormatter < Base + def self.name + 'json' + end + def format(messages, _repo, _patches) messages.map do |message| lineno = message.line.new_lineno if message.line @@ -18,3 +22,5 @@ def format(messages, _repo, _patches) end end end + +Pronto::Formatter.register(Pronto::Formatter::JsonFormatter) diff --git a/lib/pronto/formatter/null_formatter.rb b/lib/pronto/formatter/null_formatter.rb index cfd852a5..a44d2c28 100644 --- a/lib/pronto/formatter/null_formatter.rb +++ b/lib/pronto/formatter/null_formatter.rb @@ -1,7 +1,13 @@ module Pronto module Formatter class NullFormatter < Base + def self.name + 'null' + end + def format(_messages, _repo, _patches); end end end end + +Pronto::Formatter.register(Pronto::Formatter::NullFormatter) diff --git a/lib/pronto/formatter/text_formatter.rb b/lib/pronto/formatter/text_formatter.rb index 270e86e5..3b31b724 100644 --- a/lib/pronto/formatter/text_formatter.rb +++ b/lib/pronto/formatter/text_formatter.rb @@ -3,6 +3,10 @@ module Pronto module Formatter class TextFormatter < Base + def self.name + 'text' + end + def format(messages, _repo, _patches) messages.map do |message| message_format = config.message_format(self.class.name) @@ -13,3 +17,5 @@ def format(messages, _repo, _patches) end end end + +Pronto::Formatter.register(Pronto::Formatter::TextFormatter) diff --git a/spec/pronto/formatter/formatter_spec.rb b/spec/pronto/formatter/formatter_spec.rb index 324937d9..6b4c0c45 100644 --- a/spec/pronto/formatter/formatter_spec.rb +++ b/spec/pronto/formatter/formatter_spec.rb @@ -1,5 +1,43 @@ module Pronto module Formatter + describe '.register' do + context 'format method not implementend' do + subject { Formatter.register(formatter) } + + let(:formatter) do + Class.new(Pronto::Formatter::Base) do + def self.name + 'custom_formatter' + end + end + end + + specify do + -> { subject }.should raise_error( + NoMethodError, "format method is not declared in the #{formatter.name} class." + ) + end + end + + context 'formatter class is not Formatter::Base' do + subject { Formatter.register(formatter) } + + let(:formatter) do + Class.new do + def self.name + 'custom_formatter' + end + + def format(_messages, _repo, _patches); end + end + end + + specify do + -> { subject }.should raise_error(RuntimeError, "#{formatter.name} is not a #{Pronto::Formatter::Base}") + end + end + end + describe '.get' do context 'single' do subject { Formatter.get(name).first }