Skip to content

Commit

Permalink
Change how to register formatter, removing the need to pass the label.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio2hd committed Oct 24, 2023
1 parent 733c312 commit eb433d9
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 26 deletions.
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]
raise NoMethodError, "Must be implemented in subclasses."
end

def config
Expand Down
6 changes: 5 additions & 1 deletion lib/pronto/formatter/bitbucket_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class BitbucketFormatter < CommitFormatter
def self.name
'bitbucket'
end

def client_module
Bitbucket
end
Expand All @@ -16,4 +20,4 @@ def line_number(message, _)
end
end

Pronto::Formatter.register('bitbucket', Pronto::Formatter::BitbucketFormatter)
Pronto::Formatter.register(Pronto::Formatter::BitbucketFormatter)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/bitbucket_pull_request_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class BitbucketPullRequestFormatter < PullRequestFormatter
def self.name
'bitbucket_pr'
end

def client_module
Bitbucket
end
Expand All @@ -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)
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class BitbucketServerPullRequestFormatter < PullRequestFormatter
def self.name
'bitbucket_server_pr'
end

def client_module
BitbucketServer
end
Expand All @@ -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)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/checkstyle_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module Pronto
module Formatter
class CheckstyleFormatter < Base
def self.name
'checkstyle'
end

def initialize
@output = ''
end
Expand Down Expand Up @@ -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)
8 changes: 4 additions & 4 deletions lib/pronto/formatter/formatter.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/pronto/formatter/github_combined_status_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/github_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class GithubFormatter < CommitFormatter
def self.name
'github'
end

def client_module
Github
end
Expand All @@ -16,4 +20,4 @@ def line_number(message, _)
end
end

Pronto::Formatter.register('github', Pronto::Formatter::GithubFormatter)
Pronto::Formatter.register(Pronto::Formatter::GithubFormatter)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/github_pull_request_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class GithubPullRequestFormatter < PullRequestFormatter
def self.name
'github_pr'
end

def client_module
Github
end
Expand All @@ -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)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/github_pull_request_review_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class GithubPullRequestReviewFormatter < PullRequestFormatter
def self.name
'github_pr_review'
end

def client_module
Github
end
Expand All @@ -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)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/github_status_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/gitlab_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class GitlabFormatter < CommitFormatter
def self.name
'gitlab'
end

def client_module
Gitlab
end
Expand All @@ -16,4 +20,4 @@ def line_number(message, _)
end
end

Pronto::Formatter.register('gitlab', Pronto::Formatter::GitlabFormatter)
Pronto::Formatter.register(Pronto::Formatter::GitlabFormatter)
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Pronto
module Formatter
class GitlabMergeRequestReviewFormatter < PullRequestFormatter
def self.name
'gitlab_mr'
end

def client_module
Gitlab
end
Expand Down Expand Up @@ -28,4 +32,4 @@ def line_number(message, _)
end
end

Pronto::Formatter.register('gitlab_mr', Pronto::Formatter::GitlabMergeRequestReviewFormatter)
Pronto::Formatter.register(Pronto::Formatter::GitlabMergeRequestReviewFormatter)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,4 +23,4 @@ def format(messages, _repo, _patches)
end
end

Pronto::Formatter.register('json', Pronto::Formatter::JsonFormatter)
Pronto::Formatter.register(Pronto::Formatter::JsonFormatter)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/null_formatter.rb
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 5 additions & 1 deletion lib/pronto/formatter/text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -14,4 +18,4 @@ def format(messages, _repo, _patches)
end
end

Pronto::Formatter.register('text', Pronto::Formatter::TextFormatter)
Pronto::Formatter.register(Pronto::Formatter::TextFormatter)
17 changes: 10 additions & 7 deletions spec/pronto/formatter/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eb433d9

Please sign in to comment.