Skip to content

Commit

Permalink
Merge pull request #204 from aergonaut/configurable-messages
Browse files Browse the repository at this point in the history
Adds configurable option to set message format
  • Loading branch information
mmozuras authored Mar 5, 2017
2 parents 7168f73 + e54f1d7 commit 14f857e
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 46 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,43 @@ via environment variables. Their names will be the upcased path to the property.
For example: `PRONTO_GITHUB_SLUG` or `PRONTO_GITLAB_API_PRIVATE_TOKEN`. Environment variables
will always take precedence over values in configuration file.

### Message format

Pronto allows you to configure the format of the messages that are produced. You
can set a default format that will be used by all formatters, or you can
configure a separate format per formatter, if you are using several.

To change the default format:

```yaml
format: "%{runner} %{level} %{msg}"
```

To add the title of the Runner to the GitHub Pull Request formatter only:

```yaml
github_pr:
format: "%{runner} - %{msg}"
```

The available values to be interpolated into the message are:

| Key | Description |
|--------------|----------------|
| `path` | File path. |
| `line` | Line number. |
| `level` | Message level. |
| `msg` | Message. |
| `commit_sha` | SHA. |
| `runner` | Runner name. |

The following values are available only to the text formatter:

| Key | Description |
|------------------|--------------------------|
| `color_level` | Colorized message level. |
| `color_location` | Colorized location. |

## Runners

Pronto can run various tools and libraries, as long as there's a runner for it.
Expand Down
1 change: 1 addition & 0 deletions lib/pronto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require 'pronto/bitbucket'

require 'pronto/formatter/colorizable'
require 'pronto/formatter/base'
require 'pronto/formatter/text_formatter'
require 'pronto/formatter/json_formatter'
require 'pronto/formatter/git_formatter'
Expand Down
9 changes: 9 additions & 0 deletions lib/pronto/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def max_warnings
ENV['PRONTO_MAX_WARNINGS'] || @config_hash['max_warnings']
end

def message_format(formatter)
formatter_config = @config_hash[formatter]
if formatter_config && formatter_config.key?('format')
formatter_config['format']
else
ENV["PRONTO_FORMAT"] || @config_hash['format']
end
end

def logger
@logger ||= begin
verbose = ENV['PRONTO_VERBOSE'] || @config_hash['verbose']
Expand Down
8 changes: 7 additions & 1 deletion lib/pronto/config_file.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Pronto
class ConfigFile
DEFAULT_MESSAGE_FORMAT = "%{msg}".freeze

EMPTY = {
'all' => {
'exclude' => [],
Expand All @@ -22,10 +24,14 @@ class ConfigFile
'password' => nil,
'web_endpoint' => 'https://bitbucket.org/'
},
'text' => {
'format' => "%{color_location} %{color_level}: %{msg}"
},
'runners' => [],
'formatters' => [],
'max_warnings' => nil,
'verbose' => false
'verbose' => false,
'format' => DEFAULT_MESSAGE_FORMAT
}.freeze

def initialize(path = '.pronto.yml')
Expand Down
13 changes: 13 additions & 0 deletions lib/pronto/formatter/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Pronto
module Formatter
class Base
def self.name
Formatter::FORMATTERS.invert[self]
end

def config
@config ||= Config.new
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pronto/formatter/checkstyle_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pronto
module Formatter
class CheckstyleFormatter
class CheckstyleFormatter < Base
def initialize
@output = ''
end
Expand Down
10 changes: 4 additions & 6 deletions lib/pronto/formatter/git_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Pronto
module Formatter
class GitFormatter
class GitFormatter < Base
def format(messages, repo, patches)
client = client_module.new(repo)
existing = existing_comments(messages, client, repo)
Expand Down Expand Up @@ -39,10 +39,6 @@ def grouped_comments(comments)
comments.group_by { |comment| [comment.path, comment.position] }
end

def config
@config ||= Config.new
end

def consolidate_comments(comments)
comment = comments.first
if comments.length > 1
Expand All @@ -65,7 +61,9 @@ def join_comments(comments)
def new_comment(message, patches)
config.logger.log("Creating a comment from message: #{message.inspect}")
sha = message.commit_sha
body = message.msg

body = config.message_format(self.class.name) % message.to_h

path = message.path
lineno = line_number(message, patches) if message.line
Comment.new(sha, body, path, lineno)
Expand Down
2 changes: 1 addition & 1 deletion lib/pronto/formatter/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pronto
module Formatter
class JsonFormatter
class JsonFormatter < Base
def format(messages, _, _)
messages.map do |message|
lineno = message.line.new_lineno if message.line
Expand Down
2 changes: 1 addition & 1 deletion lib/pronto/formatter/null_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Pronto
module Formatter
class NullFormatter
class NullFormatter < Base
def format(_, _, _)
end
end
Expand Down
42 changes: 6 additions & 36 deletions lib/pronto/formatter/text_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,15 @@
require 'pronto/formatter/text_message_decorator'

module Pronto
module Formatter
class TextFormatter
include Colorizable

LOCATION_COLOR = :cyan

LEVEL_COLORS = {
info: :yellow,
warning: :magenta,
error: :red,
fatal: :red
}.freeze

class TextFormatter < Base
def format(messages, _, _)
messages.map do |message|
"#{format_location(message)} #{format_level(message)}: #{message.msg}".strip
end
end

private

def format_location(message)
line = message.line
lineno = line.new_lineno if line
path = message.path
commit_sha = message.commit_sha

if path || lineno
path = colorize(path, LOCATION_COLOR) if path
"#{path}:#{lineno}"
elsif commit_sha
colorize(commit_sha[0..6], LOCATION_COLOR)
message_format = config.message_format(self.class.name)
message_data = TextMessageDecorator.new(message).to_h
(message_format % message_data).strip
end
end

def format_level(message)
level = message.level
color = LEVEL_COLORS.fetch(level)

colorize(level[0].upcase, color)
end
end
end
end
46 changes: 46 additions & 0 deletions lib/pronto/formatter/text_message_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Pronto
module Formatter
class TextMessageDecorator < SimpleDelegator
include Colorizable

LOCATION_COLOR = :cyan

LEVEL_COLORS = {
info: :yellow,
warning: :magenta,
error: :red,
fatal: :red
}.freeze

def to_h
original = __getobj__.to_h
original[:color_level] = format_level(__getobj__)
original[:color_location] = format_location(__getobj__)
original
end

private

def format_location(message)
line = message.line
lineno = line.new_lineno if line
path = message.path
commit_sha = message.commit_sha

if path || lineno
path = colorize(path, LOCATION_COLOR) if path
"#{path}:#{lineno}"
elsif commit_sha
colorize(commit_sha[0..6], LOCATION_COLOR)
end
end

def format_level(message)
level = message.level
color = LEVEL_COLORS.fetch(level)

colorize(level[0].upcase, color)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/pronto/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def hash
end
end

def to_h
{
path: path,
line: line,
level: level,
msg: msg,
commit_sha: commit_sha,
runner: @runner && @runner.title
}
end

private

def comparison_attributes
Expand Down
1 change: 1 addition & 0 deletions lib/pronto/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Runner
def initialize(patches, commit = nil)
@patches = patches
@commit = commit
@config = Config.new
end

def self.runners
Expand Down
17 changes: 17 additions & 0 deletions spec/pronto/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,22 @@ module Pronto
it { should == 'ruby/ruby' }
end
end

describe '#message_format' do
subject { config.message_format('whatever') }

context 'when there is an entry in the config file' do
let(:config_hash) { { 'whatever' => { 'format' => whatever_format } } }
let(:whatever_format) { "that's just like your opinion man" }

it { should == whatever_format }
end

context 'when there is no entry in the config file' do
let(:config_hash) { ConfigFile::EMPTY }

it { should == ConfigFile::DEFAULT_MESSAGE_FORMAT }
end
end
end
end

0 comments on commit 14f857e

Please sign in to comment.