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

Bump comment tag #174

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ custom_plugin_path: "/home/me/spork-plugins"
always_promote_remote: true
skip_berkshelf: false
role_match_file_name: true
bump_comment: true
json_options:
indent: " "
plugins:
Expand Down Expand Up @@ -84,6 +85,9 @@ plugins:
enabled: true
auto_push: true
branch: some_branch
bump_tag: false
bump_commit: false
bump_comment: false
irccat:
server: irccat.mydomain.com
port: 12345
Expand Down Expand Up @@ -134,6 +138,10 @@ The `always_promote_remote` directive allows you to tell spork promote to always
#### Skip Berkshelf
The `skip_berkshelf` directive is a temporary flag added in [#138](https://github.com/jonlives/knife-spork/issues/138) to allow Berkshelf functionality to be optionally bypassed until Berkshelf 3 support has been added to knife-spork per [#85](https://github.com/jonlives/knife-spork/issues/85). It simply removed the :Berkshelf constant from the namespace used by knife-spork.

#### Bump Comment
The 'bump_comment` directive tells spork to prompt the user for a comment reguarding the changes to this version of the cookbook. This comment will be appended to the CHANGELOG.md file along with the new version # and the user name. This can also be done with the "--bump_comment" on the command line.
NOTE: The bump_comment & bump_commit directives under the Git plug-in, allow spork to take this comment and use it as a git commit message.

#### JSON Options
The `json_options` directive allows you to tell spork to pass options to [pretty_generate](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-pretty_generate) to control the format of the resulting json

Expand Down Expand Up @@ -256,18 +264,29 @@ Everything looks good!

Spork Bump
----------
This function lets you easily version your cookbooks without having to manually edit the cookbook's `metadata.rb` file. You can either specify the version level you'd like to bump (`major`, `minor`, or `patch`), or you can manually specify a version number. This might be used if, for example, you want to jump several version numbers in one go and don't want to have to run knife bump once for each number. If no bump level is specified, a patch level bump will be performed.
This function lets you easily version your cookbooks without having to manually edit the cookbook's `metadata.rb` & `CHANGELOG.md` files. You can either specify the version level you'd like to bump (`major`, `minor`, or `patch`), or you can manually specify a version number. This might be used if, for example, you want to jump several version numbers in one go and don't want to have to run knife bump once for each number. If no bump level is specified, a patch level bump will be performed.

Spork Bump can also be configured promt the user for a comment reguarding thier change. This comment will be appended to the CHANGELOG.md file along with the new version, and the current username. This is done either by using the '--bump_comment' option on the command line or by setting the bump_comment: directive to true.
NOTE: The bump_comment & bump_commit directives under the Git plug-in, allow spork to take this comment and use it as a git commit message.

#### Usage
```bash
knife spork bump COOKBOOK [major | minor | patch | manual x.x.x]
knife spork bump COOKBOOK [major | minor | patch | manual x.x.x] [--bump_commment]
````

#### Example (No patch level specified - defaulting to patch)
```text
$ knife spork bump apache2
$ knife spork bump apache2 --bump_comment
Enter Change Log comment, then press Ctrl-D:
"Bug #111 fixed."
Successfully bumped apache2 to v2.0.4!
```
``` CHANGELOG.md will be appended with the following:

2.0.4
------
<user> - Bug #111 fixed.
```

#### Example (Bumping patch level)
```text
Expand Down
44 changes: 31 additions & 13 deletions lib/chef/knife/spork-bump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,32 @@ class SporkBump < Chef::Knife
:description => 'A colon-separated path to look for cookbooks in',
:proc => lambda { |o| o.split(':') }

option :berksfile,
:short => '-b',
:long => 'berksfile',
:description => 'Path to a Berksfile to operate off of',
:default => nil,
:proc => lambda { |o| o || File.join(Dir.pwd, ::Berkshelf::DEFAULT_FILENAME) }

option :skip_dependencies,
:short => '-s',
:long => '--skip-dependencies',
:description => 'Berksfile skips resolving source cookbook dependencies',
:default => true
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',
:long => 'berksfile',
:description => 'Path to a Berksfile to operate off of',
:default => File.join(Dir.pwd, ::Berkshelf::DEFAULT_FILENAME)

option :skip_dependencies,
:short => '-s',
:long => '--skip-dependencies',
:description => 'Berksfile skips resolving source cookbook dependencies',
:default => true
end

banner 'knife spork bump COOKBOOK [major|minor|patch|manual]'
banner 'knife spork bump COOKBOOK [major|minor|patch|manual] [--bump_comment]'

def run
self.class.send(:include, KnifeSpork::Runner)
self.config = Chef::Config.merge!(config)
config[:cookbook_path] ||= Chef::Config[:cookbook_path]
config[:bump_comment] ||= spork_config.bump_comment

if @name_args.empty?
show_usage
Expand Down Expand Up @@ -77,6 +84,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
33 changes: 32 additions & 1 deletion lib/knife-spork/plugins/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ 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")
metadata_file = "#{cookbook.root_dir}/metadata.rb"
new_version = File.read(metadata_file).split(/\n/).select{ |x| x =~ /(version\s+['"])[0-9\.]+(['"])/ }[0].split(/\s+/)[1].gsub"\'",""
top_level = `cd #{cookbook_path_for(cookbook)} && git rev-parse --show-toplevel 2>&1`.chomp
Dir.chdir(top_level)
if config.bump_tag
ui.info "Creating Git tag as #{cookbook.name}@#{new_version}"
git_tag("#{cookbook.name}@#{new_version}")
end
if config.bump_commit
ui.info "Creating Git commit"
if config[:bump_comment]
change_file = "#{cookbook.root_dir}/CHANGELOG.md"
last_comment_line = 0
File.foreach(change_file).with_index { |line, line_num|
last_comment_line = line_num if line == "---------\n"
}
file = File.open change_file
change_comment=[*file][last_comment_line + 1]
git_commit('.', "[Knife-Spork] #{change_comment}")
else
git_commit('.',"[Knife-Spork] Bumping #{cookbook.name} to #{new_version}")
end
end
end
end

Expand All @@ -50,12 +74,19 @@ def after_promote_local
else
"master"
end

git_commit(environment_path, "promote #{cookbooks.collect{ |c| "#{c.name}@#{c.version}" }.join(",")} to #{environments.join(",")}")
git_push(branch)
end
end

def bump_commit
config.bump_commit || false
end

def bump_tag
config.bump_tag || false
end

private
def git
safe_require 'git'
Expand Down
4 changes: 4 additions & 0 deletions lib/knife-spork/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,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
31 changes: 30 additions & 1 deletion plugins/Git.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ plugins:
remote: origin
branch: master
auto_push: true
bump_tag: false
bump_commit: false
bump_comment: false
```

**Note** Due to the nature of the git plugin, it's possible that you accept all the defaults. In that case, you should make your configuration like this:
Expand All @@ -50,4 +53,30 @@ The git branch to push/pull to/from.
An optional true / false parameter indicating whether or not changes should be automatically comitted and pushed to Git

- Type: `Boolean`
- Default: `false`
- Default: `false`

#### bump_tag
A git tag will be created after a cookbook bump in the format
<cookbook name>-<new version string>

- Type: `Bouleen`
- Default: `false`

#### bump_comment
This option is intended to take the comment entered when using the general :bump_comment option.
This option is meaningless without also enabling bump_commit.
The comment entered into the CHANGE_LOG.md will be used as the git commit message in the following format.
'[KnifeSpork] <user> - <comment>'

- Type: `Bouleen`
- Default: `false`

#### bump_commit
All changes will be commited locally after a cookbook bump.
If the bump_comment option is used (above) the commit meessage described there will be used. Otherwise,
the commit message will be:
'[KnifeSpork] Bumping <cookbook name> to <new version string>'

- Type: `Bouleen`
- Default: `false`