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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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]
raise NoMethodError, 'Must be implemented in subclasses.'
end

def config
Expand Down
6 changes: 6 additions & 0 deletions 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 @@ -15,3 +19,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::BitbucketFormatter)
8 changes: 7 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 @@ -17,11 +21,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.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 @@ -15,3 +19,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::BitbucketServerPullRequestFormatter)
6 changes: 6 additions & 0 deletions 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 @@ -57,3 +61,5 @@ def to_checkstyle_severity(pronto_level)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::CheckstyleFormatter)
45 changes: 21 additions & 24 deletions lib/pronto/formatter/formatter.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 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,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
Expand All @@ -22,3 +26,5 @@ def create_status(client, sha, messages)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::GithubCombinedStatusFormatter)
6 changes: 6 additions & 0 deletions 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 @@ -15,3 +19,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::GithubFormatter)
6 changes: 6 additions & 0 deletions 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 @@ -16,3 +20,5 @@ def line_number(message, patches)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::GithubPullRequestFormatter)
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 @@ -22,3 +26,5 @@ def line_number(message, patches)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::GithubPullRequestReviewFormatter)
8 changes: 7 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,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
Expand All @@ -26,3 +30,5 @@ def create_status(client, sha, runner, messages)
end
end
end

Pronto::Formatter.register(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
6 changes: 6 additions & 0 deletions 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 @@ -15,3 +19,5 @@ def line_number(message, _)
end
end
end

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 All @@ -27,3 +31,5 @@ def line_number(message, _)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::GitlabMergeRequestReviewFormatter)
6 changes: 6 additions & 0 deletions 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 @@ -18,3 +22,5 @@ def format(messages, _repo, _patches)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::JsonFormatter)
6 changes: 6 additions & 0 deletions lib/pronto/formatter/null_formatter.rb
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions 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 @@ -13,3 +17,5 @@ def format(messages, _repo, _patches)
end
end
end

Pronto::Formatter.register(Pronto::Formatter::TextFormatter)
38 changes: 38 additions & 0 deletions spec/pronto/formatter/formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
Loading