Skip to content

Commit

Permalink
Merge branch 'laziness' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
vassilevsky committed Aug 11, 2015
2 parents d8ffd84 + 0888d38 commit ea0ff59
Show file tree
Hide file tree
Showing 31 changed files with 869 additions and 172 deletions.
3 changes: 3 additions & 0 deletions .vexor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ rvm:
- 2.0
- 2.1
- 2.2

before_script:
- sudo npm install -g coffeelint
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Vexor](https://ci.vexor.io/projects/126da196-c8e6-46f0-8bc7-b5f8f4b49732/status.svg)](https://ci.vexor.io/ui/projects/126da196-c8e6-46f0-8bc7-b5f8f4b49732/builds)
[![Coveralls](https://img.shields.io/coveralls/vassilevsky/face_control.svg)](https://coveralls.io/github/vassilevsky/face_control)

# Face Control

Comment on added lines of pull requests in [Atlassian Stash][].
Expand All @@ -12,8 +15,6 @@ Inspired by [Hound][].

## Usage

rubocop -f json -o rubocop.json
coffeelint --reporter raw app/assets/javascripts > coffeelint_report.json
face-control <project> <repository> <pull_request_id>

It's natural to run this on a continuous integration server.
Expand Down Expand Up @@ -43,8 +44,6 @@ For example, here's a [Jenkins][] project setup:
gem install rubocop face_control
npm install -g coffeelint

rubocop -f json -o rubocop.json || true
coffeelint --reporter raw app/assets/javascripts > coffeelint_report.json || true
face-control <project> <repository> $PULL_REQUEST_ID

If you don't want to receive RuboCop comments with certain severity level,
Expand Down
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.test_files = FileList['test/**/*_test.rb']
t.warning = true
end

task :default => :test
16 changes: 0 additions & 16 deletions bin/bundler

This file was deleted.

14 changes: 0 additions & 14 deletions bin/console

This file was deleted.

16 changes: 0 additions & 16 deletions bin/face-control

This file was deleted.

16 changes: 0 additions & 16 deletions bin/httparty

This file was deleted.

16 changes: 0 additions & 16 deletions bin/rake

This file was deleted.

7 changes: 0 additions & 7 deletions bin/setup

This file was deleted.

34 changes: 1 addition & 33 deletions exe/face-control
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
#!/usr/bin/env ruby

require 'logger'
require 'docopt'
require 'face_control'
require 'stash'

begin
USAGE = "#{File.dirname(__FILE__)}/../USAGE"
args = Docopt.docopt(File.read(USAGE))

project = args['<project>']
repository = args['<repository>']
pull_request_id = args['<pull_request_id>']

ignored_severities = []
if args['--skip-severity']
ignored_severities = args['--skip-severity'].split(',')
puts "Skipping RuboCop offenses with severities: #{ignored_severities.join(', ')}."
end

config = Stash::Config.new
server = Stash::Server.new(config.host, config.user, config.password, Logger.new(STDOUT))
repository = server.repository(project, repository)
pull_request = repository.pull_request(pull_request_id)

[
FaceControl::Inputs::RubocopJson.new(ignored_severities),
FaceControl::Inputs::CoffeeLintRaw.new
].each do |input|
input.comments.each do |comment|
pull_request.add_comment(comment.file, comment.line, comment.text)
end
end
rescue Docopt::Exit => e
puts e.message
end
FaceControl::CLI.new.run
4 changes: 4 additions & 0 deletions face_control.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'bundler', '~> 1.8'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'minitest', '~> 5.8'
spec.add_development_dependency 'minitest-reporters', '~> 1.0'
spec.add_development_dependency 'webmock', '~> 1.21'
spec.add_development_dependency 'coveralls', '~> 0.8'
end
4 changes: 3 additions & 1 deletion lib/face_control.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'face_control/version'
require 'face_control/cli'
require 'face_control/comment'
require 'face_control/inputs'
require 'face_control/checker_runner'
require 'face_control/checkers'
28 changes: 28 additions & 0 deletions lib/face_control/checker_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module FaceControl
class CheckerRunner
def initialize(checker_class, filenames = [], options = {})
@checker = checker_class.new
if @checker.respond_to?(:options=)
@checker.options = options
end

@filenames = filenames
end

def comments
return [] if relevant_filenames.empty?

@checker.parse(`#{@checker.command(relevant_filenames.join(' '))}`)
end

private

def relevant_filenames
@relevant_filenames ||= @checker.relevant_globs.map do |glob|
@filenames.select do |filename|
File.fnmatch?(glob, filename)
end
end.flatten.uniq
end
end
end
2 changes: 2 additions & 0 deletions lib/face_control/checkers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'face_control/checkers/rubocop'
require 'face_control/checkers/coffeelint'
39 changes: 39 additions & 0 deletions lib/face_control/checkers/_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'face_control/comment'

module FaceControl
module Checkers
class Example
# @optional
# Define only if you use @options in the following methods
attr_writer :options

# @return [Array<String>] Shell globs to filter only files relevant to this checker
# out of all files with added lines in the pull request
def relevant_globs
%w(bin/*)
end

# @param filenames [String] Files with added lines in the pull request
# only relevant to this checker (filtered by globs above)
# @return [String] Command line to check the files
def command(filenames)
"ls -l #{filenames}"
end

# @param command_output [String] Stdout of the command above
# @return Array<FaceControl::Comment> Comments to post to the pull request
def parse(command_output)
command_output.split("\n").map do |line|
fields = line.split

mode = fields.first
file = fields.last

if mode != '-rwxr-xr-x'
Comment.new(file: file, line: 1, text: 'Invalid file mode')
end
end
end
end
end
end
28 changes: 28 additions & 0 deletions lib/face_control/checkers/coffeelint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'json'
require 'face_control/comment'

module FaceControl
module Checkers
class CoffeeLint
def relevant_globs
%w(*.coffee)
end

def command(filenames)
"coffeelint --reporter raw #{filenames}"
end

def parse(command_output)
JSON.parse(command_output).map do |file, problems|
problems.map do |problem|
Comment.new(
file: file,
line: problem['lineNumber'],
text: "(#{problem['level']}) #{problem['message']}"
)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
require 'face_control/comment'

module FaceControl
module Inputs
class RubocopJson
attr_accessor :ignored_severities, :filename
module Checkers
class RuboCop
attr_writer :options

def initialize(ignored_severities = [], filename = 'rubocop.json')
self.ignored_severities = ignored_severities
self.filename = filename
def relevant_globs
%w(
*.rb
*.rake
Capfile
Gemfile
Rakefile
Vagrantfile
)
end

fail "#{filename} does not exist" unless File.exist?(filename)
def command(filenames)
"rubocop --format json #{filenames}"
end

def comments
report['files'].map do |file|
def parse(command_output)
JSON.parse(command_output)['files'].map do |file|
file['offenses'].reject do |offense|
ignored_severities.include?(offense['severity'])
end.map do |offense|
Expand All @@ -27,13 +35,13 @@ def comments
text: text(offense, file)
)
end
end.flatten
end
end

private

def report
JSON.parse(File.read(filename))
def ignored_severities
@options.fetch(:ignored_severities, [])
end

def text(offense, file)
Expand All @@ -52,7 +60,7 @@ def text(offense, file)

def style_guide_url(offense)
cop_name = offense['cop_name']
cop_config = RuboCop::ConfigLoader.default_configuration[cop_name]
cop_config = ::RuboCop::ConfigLoader.default_configuration[cop_name]
cop_config['StyleGuide']
end

Expand Down
Loading

0 comments on commit ea0ff59

Please sign in to comment.