-
Notifications
You must be signed in to change notification settings - Fork 245
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
Add GitHub status formatter #144
Merged
mmozuras
merged 9 commits into
prontolabs:master
from
mknapik:feature/github_status_formatter
Apr 17, 2016
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
035f3c6
Add GithubStatusFormatter
mknapik f49f0ae
extract constant
mknapik 1ae5c0e
Extract StatusBuilder from GithubStatusFormatter
mknapik e610bf1
Simplify level_mapping
mknapik ede95f2
Remove `opts` from GithubStatusFormatter initializer
mknapik a00c2be
Rename `github_pr_status` to `github_status`
mknapik 0e58c66
Add info about GithubStatusFormatter to CHANGELOG and README
mknapik 97175be
README: combine multiple formatters
mknapik e6e10fe
Use underscore context names instead of class names
mknapik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require_relative 'github_status_formatter/status_builder' | ||
|
||
module Pronto | ||
module Formatter | ||
class GithubStatusFormatter | ||
def format(messages, repo, _) | ||
client = Github.new(repo) | ||
head = repo.head_commit_sha | ||
|
||
messages_by_runner = messages.uniq.group_by(&:runner) | ||
|
||
Runner.runners.each do |runner| | ||
create_status(client, head, runner, messages_by_runner[runner] || []) | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_status(client, sha, runner, messages) | ||
builder = StatusBuilder.new(runner, messages) | ||
status = Github::Status.new(sha, builder.state, builder.context, builder.description) | ||
|
||
client.create_commit_status(status) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Pronto | ||
module Formatter | ||
class GithubStatusFormatter | ||
class Inflector | ||
def self.underscore(camel_cased_word) | ||
return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/ | ||
word = camel_cased_word.to_s.gsub(/::/, '/') | ||
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') | ||
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') | ||
word.tr!('-', '_') | ||
word.downcase! | ||
word | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Pronto | ||
module Formatter | ||
class GithubStatusFormatter | ||
class Sentence | ||
def initialize(words) | ||
@words = words | ||
end | ||
|
||
def to_s | ||
case words.size | ||
when 0 | ||
'' | ||
when 1 | ||
words[0].to_s.dup | ||
when 2 | ||
"#{words[0]}#{WORD_CONNECTORS[:two_words_connector]}#{words[1]}" | ||
else | ||
to_oxford_comma_sentence | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why have you chosen oxford comma? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I shamelessly ripped off and simplified the implementation from ActiveSupport. |
||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :words | ||
|
||
WORD_CONNECTORS = { | ||
words_connector: ', ', | ||
two_words_connector: ' and ', | ||
last_word_connector: ', and ' | ||
}.freeze | ||
|
||
private_constant :WORD_CONNECTORS | ||
|
||
def to_oxford_comma_sentence | ||
"#{words[0...-1].join(WORD_CONNECTORS[:words_connector])}"\ | ||
"#{WORD_CONNECTORS[:last_word_connector]}"\ | ||
"#{words[-1]}" | ||
end | ||
end | ||
end | ||
end | ||
end |
76 changes: 76 additions & 0 deletions
76
lib/pronto/formatter/github_status_formatter/status_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require_relative 'sentence' | ||
require_relative 'inflector' | ||
|
||
module Pronto | ||
module Formatter | ||
class GithubStatusFormatter | ||
class StatusBuilder | ||
def initialize(runner, messages) | ||
@runner = runner | ||
@messages = messages | ||
end | ||
|
||
def description | ||
desc = map_description | ||
desc.empty? ? NO_ISSUES_DESCRIPTION : "Found #{desc}." | ||
end | ||
|
||
def state | ||
failure? ? :failure : :success | ||
end | ||
|
||
def context | ||
Inflector.underscore(@runner.name) | ||
end | ||
|
||
private | ||
|
||
def failure? | ||
@messages.any? { |message| failure_message?(message) } | ||
end | ||
|
||
def failure_message?(message) | ||
message_state(message) == :failure | ||
end | ||
|
||
def message_state(message) | ||
DEFAULT_LEVEL_TO_STATE_MAPPING[message.level] | ||
end | ||
|
||
def map_description | ||
words = count_issue_types.map do |issue_type, issue_count| | ||
pluralize(issue_count, issue_type) | ||
end | ||
|
||
Sentence.new(words).to_s | ||
end | ||
|
||
def count_issue_types | ||
counts = @messages.each_with_object(Hash.new(0)) do |message, r| | ||
r[message.level] += 1 | ||
end | ||
order_by_severity(counts) | ||
end | ||
|
||
def order_by_severity(counts) | ||
Hash[counts.sort_by { |k, _v| Pronto::Message::LEVELS.index(k) }] | ||
end | ||
|
||
def pluralize(count, word) | ||
"#{count} #{word}#{count > 1 ? 's' : ''}" | ||
end | ||
|
||
DEFAULT_LEVEL_TO_STATE_MAPPING = { | ||
info: :success, | ||
warning: :failure, | ||
error: :failure, | ||
fatal: :failure | ||
}.freeze | ||
|
||
NO_ISSUES_DESCRIPTION = 'Coast is clear!'.freeze | ||
|
||
private_constant :DEFAULT_LEVEL_TO_STATE_MAPPING, :NO_ISSUES_DESCRIPTION | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
spec/pronto/formatter/github_status_formatter/inflector_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Pronto | ||
module Formatter | ||
RSpec.describe GithubStatusFormatter::Inflector do | ||
describe '#underscore' do | ||
subject { described_class.underscore(class_name) } | ||
|
||
context 'when class is just one word' do | ||
let(:class_name) { 'Pronto::Runner' } | ||
|
||
it { should == 'pronto/runner' } | ||
end | ||
|
||
context 'when class contains camel case' do | ||
let(:class_name) { 'Pronto::FakeRunner' } | ||
|
||
it { should == 'pronto/fake_runner' } | ||
end | ||
|
||
context 'when class contains acronym' do | ||
let(:class_name) { 'Pronto::SUPERFakeRunner' } | ||
|
||
it { should == 'pronto/super_fake_runner' } | ||
end | ||
end | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
spec/pronto/formatter/github_status_formatter/sentence_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Pronto | ||
module Formatter | ||
RSpec.describe GithubStatusFormatter::Sentence do | ||
let(:sentence) { described_class.new(words) } | ||
|
||
describe '#to_s' do | ||
subject { sentence.to_s } | ||
|
||
context 'when no words' do | ||
let(:words) { [] } | ||
|
||
it 'returns empty string' do | ||
subject.should == '' | ||
end | ||
end | ||
|
||
context 'when 1 word' do | ||
let(:words) { %w(eeny) } | ||
|
||
it 'returns the word' do | ||
subject.should == 'eeny' | ||
end | ||
end | ||
|
||
context 'when 2 words' do | ||
let(:words) { %w(eeny meeny) } | ||
|
||
it 'uses and to join words' do | ||
subject.should == 'eeny and meeny' | ||
end | ||
end | ||
|
||
context 'when 3 words' do | ||
let(:words) { %w(eeny meeny miny moe) } | ||
|
||
it 'enumerates words using oxford comma' do | ||
subject.should == 'eeny, meeny, miny, and moe' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it's worth showing that multiple formatters can be used here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll add one more example.
Although I believe using multiple formatters is not documented anywhere else.