cookbook-release 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1fcf382dc644cd29130d51f7592cf01700e1cb84
4
+ data.tar.gz: 422d1b34f9a96ad642b2b101c8188697ffc78a73
5
+ SHA512:
6
+ metadata.gz: 8a8e6d46bb049d01efb805518e63289dbd872b4b1f4ffca52f4e5f4114c3ee0ccb94772a24183006ed99ba5165004f6eefade13612cb1e3c7d7e016dd2b2c37e
7
+ data.tar.gz: f36559e550cc0e9a5c04dceaf8068bd8b1edeca14a3a152e813cda2d2d1d0ed8d7f0e526d4b9de6cbd2ac1c34467ec960390b02c8b091f6e33dd2dfda4a8446b
@@ -0,0 +1,50 @@
1
+ Gemfile.lock
2
+ # rcov generated
3
+ coverage
4
+ coverage.data
5
+
6
+ # rdoc generated
7
+ rdoc
8
+
9
+ # yard generated
10
+ doc
11
+ .yardoc
12
+
13
+ # bundler
14
+ .bundle
15
+
16
+ # jeweler generated
17
+ pkg
18
+
19
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
20
+ #
21
+ # * Create a file at ~/.gitignore
22
+ # * Include files you want ignored
23
+ # * Run: git config --global core.excludesfile ~/.gitignore
24
+ #
25
+ # After doing this, these files will be ignored in all your git projects,
26
+ # saving you from having to 'pollute' every project you touch with them
27
+ #
28
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
29
+ #
30
+ # For MacOS:
31
+ #
32
+ #.DS_Store
33
+
34
+ # For TextMate
35
+ #*.tmproj
36
+ #tmtags
37
+
38
+ # For emacs:
39
+ #*~
40
+ #\#*
41
+ #.\#*
42
+
43
+ # For vim:
44
+ #*.swp
45
+
46
+ # For redcar:
47
+ #.redcar
48
+
49
+ # For rubinius:
50
+ #*.rbc
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,36 @@
1
+ CookbookRelease
2
+ ===============
3
+
4
+ Helper to release cookbooks. This motivation is to publish new version at each commit on master branch from the CI.
5
+
6
+ This helper will create tags, push them and publish to supermarket.
7
+
8
+ Usage
9
+ -----
10
+
11
+ Include cookbook-release into your `Gemfile`.
12
+
13
+ Require cookbook-release into the `metadata.rb` file and replace the version by the helper:
14
+
15
+ ```
16
+ version Release.current_version
17
+ ```
18
+
19
+ Include the rake tasks in your Rakefile:
20
+
21
+ ```
22
+ require 'cookbook-release'
23
+ CookbookRelease::Rake::CookbookTask.new
24
+ ```
25
+
26
+ Then you can publish on your supermarket using:
27
+
28
+ ```
29
+ export SUPERMARKET_CLIENTKEYFILE=/tmp/my_key.pem
30
+ export SUPERMARKET_USERID=my_username
31
+ export SUPERMARKET_URL="http://supermarket.chef.io/api/v1/cookbooks"
32
+ export NO_PROMPT=""
33
+ rake release!
34
+ ```
35
+
36
+ Note: this setup is intended to be used in a CI system such as jenkins or travis.
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ rescue LoadError
5
+ end
6
+
7
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'English'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'cookbook-release'
9
+ spec.version = '0.1'
10
+ spec.authors = ['Grégoire Seux']
11
+ spec.email = 'g.seux@criteo.com'
12
+ spec.summary = 'Provide primitives (and rake tasks) to release a cookbook'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/criteo/cookbook-release.git'
15
+ spec.license = 'Apache License v2'
16
+
17
+ spec.files = `git ls-files`.split($RS)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'semantic'
23
+ spec.add_dependency 'highline'
24
+ spec.add_dependency 'mixlib-shellout'
25
+ spec.add_dependency 'chef'
26
+
27
+
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'rspec_junit_formatter'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'webmock'
32
+ end
@@ -0,0 +1,51 @@
1
+ require_relative 'cookbook-release/commit'
2
+ require_relative 'cookbook-release/git-utilities'
3
+ require_relative 'cookbook-release/supermarket'
4
+ require_relative 'cookbook-release/release'
5
+
6
+ require 'rake'
7
+ require 'rake/tasklib'
8
+
9
+ module CookbookRelease
10
+ module Rake
11
+ class CookbookTask < ::Rake::TaskLib
12
+
13
+ def initialize(namespaced=false)
14
+ define_tasks(namespaced)
15
+ end
16
+
17
+ def define_tasks(namespaced)
18
+
19
+ desc "Prepare cookbook release and push tag to git"
20
+ task "release!" do
21
+ opts = {
22
+ no_prompt: ENV['NO_PROMPT']
23
+ }
24
+ git = GitUtilities.new
25
+ Release.new(git, opts).release!
26
+ end
27
+
28
+ desc "Suggest new release version"
29
+ task "release:suggest_version" do
30
+ git = GitUtilities.new
31
+ release = Release.new(git)
32
+ release.display_suggested_version(*release.new_version)
33
+ end
34
+
35
+ desc "Display last released version"
36
+ task "release:version" do
37
+ git = GitUtilities.new
38
+ release = Release.new(git)
39
+ puts release.last_release
40
+ end
41
+
42
+ desc "Display changelog since last release"
43
+ task "release:changelog" do
44
+ git = GitUtilities.new
45
+ release = Release.new(git)
46
+ release.display_changelog(release.new_version.first)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ class Commit
2
+ extend Forwardable
3
+ def_delegators :@hash, :[]
4
+
5
+ def initialize(hash)
6
+ @hash = hash
7
+ end
8
+
9
+ def major?
10
+ !!(self[:subject] =~ /breaking/i)
11
+ end
12
+
13
+ def patch?
14
+ !!(self[:subject] =~ /\bfix\b/i) || !!(self[:subject] =~ /\bbugfix\b/i)
15
+ end
16
+
17
+ def minor?
18
+ !(major? || patch?)
19
+ end
20
+
21
+ def color
22
+ case true
23
+ when major?
24
+ :red
25
+ else
26
+ :grey
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,78 @@
1
+ require 'semantic'
2
+ require 'semantic/core_ext'
3
+ require 'mixlib/shellout'
4
+ require 'highline/import'
5
+
6
+ class GitUtilities
7
+
8
+ def initialize(options={})
9
+ @tag_prefix = options['tag_prefix'] || ''
10
+ end
11
+
12
+ def reset_command(new_version)
13
+ "git reset --hard HEAD^ && git tag -d #{new_version}"
14
+ end
15
+
16
+ def clean_index?
17
+ clean_index = Mixlib::ShellOut.new("git diff --exit-code")
18
+ clean_index.run_command
19
+ clean_staged = Mixlib::ShellOut.new("git diff --exit-code --cached")
20
+ clean_staged.run_command
21
+ !clean_index.error? && !clean_staged.error?
22
+ end
23
+
24
+ def clean_index!
25
+ raise "All changes must be committed!" unless clean_index?
26
+ end
27
+
28
+ def compute_last_release
29
+
30
+ tag = Mixlib::ShellOut.new([
31
+ 'git describe',
32
+ "--tags",
33
+ "--match \"#{@tag_prefix}[0-9]\.[0-9]*\.[0-9]*\""
34
+ ].join " ")
35
+ tag.run_command
36
+ tag.stdout.split('-').first.to_version
37
+ end
38
+
39
+ def compute_changelog(since)
40
+ # TODO use whole commit message instead of title only
41
+ log_cmd = Mixlib::ShellOut.new("git log --pretty='format:%an <%ae>::%s::%h' #{since}..HEAD")
42
+ log_cmd.run_command
43
+ log = log_cmd.stdout
44
+ log.split("\n").map do |entry|
45
+ author, subject, hash = entry.chomp.split("::")
46
+ Commit.new({
47
+ author: author,
48
+ subject: subject,
49
+ hash: hash
50
+ })
51
+ end.reject { |commit| commit[:subject] =~ /^Merge branch (.*) into/i }
52
+ end
53
+
54
+ def tag(version)
55
+ cmd = Mixlib::ShellOut.new("git tag #{@tag_prefix}#{version}")
56
+ cmd.run_command
57
+ cmd.error!
58
+ end
59
+
60
+ def choose_remote
61
+ cmd = Mixlib::ShellOut.new("git remote")
62
+ cmd.run_command
63
+ cmd.error!
64
+ remotes = cmd.stdout.split("\n")
65
+ if remotes.size == 1
66
+ remotes.first
67
+ else
68
+ choose(*remotes)
69
+ end
70
+ end
71
+
72
+ def push_tag(version)
73
+ remote = choose_remote
74
+ cmd = Mixlib::ShellOut.new("git push #{remote} #{@tag_prefix}#{version}")
75
+ cmd.run_command
76
+ cmd.error!
77
+ end
78
+ end
@@ -0,0 +1,91 @@
1
+ class Release
2
+
3
+ def self.current_version
4
+ r = Release.new(GitUtilities.new)
5
+ begin
6
+ r.new_version.first
7
+ rescue ExistingRelease
8
+ r.last_release
9
+ end
10
+ end
11
+
12
+ class ExistingRelease < StandardError
13
+ end
14
+
15
+ attr_reader :git
16
+
17
+ def initialize(git, opts={})
18
+ @git = git
19
+ @no_prompt = opts[:no_prompt]
20
+ end
21
+
22
+ def last_release
23
+ @last_release ||= git.compute_last_release
24
+ end
25
+
26
+ def git_changelog
27
+ @git_changelog ||= git.compute_changelog(last_release)
28
+ end
29
+
30
+ # return the new version and the reasons
31
+ def new_version
32
+ %w(major minor patch).each do |level|
33
+ changes = git_changelog.select(&"#{level}?".to_sym)
34
+ return [ last_release.send("#{level}!"), changes ] if changes.size > 0
35
+ end
36
+ raise ExistingRelease, "No commit since last release (#{last_release})"
37
+ end
38
+
39
+ def user_defined_version
40
+ puts "Which kind of upgrade ?"
41
+ new_release_level = choose(*%w(major minor patch))
42
+ last_release.send("#{new_release_level}!")
43
+ end
44
+
45
+ def display_suggested_version(new_version, reasons)
46
+ puts "Suggested version: " + new_version.to_s
47
+ puts "Commits that suggest this change:"
48
+ reasons.each do |commit|
49
+ puts "* #{commit[:hash]} #{commit[:subject]} (#{commit[:author]})"
50
+ end
51
+ end
52
+
53
+ def display_changelog(new_version)
54
+ puts "Changelog for #{new_version}:"
55
+ git_changelog.each do |commit|
56
+ puts "* #{commit[:hash]} #{HighLine.color(commit[:subject], commit.color)} (#{commit[:author]})"
57
+ end
58
+ end
59
+
60
+ def prepare_release
61
+ git.clean_index!
62
+ new_version , reasons = self.new_version
63
+ puts "Last release was: " + last_release.to_s
64
+ display_suggested_version(new_version, reasons)
65
+ puts ""
66
+
67
+ agreed = @no_prompt || agree("Do you agree with that version?") { |q| q.default = "yes" }
68
+ new_version = user_defined_version unless agreed
69
+ puts "New release will be #{new_version}"
70
+ puts ""
71
+
72
+ new_version
73
+ end
74
+
75
+ def release!
76
+ new_version = prepare_release
77
+ begin
78
+ git.tag(new_version)
79
+ display_changelog(new_version)
80
+ puts ""
81
+ agreed = @no_prompt || agree("Do you agree with this changelog?") { |q| q.default = "yes" }
82
+ exit 1 unless agreed
83
+ git.push_tag(new_version)
84
+ supermarket = Supermarket.new
85
+ supermarket.publish_ck('Others')
86
+ ensure
87
+ puts HighLine.color("Release aborted, you have to reset to previous state manually", :red)
88
+ puts ":use with care: #{git.reset_command(new_version)}"
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,68 @@
1
+ require 'chef/cookbook_loader'
2
+ require 'chef/cookbook/cookbook_version_loader'
3
+ require 'chef/cookbook_uploader'
4
+ require 'chef/cookbook_site_streaming_uploader'
5
+ require 'chef/mixin/shell_out'
6
+ require 'json'
7
+
8
+ class Supermarket
9
+
10
+ # This code is adapted from "knife cookbook share" and travis dpl provider
11
+ # for supermarket.
12
+
13
+ def initialize(opts={})
14
+ @url = opts[:url] || ENV['SUPERMARKET_URL'] || (raise "Require a supermarket url")
15
+ @user_id = opts[:user_id] || ENV['SUPERMARKET_USERID'] || (raise "Require a user id")
16
+ @client_key = opts[:client_key_file] || ENV['SUPERMARKET_CLIENTKEYFILE'] || (raise "Require a client key file")
17
+ end
18
+
19
+ include ::Chef::Mixin::ShellOut
20
+
21
+ def publish_ck(category)
22
+ ck = ::Chef::Cookbook::CookbookVersionLoader.new('.')
23
+ ck.load!
24
+ cookbook = ck.cookbook_version
25
+ # we have to provide a rest option otherwise it will try to load a
26
+ # client.pem key
27
+ ::Chef::CookbookUploader.new(cookbook, rest: 'fake_rest').validate_cookbooks
28
+
29
+ tmp_cookbook_dir = Chef::CookbookSiteStreamingUploader.create_build_dir(cookbook)
30
+ begin
31
+ shell_out!("tar -czf #{cookbook.name}.tgz #{cookbook.name}", :cwd => tmp_cookbook_dir)
32
+ rescue StandardError => e
33
+ raise "Impossible to make a tarball out of the cookbook, #{e}"
34
+ end
35
+
36
+ begin
37
+ upload("#{tmp_cookbook_dir}/#{cookbook.name}.tgz", category)
38
+ puts "Uploaded to supermarket #{@url}"
39
+ FileUtils.rm_rf tmp_cookbook_dir
40
+ rescue StandardError => e
41
+ $stderr.puts "Impossible to upload the cookbook to supermarket: #{e}"
42
+ raise
43
+ end
44
+ end
45
+
46
+ def upload(filename, category)
47
+ http_resp = ::Chef::CookbookSiteStreamingUploader.post(
48
+ @url,
49
+ @user_id,
50
+ @client_key,
51
+ {
52
+ tarball: File.open(filename),
53
+ cookbook: { category: category }.to_json,
54
+ })
55
+ res = ::Chef::JSONCompat.from_json(http_resp.body)
56
+ if http_resp.code.to_i != 201
57
+ if res['error_messages']
58
+ if res['error_messages'][0] =~ /Version already exists/
59
+ raise "The same version of this cookbook already exists on the Opscode Cookbook Site."
60
+ else
61
+ raise "#{res['error_messages'][0]}"
62
+ end
63
+ else
64
+ raise "Unknown error while sharing cookbook\nServer response: #{http_resp.body}"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Commit do
4
+
5
+ let(:breaking_change) { Commit.new(subject: '[Breaking] Removed thing') }
6
+ let(:fix_change) { Commit.new(subject: 'This is a fix') }
7
+ let(:minor_change) { Commit.new(subject: 'This introduces a feature') }
8
+
9
+ describe '.(major|patch|minor)?' do
10
+ it 'detects major changes' do
11
+ expect(breaking_change).to be_major
12
+ expect(fix_change) .not_to be_major
13
+ expect(minor_change) .not_to be_major
14
+ end
15
+ it 'detects minor changes' do
16
+ expect(breaking_change).not_to be_minor
17
+ expect(fix_change) .not_to be_minor
18
+ expect(minor_change) .to be_minor
19
+ end
20
+ it 'detects patch changes' do
21
+ expect(breaking_change).not_to be_patch
22
+ expect(fix_change) .to be_patch
23
+ expect(minor_change) .not_to be_patch
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,110 @@
1
+ require_relative 'spec_helper'
2
+
3
+ require 'mixlib/shellout'
4
+ require 'fileutils'
5
+
6
+ describe GitUtilities do
7
+ before(:each) do
8
+ @tmp = Dir.mktmpdir('cookbook-release')
9
+ @old_dir = Dir.pwd
10
+ Dir.chdir(@tmp)
11
+ cmds = <<-EOH
12
+ git init .
13
+ git config user.email "you@example.com"
14
+ git config user.name Hello
15
+ EOH
16
+ cmds.split("\n").each do |cmd|
17
+ cmd = Mixlib::ShellOut.new(cmd)
18
+ cmd.run_command
19
+ cmd.error!
20
+ end
21
+ end
22
+
23
+ after(:each) do
24
+ Dir.chdir(@old_dir)
25
+ FileUtils.rm_rf(@tmp)
26
+ end
27
+
28
+ let(:git) { GitUtilities.new }
29
+
30
+ describe '.clean_index(?|!)' do
31
+ it 'detects clean index' do
32
+ expect(git.clean_index?).to be(true)
33
+ expect{git.clean_index!}.not_to raise_error
34
+ end
35
+ it 'detects dirty cached index' do
36
+ cmds = <<-EOH
37
+ touch toto
38
+ git add toto
39
+ EOH
40
+ cmds.split("\n").each do |cmd|
41
+ cmd = Mixlib::ShellOut.new(cmd)
42
+ cmd.run_command
43
+ cmd.error!
44
+ end
45
+ expect(git.clean_index?).to be(false)
46
+ expect{git.clean_index!}.to raise_error(RuntimeError)
47
+ end
48
+ it 'detects dirty cached index' do
49
+ cmds = <<-EOH
50
+ touch toto
51
+ git add toto
52
+ git commit -m'none'
53
+ echo titi > toto
54
+ EOH
55
+ cmds.split("\n").each do |cmd|
56
+ cmd = Mixlib::ShellOut.new(cmd)
57
+ cmd.run_command
58
+ cmd.error!
59
+ end
60
+ expect(git.clean_index?).to be(false)
61
+ expect{git.clean_index!}.to raise_error(RuntimeError)
62
+ end
63
+ end
64
+
65
+ describe '.compute_last_release' do
66
+ it 'finds the last release' do
67
+ cmds = <<-EOH
68
+ touch toto
69
+ git add toto
70
+ git commit -m'none'
71
+ git tag 1.0.0
72
+ git commit --allow-empty -m 'none'
73
+ git tag 1.2.43
74
+ git commit --allow-empty -m 'none'
75
+ git tag v1.2.3
76
+ git commit --allow-empty -m 'none'
77
+ git tag interesting_tag
78
+ EOH
79
+ cmds.split("\n").each do |cmd|
80
+ cmd = Mixlib::ShellOut.new(cmd)
81
+ cmd.run_command
82
+ cmd.error!
83
+ end
84
+ expect(git.compute_last_release).to eq("1.2.43")
85
+ end
86
+ end
87
+
88
+ describe '.compute_changelog' do
89
+ it 'find the proper changelog' do
90
+ cmds = <<-EOH
91
+ touch toto
92
+ git add toto
93
+ git commit -m'none'
94
+ git tag 1.0.0
95
+ git commit --allow-empty -m 'A commit'
96
+ git commit --allow-empty -m 'Another commit'
97
+ git commit --allow-empty -m 'A third one'
98
+ EOH
99
+ cmds.split("\n").each do |cmd|
100
+ cmd = Mixlib::ShellOut.new(cmd)
101
+ cmd.run_command
102
+ cmd.error!
103
+ end
104
+
105
+ changelog = git.compute_changelog('1.0.0')
106
+ expect(changelog.size).to be(3)
107
+ expect(changelog.map {|c| c[:subject]}).to contain_exactly('A commit', 'Another commit', 'A third one')
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,59 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Release do
4
+ let(:git) { double('git') }
5
+
6
+ levels = %w(major minor patch)
7
+ levels.each do |l|
8
+ send(:let, l.to_sym) do
9
+ m = double(l)
10
+ allow(m).to receive("#{l}?".to_sym).and_return true
11
+ (levels - [l]).each do |ll|
12
+ allow(m).to receive("#{ll}?".to_sym).and_return false
13
+ end
14
+ m
15
+ end
16
+ end
17
+
18
+ describe '.new_version' do
19
+
20
+ it 'raise when no commit has been made since last release' do
21
+ allow(git).to receive(:compute_last_release).and_return('1.0.1'.to_version)
22
+ expect(git).to receive(:compute_changelog).and_return([])
23
+
24
+ release = Release.new(git)
25
+ expect{release.new_version}.to raise_error(Release::ExistingRelease, /no commit since/i)
26
+ end
27
+
28
+ it 'suggests major release when one commit is major' do
29
+ allow(git).to receive(:compute_last_release).and_return('1.0.1'.to_version)
30
+ expect(git).to receive(:compute_changelog).and_return([minor, minor, major, patch])
31
+
32
+ release = Release.new(git)
33
+ new_version, reasons = release.new_version
34
+
35
+ expect(new_version).to eq('2.0.0'.to_version)
36
+ end
37
+
38
+ it 'suggests minor release when one commit is minor' do
39
+ allow(git).to receive(:compute_last_release).and_return('1.0.1'.to_version)
40
+ expect(git).to receive(:compute_changelog).and_return([minor, minor, patch, patch])
41
+
42
+ release = Release.new(git)
43
+ new_version, reasons = release.new_version
44
+
45
+ expect(new_version).to eq('1.1.0'.to_version)
46
+ end
47
+
48
+ it 'suggests patch release when all commits are patches' do
49
+ allow(git).to receive(:compute_last_release).and_return('1.0.1'.to_version)
50
+ expect(git).to receive(:compute_changelog).and_return([patch, patch, patch])
51
+
52
+ release = Release.new(git)
53
+ new_version, reasons = release.new_version
54
+
55
+ expect(new_version).to eq('1.0.2'.to_version)
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'rspec/mocks'
3
+ require_relative '../lib/cookbook-release'
4
+ require 'webmock/rspec'
5
+
6
+ WebMock.disable_net_connect!
@@ -0,0 +1,49 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Supermarket do
4
+ let (:opts) do
5
+ { user_id: 'a_name', client_key_file: 'a_file',
6
+ url: 'http://a_url' }
7
+ end
8
+
9
+ before(:each) do
10
+ @tmp = Dir.mktmpdir('cookbook-release')
11
+ @old_dir = Dir.pwd
12
+ Dir.chdir(@tmp)
13
+ end
14
+
15
+ after(:each) do
16
+ Dir.chdir(@old_dir)
17
+ FileUtils.rm_rf(@tmp)
18
+ end
19
+
20
+ def init_cookbook
21
+ ::File.open('metadata.rb', 'wb+') do |f|
22
+ f.write <<-EOH
23
+ name 'yum-criteo'
24
+ maintainer 'Criteo'
25
+ maintainer_email 'j.mauro@criteo.com'
26
+ license 'All rights reserved'
27
+ description "Update system to frozen"
28
+ long_description ""
29
+ version '2.0.0'
30
+ EOH
31
+ end
32
+ end
33
+
34
+ describe '.pusblish_ck' do
35
+ it 'publish to supermarket' do
36
+ init_cookbook
37
+ s = Supermarket.new(opts)
38
+ response = double('http response',
39
+ body: "{}",
40
+ code: "201"
41
+ )
42
+ expect(::Chef::CookbookSiteStreamingUploader).
43
+ to receive(:post).
44
+ with('http://a_url', 'a_name', 'a_file', anything()).
45
+ and_return(response)
46
+ expect { s.publish_ck('a_category') }.not_to raise_error
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cookbook-release
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Grégoire Seux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: semantic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mixlib-shellout
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: chef
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec_junit_formatter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Provide primitives (and rake tasks) to release a cookbook
126
+ email: g.seux@criteo.com
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - ".gitignore"
132
+ - ".travis.yml"
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile.rb
137
+ - cookbook-release.gemspec
138
+ - lib/cookbook-release.rb
139
+ - lib/cookbook-release/commit.rb
140
+ - lib/cookbook-release/git-utilities.rb
141
+ - lib/cookbook-release/release.rb
142
+ - lib/cookbook-release/supermarket.rb
143
+ - spec/commit_spec.rb
144
+ - spec/git_spec.rb
145
+ - spec/release_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/supermarket_spec.rb
148
+ homepage: https://github.com/criteo/cookbook-release.git
149
+ licenses:
150
+ - Apache License v2
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.5.1
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Provide primitives (and rake tasks) to release a cookbook
172
+ test_files:
173
+ - spec/commit_spec.rb
174
+ - spec/git_spec.rb
175
+ - spec/release_spec.rb
176
+ - spec/spec_helper.rb
177
+ - spec/supermarket_spec.rb
178
+ has_rdoc: