From eb433d99c7f158d027e1c4cfa07c90d02e3bde17 Mon Sep 17 00:00:00 2001 From: Emilio Date: Mon, 23 Oct 2023 23:26:16 -0300 Subject: [PATCH] Change how to register formatter, removing the need to pass the label. --- lib/pronto/formatter/base.rb | 2 +- lib/pronto/formatter/bitbucket_formatter.rb | 6 +++++- .../bitbucket_pull_request_formatter.rb | 6 +++++- .../bitbucket_server_pull_request_formatter.rb | 6 +++++- lib/pronto/formatter/checkstyle_formatter.rb | 6 +++++- lib/pronto/formatter/formatter.rb | 8 ++++---- .../github_combined_status_formatter.rb | 6 +++++- lib/pronto/formatter/github_formatter.rb | 6 +++++- .../formatter/github_pull_request_formatter.rb | 6 +++++- .../github_pull_request_review_formatter.rb | 6 +++++- lib/pronto/formatter/github_status_formatter.rb | 6 +++++- lib/pronto/formatter/gitlab_formatter.rb | 6 +++++- .../gitlab_merge_request_review_formatter.rb | 6 +++++- lib/pronto/formatter/json_formatter.rb | 6 +++++- lib/pronto/formatter/null_formatter.rb | 6 +++++- lib/pronto/formatter/text_formatter.rb | 6 +++++- spec/pronto/formatter/formatter_spec.rb | 17 ++++++++++------- 17 files changed, 85 insertions(+), 26 deletions(-) diff --git a/lib/pronto/formatter/base.rb b/lib/pronto/formatter/base.rb index 440ddcd5..7008a7d4 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 86ca5d8c..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 @@ -16,4 +20,4 @@ def line_number(message, _) end end -Pronto::Formatter.register('bitbucket', Pronto::Formatter::BitbucketFormatter) +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 ac2f5957..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 @@ -26,4 +30,4 @@ def approve_pull_request(comments_count, additions_count, client) end end -Pronto::Formatter.register('bitbucket_pr', Pronto::Formatter::BitbucketPullRequestFormatter) +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 306ba878..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 @@ -16,4 +20,4 @@ def line_number(message, _) end end -Pronto::Formatter.register('bitbucket_server_pr', Pronto::Formatter::BitbucketServerPullRequestFormatter) +Pronto::Formatter.register(Pronto::Formatter::BitbucketServerPullRequestFormatter) diff --git a/lib/pronto/formatter/checkstyle_formatter.rb b/lib/pronto/formatter/checkstyle_formatter.rb index ee45a493..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 @@ -58,4 +62,4 @@ def to_checkstyle_severity(pronto_level) end end -Pronto::Formatter.register('checkstyle', Pronto::Formatter::CheckstyleFormatter) +Pronto::Formatter.register(Pronto::Formatter::CheckstyleFormatter) diff --git a/lib/pronto/formatter/formatter.rb b/lib/pronto/formatter/formatter.rb index 8faa0304..8124029e 100644 --- a/lib/pronto/formatter/formatter.rb +++ b/lib/pronto/formatter/formatter.rb @@ -1,15 +1,15 @@ module Pronto module Formatter class << self - def register(label, formatter_klass) + def register(formatter_klass) unless formatter_klass.method_defined?(:format) - raise NoMethodError, "format method is not declared in the #{label} class." + raise NoMethodError, "format method is not declared in the #{formatter_klass.name} class." end base = Pronto::Formatter::Base - raise "#{label.inspect} is not a #{base}" unless formatter_klass.ancestors.include?(base) + raise "#{formatter_klass.name} is not a #{base}" unless formatter_klass.ancestors.include?(base) - formatters[label] = formatter_klass + formatters[formatter_klass.name] = formatter_klass end def get(names) diff --git a/lib/pronto/formatter/github_combined_status_formatter.rb b/lib/pronto/formatter/github_combined_status_formatter.rb index a2b2e830..36d64c69 100644 --- a/lib/pronto/formatter/github_combined_status_formatter.rb +++ b/lib/pronto/formatter/github_combined_status_formatter.rb @@ -3,6 +3,10 @@ module Pronto module Formatter class GithubCombinedStatusFormatter < Base + def self.name + 'github_combined_status' + end + def format(messages, repo, _) client = Github.new(repo) head = repo.head_commit_sha @@ -23,4 +27,4 @@ def create_status(client, sha, messages) end end -Pronto::Formatter.register('github_combined_status', Pronto::Formatter::GithubCombinedStatusFormatter) +Pronto::Formatter.register(Pronto::Formatter::GithubCombinedStatusFormatter) diff --git a/lib/pronto/formatter/github_formatter.rb b/lib/pronto/formatter/github_formatter.rb index 3e50b433..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 @@ -16,4 +20,4 @@ def line_number(message, _) end end -Pronto::Formatter.register('github', Pronto::Formatter::GithubFormatter) +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 b519ff0e..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 @@ -17,4 +21,4 @@ def line_number(message, patches) end end -Pronto::Formatter.register('github_pr', Pronto::Formatter::GithubPullRequestFormatter) +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 82108199..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 @@ -23,4 +27,4 @@ def line_number(message, patches) end end -Pronto::Formatter.register('github_pr_review', Pronto::Formatter::GithubPullRequestReviewFormatter) +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 33a67b8f..218d8170 100644 --- a/lib/pronto/formatter/github_status_formatter.rb +++ b/lib/pronto/formatter/github_status_formatter.rb @@ -3,6 +3,10 @@ module Pronto module Formatter class GithubStatusFormatter < Base + def self.name + 'github_status' + end + def format(messages, repo, _) client = Github.new(repo) head = repo.head_commit_sha @@ -27,4 +31,4 @@ def create_status(client, sha, runner, messages) end end -Pronto::Formatter.register('github_status', Pronto::Formatter::GithubStatusFormatter) +Pronto::Formatter.register(Pronto::Formatter::GithubStatusFormatter) diff --git a/lib/pronto/formatter/gitlab_formatter.rb b/lib/pronto/formatter/gitlab_formatter.rb index 5b3fc875..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 @@ -16,4 +20,4 @@ def line_number(message, _) end end -Pronto::Formatter.register('gitlab', Pronto::Formatter::GitlabFormatter) +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 85fb898d..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 @@ -28,4 +32,4 @@ def line_number(message, _) end end -Pronto::Formatter.register('gitlab_mr', Pronto::Formatter::GitlabMergeRequestReviewFormatter) +Pronto::Formatter.register(Pronto::Formatter::GitlabMergeRequestReviewFormatter) diff --git a/lib/pronto/formatter/json_formatter.rb b/lib/pronto/formatter/json_formatter.rb index 554a4cea..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 @@ -19,4 +23,4 @@ def format(messages, _repo, _patches) end end -Pronto::Formatter.register('json', Pronto::Formatter::JsonFormatter) +Pronto::Formatter.register(Pronto::Formatter::JsonFormatter) diff --git a/lib/pronto/formatter/null_formatter.rb b/lib/pronto/formatter/null_formatter.rb index db02fc1b..a44d2c28 100644 --- a/lib/pronto/formatter/null_formatter.rb +++ b/lib/pronto/formatter/null_formatter.rb @@ -1,9 +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('null', Pronto::Formatter::NullFormatter) +Pronto::Formatter.register(Pronto::Formatter::NullFormatter) diff --git a/lib/pronto/formatter/text_formatter.rb b/lib/pronto/formatter/text_formatter.rb index 6de43c9d..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) @@ -14,4 +18,4 @@ def format(messages, _repo, _patches) end end -Pronto::Formatter.register('text', Pronto::Formatter::TextFormatter) +Pronto::Formatter.register(Pronto::Formatter::TextFormatter) diff --git a/spec/pronto/formatter/formatter_spec.rb b/spec/pronto/formatter/formatter_spec.rb index 5cb38155..ed090a40 100644 --- a/spec/pronto/formatter/formatter_spec.rb +++ b/spec/pronto/formatter/formatter_spec.rb @@ -2,30 +2,33 @@ module Pronto module Formatter describe '.register' do context 'format method not implementend' do - subject { Formatter.register(formatter_label, formatter) } + subject { Formatter.register(formatter) } - let(:formatter_label) { 'formatter_without_method' } - let(:formatter) { Class.new(Pronto::Formatter::Base) } + 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_label} class." + 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_label, formatter) } + subject { Formatter.register(formatter) } - let(:formatter_label) { 'formatter_without_base_class' } 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("#{formatter_label.inspect} is not a #{Pronto::Formatter::Base}") + -> { subject }.should raise_error(RuntimeError, "#{formatter.name} is not a #{Pronto::Formatter::Base}") end end end