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

Rubocop plugin #120

Closed
wants to merge 22 commits into from
Closed
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
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ plugins:
port: 2003
eventinator:
url: http://eventinator.mydomain.com/events/oneshot
rubocop:
epic_fail: true
show_name: false
autocorrect: false
out_file: <file>
sev_level: <C|W|E>
lint: false
```

#### Default Environments
Expand Down Expand Up @@ -465,3 +472,51 @@ knife spork environment delete
knife spork environment edit
knife spork environment from file
```

Spork Rubocop
-------------
Automatically runs rubocop against your cookbooks on check and upload.
This is entirely based off of the Foodcritic plugin.


#### Usage
```ruby
knife spork check <cookbook>
knife spork upload <cookbook>
```

#### Example
``` ruby
chef_workstation01$ knife spork check chef-client
Checking versions for cookbook chef-client...

Local Version:
3.3.3

Remote Versions: (* indicates frozen)
3.3.3
3.2.0

ERROR: The version 3.3.3 exists on the server and is not frozen. Uploading will overwrite!
Running rubocop against [email protected]...
/home/chef-repo/cookbooks/chef-client
Inspecting 25 files
....CCCCC.CWCCCCCWCCCWCCC

Offenses:

chef-client/files/default/tests/minitest/config_test.rb:22:53: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
file(File.join(node['chef_client']['conf_dir'], "client.rb")).must_exist
^^^^^^^^^^^
chef-client/libraries/helpers.rb:35:72: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Chef::Log.debug("Node has Chef Server Recipe? #{node.recipe?("chef-server")}")
^^^^^^^^^^^^^
chef-client/libraries/helpers.rb:36:70: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Chef::Log.debug("Node has Chef Server Executable? #{system("which chef-server > /dev/null 2>&1")}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
chef-client/libraries/helpers.rb:37:74: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Chef::Log.debug("Node has Chef Server Ctl Executable? #{system("which chef-server-ctl > /dev/null 2>&1")}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

```
16 changes: 16 additions & 0 deletions lib/chef/knife/spork-bump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class SporkBump < Chef::Knife
:description => 'A colon-separated path to look for cookbooks in',
:proc => lambda { |o| o.split(':') }

option :bump_comment,
:long => '--bump_comment',
:description => 'Bump will prompt for a Change comment, which will be appended to CHANGELOG.md along with the new version # and username',
:default => nil

if defined?(::Berkshelf)
option :berksfile,
:short => '-b',
Expand Down Expand Up @@ -74,6 +79,17 @@ def bump
new_contents = File.read(metadata_file).gsub(/(version\s+['"])[0-9\.]+(['"])/, "\\1#{new_version}\\2")
File.open(metadata_file, 'w'){ |f| f.write(new_contents) }

if config[:bump_comment]
changelog_file = "#{@cookbook.root_dir}/CHANGELOG.md"
ui.info "Enter Change Log comment, then press Ctrl-D: "
change_comment = $stdin.read
File.open(changelog_file, 'a') { |cl|
cl.write("\n#{new_version}\n")
cl.write("---------\n")
cl.write("#{ENV['USER']} - #{change_comment}\n")
}
end

ui.info "Successfully bumped #{@cookbook.name} to v#{new_version}!"
end

Expand Down
9 changes: 8 additions & 1 deletion lib/chef/knife/spork-omni.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class SporkOmni < Chef::Knife
:description => 'Environment to promote the cookbook to',
:default => nil

option :omni_promote,
:long => '--promote',
:description => 'Omni will run promote, overrides config setting',
:default => nil

option :remote,
:long => '--remote',
:description => 'Make omni finish with promote --remote instead of a local promote',
Expand Down Expand Up @@ -103,7 +108,9 @@ def omni(cookbook)
ui.msg ""
upload(cookbook)
ui.msg ""
promote(cookbook)
if config[:omni_promote]
promote(cookbook)
end
end
end
end
1 change: 1 addition & 0 deletions lib/knife-spork/plugins/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def before_promote
def after_bump
cookbooks.each do |cookbook|
git_add(cookbook_path_for(cookbook),"metadata.rb")
git_add(cookbook_path_for(cookbook),"CHANGELOG.md")
end
end

Expand Down
52 changes: 52 additions & 0 deletions lib/knife-spork/plugins/rubocop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'knife-spork/plugins/plugin'

module KnifeSpork
module Plugins
class Rubocop < Plugin
name :rubocop
hooks :after_check, :before_upload

def perform
safe_require 'rubocop'
safe_require 'rubocop/cli'
safe_require 'rubocop/config_store'

if Gem::Specification.find_all_by_name("rubocop").empty?
ui.fatal "The knife-spork rubocop plugin requires rubocop."
exit 1
end

base_options = []
base_options = base_options.concat([ "-D" ]) if config.show_name # Lists the name of the offense along with the description
base_options = base_options.concat([ "--auto-correct" ]) if config.autocorrect
base_options = base_options.concat([ "--out", config.out_file ]) if config.out_file # Specify a file output rather than STDOUT for the specific errors
base_options = base_options.concat([ "--fail-level", config.sev_level ]) if config.sev_level # Specify a severity level for when rubocop should fail
base_options = base_options.concat([ "--lint"]) if config.lint # Only run lint checks

cookbooks.each do |cookbook|
ui.info "Running rubocop against #{cookbook.name}@#{cookbook.version}..."

cookbook_path = cookbook.root_dir

ui.info cookbook_path

options = [ cookbook_path ]

cli = ::Rubocop::CLI.new
result = cli.run(options)

unless result == 0
ui.error "Rubocop failed!"
exit(1) if config.epic_fail
else
ui.info "Passed!"
end
end
end

def epic_fail?
config.epic_fail.nil? ? 'true' : config.epic_fail
end
end
end
end
8 changes: 8 additions & 0 deletions lib/knife-spork/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def cookbook_path
[config[:cookbook_path] ||= ::Chef::Config.cookbook_path].flatten[0]
end

def omni_promote
spork_config[:omni_promote] || true
end

def environment_path
spork_config[:environment_path] || Chef::Config.environment_path.first || cookbook_path.gsub("/cookbooks","/environments")
end
Expand All @@ -146,6 +150,10 @@ def all_cookbooks
::Chef::CookbookLoader.new(::Chef::Config.cookbook_path)
end

def bump_comment
spork_config[:bump_comment] || false
end

def load_cookbook(name)
return name if name.is_a?(Chef::CookbookVersion)

Expand Down
66 changes: 66 additions & 0 deletions plugins/Rubocop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Rubocop
==========
Automatically runs rubocop against your cookbooks on check and upload.
This is entirely based off of the Foodcritic plugin.

Gem Requirements
----------------
This plugin requires the following gems:

```ruby
gem 'rubocop'
```

Hooks
-----
- `after_check`
- `before_upload`

Configuration
-------------
```yaml
plugins:
rubocop:
epic_fail: true
show_name: false
autocorrect: false
out_file: <file>
sev_level: <C|W|E>
lint: false
```

#### epic_fail:
If set to true, `epic_fail` will prevent you from uploading a cookbook or further checks from running until rubocop passes.
- Type: `Boolean`
- Default: `true`

#### The following options are passing command line options to rubocop. See rubocop --help for more details
#### show_name:
- Type: `Boolean`
- Default: `false`
- Rubocop command line equivilant: "-D"
Shows the name of the offending rule as well as the decription and line reference.

#### autocorrect:
- Type: `Boolean`
- Default: `false`
- Rubocop command line equivilant: "--auto-correct"
Automatically correct some offenses.

#### out_file:
- Type: `String` - file name
- Default: nil
- Rubocop command line equivilant: "--out <file>"
Redirects the rubocop output to a file instead of STDOUT.

#### sev_level:
- Type: `String`
- Default: nil
- Rubocop command line equivilant: "--fail-level [C|W|E]"
Set the severity level at which Rubocop will fail (see rubocop --help for more).

#### lint:
- Type: `Boolean`
- Default: `false`
- Rubocop command line equivilant: "--lint"
Only run linting rules.