knife-changelog 0.1.1.pre.alpha.pre.23 → 0.2.0.pre.alpha.pre.25

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2RmNzcwYWViYWY1MWMwYjM4MzllNjdlYTU5ODhhZWI0ZThhNWJjZg==
4
+ ZmRkMTY4NGNiN2Y2NTE2YTUyN2RlZjg4MDBiMmUyYmQyNzZlYThjNg==
5
5
  data.tar.gz: !binary |-
6
- ZDgyMWM2N2RhNWZmZWFmYjlmZDliM2VhZjIyMzNlYmNmMDZjY2U0OA==
6
+ NmM0YWZlM2MwZWZhZTdiMWVlNWZiYmU1MTc0ZmM0YmUyNTE3M2JhMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTY4NzI4NmNhNjkzZjJiMjI5MmJhYjRjZDJmNjEwMTUxZGFlNzc0OTlhNTUw
10
- MzJiNjgwZjQxM2JhZTFhYzc0OTJiYmIxZGVhM2E5ZmUzZDQ1MWQzMGFmODJk
11
- NjczMTJlOGE2MDg0M2I3NjE2Y2RkMGM5OGZhZGQ5MzVkNDkzZjI=
9
+ ZWZhMDQ2MGMyNGVmOTQ0MjNkNzdjY2NiM2Y4MTA3NWRjOTlkNGY5OWM3NTZm
10
+ NGM0MzI1Mzg2NjhhYzMwZjgzYmIzYmJkODU5MTYxNDZiOWYxZjJlNjNmNThk
11
+ NmVmNmNmM2Q0NDU3ODZmNWM4ZjIwYzgwZjgxOTliMDY1Zjk3ZGU=
12
12
  data.tar.gz: !binary |-
13
- ZmFjOTI0NGIzZmEyMmY2NTNlNjYzYTYxZTQzMDI1MmIwMGYzNmZmMTM3YTc3
14
- YjAwOGRhNTBjYTYzZTkxNTI5MDhiNmZhOTRmMDhhNjNhOGUzZjYyMjBlNDA1
15
- ZDhkNWFhMTM1MGQ3ZTNmZGYxNjllNWQ5ZjRiNDJhNDJjYjczYjQ=
13
+ N2E0NWIzZWJmYzQ4NmE1NmQwOTAyMjBjZTdlMTg3YjllMTQ5MjI5ZGU1Y2Zm
14
+ MTU2NzEyOWZmZTBjNmMyMjBjYzNlZjBjMjIwMTM3NjM4NmM1MmUyZmE5N2I5
15
+ ZmI3ZjJkMmQ1OGQ2MGJlNWE5MDJhNjRmYjJjMGFlZGZiMzNhOGM=
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
 
26
26
 
27
27
  spec.add_dependency 'berkshelf'
28
+ spec.add_dependency 'rest-client'
28
29
  spec.add_dependency 'mixlib-shellout'
29
30
  spec.add_dependency 'chef', '~> 11.16'
30
31
  end
@@ -1,4 +1,5 @@
1
1
  require "knife/changelog/version"
2
+ require "knife/changelog/changelog"
2
3
  require "berkshelf"
3
4
  require 'chef/knife'
4
5
  require 'mixlib/shellout'
@@ -11,9 +12,8 @@ class Chef
11
12
 
12
13
  def initialize(options)
13
14
  super
14
- @tmp_prefix = 'knife-changelog'
15
- @berksfile = Berkshelf::Berksfile.from_options({})
16
- @tmp_dirs = []
15
+ berksfile = Berkshelf::Berksfile.from_options({})
16
+ @changelog = KnifeChangelog::Changelog.new(berksfile, config)
17
17
  end
18
18
 
19
19
  option :linkify,
@@ -39,185 +39,10 @@ class Chef
39
39
 
40
40
 
41
41
  def run
42
- begin
43
- Log.info config
44
- if @name_args.empty?
45
- cks = @berksfile.cookbooks.collect {|c| c.cookbook_name }
46
- else
47
- cks = @name_args
48
- end
49
- cks.each do |cookbook|
50
- Log.debug "Checking changelog for #{cookbook} (cookbook)"
51
- execute cookbook
52
- end
53
- (config[:submodules] || '').split(',').each do |submodule|
54
- Log.debug "Checking changelog for #{submodule} (submodule)"
55
- execute(submodule, true)
56
- end
57
- ensure
58
- clean
59
- end
42
+ Log.info config
43
+ @changelog.run(@name_args)
60
44
  end
61
-
62
- def clean
63
- @tmp_dirs.each do |dir|
64
- FileUtils.rm_r dir
65
- end
66
- end
67
-
68
- def ck_dep(name)
69
- @berksfile.lockfile.find(name)
70
- end
71
-
72
- def ck_location(name)
73
- ck_dep(name).location
74
- end
75
-
76
- def version_for(name)
77
- # FIXME uses public methods instead
78
- @berksfile.lockfile.graph.instance_variable_get(:@graph)[name].version
79
- end
80
-
81
- def execute(name, submodule=false)
82
- changelog = if submodule
83
- handle_submodule(name)
84
- else
85
- loc = ck_location(name)
86
- case loc
87
- when NilClass
88
- handle_source name, ck_dep(name)
89
- when Berkshelf::GitLocation
90
- handle_git loc
91
- when Berkshelf::PathLocation
92
- Log.debug "path location are always at the last version"
93
- ""
94
- else
95
- raise "Cannot handle #{loc.class} yet"
96
- end
97
- end
98
- print_changelog(name, changelog)
99
- end
100
-
101
- def print_changelog(name, changelog)
102
- unless changelog.empty?
103
- puts "### Changelog for #{name}"
104
- puts changelog
105
- puts "\n\n"
106
- end
107
- end
108
-
109
- def handle_source(name, dep)
110
- ck = noauth_rest.get_rest("https://supermarket.getchef.com/api/v1/cookbooks/#{name}")
111
- url = ck['source_url'] || ck ['external_url']
112
- Chef::Log.debug("Using #{url} as source url")
113
- case url.strip
114
- when nil,""
115
- Log.warn "No external url for #{name}, can't find any changelog source"
116
- ""
117
- when /github.com\/(.*)(.git)?/
118
- url = "https://github.com/#{$1.chomp('/')}.git"
119
- options = {
120
- :git => url,
121
- :revision => version_for(name),
122
- }
123
- location = Berkshelf::GitLocation.new dep, options
124
- handle_git(location)
125
- else
126
- fail "External url #{url} points to unusable location!"
127
- end
128
- end
129
-
130
- def revision_exists?(dir, revision)
131
- Log.debug "Testing existence of #{revision}"
132
- revlist = Mixlib::ShellOut.new("git rev-list --quiet #{revision}", :cwd => dir)
133
- revlist.run_command
134
- not revlist.error?
135
- end
136
-
137
- def detect_cur_revision(dir, rev)
138
- unless revision_exists?(dir, rev)
139
- prefixed_rev = 'v' + rev
140
- return prefixed_rev if revision_exists?(dir, prefixed_rev)
141
- fail "#{rev} is not a valid revision"
142
- end
143
- rev
144
- end
145
-
146
- def handle_submodule(name)
147
- subm_url = Mixlib::ShellOut.new("git config --list| grep ^submodule | grep ^submodule.#{name}.url")
148
- subm_url.run_command
149
- subm_url.error!
150
- url = subm_url.stdout.lines.first.split('=')[1].chomp
151
- subm_revision = Mixlib::ShellOut.new("git submodule status #{name}")
152
- subm_revision.run_command
153
- subm_revision.error!
154
- revision = subm_revision.stdout.strip.split(' ').first
155
- loc = Berkshelf::Location.init(nil, {git: url,revision: revision})
156
- handle_git(loc)
157
- end
158
-
159
- def handle_git(location)
160
- tmp_dir = shallow_clone(@tmp_prefix,location.uri)
161
-
162
- rev_parse = location.instance_variable_get(:@rev_parse)
163
- cur_rev = location.revision.rstrip
164
- cur_rev = detect_cur_revision(tmp_dir, cur_rev)
165
- ls_tree = Mixlib::ShellOut.new("git ls-tree -r #{rev_parse}", :cwd => tmp_dir)
166
- ls_tree.run_command
167
- changelog = ls_tree.stdout.lines.find { |line| line =~ /\s(changelog.*$)/i }
168
- if changelog and not config[:ignore_changelog_file]
169
- Log.info "Found changelog file : " + $1
170
- generate_from_changelog_file($1, cur_rev, rev_parse, tmp_dir)
171
- else
172
- generate_from_git_history(tmp_dir, location, cur_rev, rev_parse)
173
- end
174
- end
175
-
176
- def generate_from_changelog_file(filename, current_rev, rev_parse, tmp_dir)
177
- diff = Mixlib::ShellOut.new("git diff #{current_rev}..#{rev_parse} -- #{filename}", :cwd => tmp_dir)
178
- diff.run_command
179
- diff.stdout.lines.collect {|line| $1 if line =~ /^\+([^+].*)/}.compact
180
- end
181
-
182
- def generate_from_git_history(tmp_dir, location, current_rev, rev_parse)
183
- log = Mixlib::ShellOut.new("git log --no-merges --abbrev-commit --pretty=oneline #{current_rev}..#{rev_parse}", :cwd => tmp_dir)
184
- log.run_command
185
- c = log.stdout.lines
186
- n = https_url(location)
187
- c = linkify(n, c) if config[:linkify] and n
188
- c = c.map { |line| "* " + line } if config[:markdown]
189
- c
190
- end
191
-
192
- def linkify(url, changelog)
193
- changelog.map do |line|
194
- line.gsub(/^([a-f0-9]+) /, '[\1](%s/commit/\1) ' % [url.chomp('.git')])
195
- end
196
- end
197
-
198
- def https_url(location)
199
- if location.uri =~ /^\w+:\/\/(.*@)?(.*)(\.git?)/
200
- "https://%s" % [ $2 ]
201
- else
202
- fail "Cannot guess http url from git remote url"
203
- end
204
- end
205
-
206
- def short(location)
207
- if location.uri =~ /([\w-]+)\/([\w-]+)(\.git)?$/
208
- "%s/%s" % [$1,$2]
209
- end
210
- end
211
-
212
- def shallow_clone(tmp_prefix, uri)
213
- dir = Dir.mktmpdir(tmp_prefix)
214
- @tmp_dirs << dir
215
- clone = Mixlib::ShellOut.new("git clone --bare #{uri} bare-clone", :cwd => dir)
216
- clone.run_command
217
- clone.error!
218
- ::File.join(dir, 'bare-clone')
219
- end
220
-
221
45
  end
46
+
222
47
  end
223
48
  end
@@ -0,0 +1,195 @@
1
+ require 'chef/log'
2
+ require 'chef/knife'
3
+ require 'rest-client'
4
+ require 'json'
5
+
6
+ class KnifeChangelog
7
+ class Changelog
8
+ def initialize(berksfile, config = {})
9
+ @tmp_prefix = 'knife-changelog'
10
+ @berksfile = berksfile
11
+ @config = config
12
+ @tmp_dirs = []
13
+ end
14
+
15
+ def run(cookbooks)
16
+ begin
17
+ if cookbooks.empty?
18
+ cks = @berksfile.cookbooks.collect {|c| c.cookbook_name }
19
+ else
20
+ cks = cookbooks
21
+ end
22
+ cks.each do |cookbook|
23
+ Chef::Log.debug "Checking changelog for #{cookbook} (cookbook)"
24
+ execute cookbook
25
+ end
26
+ (@config[:submodules] || '').split(',').each do |submodule|
27
+ Chef::Log.debug "Checking changelog for #{submodule} (submodule)"
28
+ execute(submodule, true)
29
+ end
30
+ ensure
31
+ clean
32
+ end
33
+ end
34
+
35
+ def clean
36
+ @tmp_dirs.each do |dir|
37
+ FileUtils.rm_r dir
38
+ end
39
+ end
40
+
41
+ def ck_dep(name)
42
+ @berksfile.lockfile.locks[name]
43
+ end
44
+
45
+ def ck_location(name)
46
+ ck_dep(name).location
47
+ end
48
+
49
+ def version_for(name)
50
+ # FIXME uses public methods instead
51
+ @berksfile.lockfile.graph.instance_variable_get(:@graph)[name].version
52
+ end
53
+
54
+ def execute(name, submodule=false)
55
+ changelog = if submodule
56
+ handle_submodule(name)
57
+ else
58
+ loc = ck_location(name)
59
+ case loc
60
+ when NilClass
61
+ handle_source name, ck_dep(name)
62
+ when Berkshelf::GitLocation
63
+ handle_git loc
64
+ when Berkshelf::PathLocation
65
+ Chef::Log.debug "path location are always at the last version"
66
+ ""
67
+ else
68
+ raise "Cannot handle #{loc.class} yet"
69
+ end
70
+ end
71
+ print_changelog(name, changelog)
72
+ end
73
+
74
+ def print_changelog(name, changelog)
75
+ unless changelog.empty?
76
+ puts "### Changelog for #{name}"
77
+ puts changelog
78
+ puts "\n\n"
79
+ end
80
+ end
81
+
82
+ def handle_source(name, dep)
83
+ ck = JSON.parse(RestClient.get "https://supermarket.getchef.com/api/v1/cookbooks/#{name}")
84
+ url = ck['source_url'] || ck ['external_url']
85
+ Chef::Log.debug("Using #{url} as source url")
86
+ case url.strip
87
+ when nil,""
88
+ Chef::Log.warn "No external url for #{name}, can't find any changelog source"
89
+ ""
90
+ when /github.com\/(.*)(.git)?/
91
+ url = "https://github.com/#{$1.chomp('/')}.git"
92
+ options = {
93
+ :git => url,
94
+ :revision => version_for(name),
95
+ }
96
+ location = Berkshelf::GitLocation.new dep, options
97
+ handle_git(location)
98
+ else
99
+ fail "External url #{url} points to unusable location!"
100
+ end
101
+ end
102
+
103
+ def revision_exists?(dir, revision)
104
+ Chef::Log.debug "Testing existence of #{revision}"
105
+ revlist = Mixlib::ShellOut.new("git rev-list --quiet #{revision}", :cwd => dir)
106
+ revlist.run_command
107
+ not revlist.error?
108
+ end
109
+
110
+ def detect_cur_revision(dir, rev)
111
+ unless revision_exists?(dir, rev)
112
+ prefixed_rev = 'v' + rev
113
+ return prefixed_rev if revision_exists?(dir, prefixed_rev)
114
+ fail "#{rev} is not a valid revision"
115
+ end
116
+ rev
117
+ end
118
+
119
+ def handle_submodule(name)
120
+ subm_url = Mixlib::ShellOut.new("git config --list| grep ^submodule | grep ^submodule.#{name}.url")
121
+ subm_url.run_command
122
+ subm_url.error!
123
+ url = subm_url.stdout.lines.first.split('=')[1].chomp
124
+ subm_revision = Mixlib::ShellOut.new("git submodule status #{name}")
125
+ subm_revision.run_command
126
+ subm_revision.error!
127
+ revision = subm_revision.stdout.strip.split(' ').first
128
+ loc = Berkshelf::Location.init(nil, {git: url,revision: revision})
129
+ handle_git(loc)
130
+ end
131
+
132
+ def handle_git(location)
133
+ tmp_dir = shallow_clone(@tmp_prefix,location.uri)
134
+
135
+ rev_parse = location.instance_variable_get(:@rev_parse)
136
+ cur_rev = location.revision.rstrip
137
+ cur_rev = detect_cur_revision(tmp_dir, cur_rev)
138
+ ls_tree = Mixlib::ShellOut.new("git ls-tree -r #{rev_parse}", :cwd => tmp_dir)
139
+ ls_tree.run_command
140
+ changelog = ls_tree.stdout.lines.find { |line| line =~ /\s(changelog.*$)/i }
141
+ if changelog and not @config[:ignore_changelog_file]
142
+ Chef::Log.info "Found changelog file : " + $1
143
+ generate_from_changelog_file($1, cur_rev, rev_parse, tmp_dir)
144
+ else
145
+ generate_from_git_history(tmp_dir, location, cur_rev, rev_parse)
146
+ end
147
+ end
148
+
149
+ def generate_from_changelog_file(filename, current_rev, rev_parse, tmp_dir)
150
+ diff = Mixlib::ShellOut.new("git diff #{current_rev}..#{rev_parse} -- #{filename}", :cwd => tmp_dir)
151
+ diff.run_command
152
+ diff.stdout.lines.collect {|line| $1 if line =~ /^\+([^+].*)/}.compact
153
+ end
154
+
155
+ def generate_from_git_history(tmp_dir, location, current_rev, rev_parse)
156
+ log = Mixlib::ShellOut.new("git log --no-merges --abbrev-commit --pretty=oneline #{current_rev}..#{rev_parse}", :cwd => tmp_dir)
157
+ log.run_command
158
+ c = log.stdout.lines
159
+ n = https_url(location)
160
+ c = linkify(n, c) if @config[:linkify] and n
161
+ c = c.map { |line| "* " + line } if @config[:markdown]
162
+ c
163
+ end
164
+
165
+ def linkify(url, changelog)
166
+ changelog.map do |line|
167
+ line.gsub(/^([a-f0-9]+) /, '[\1](%s/commit/\1) ' % [url.chomp('.git')])
168
+ end
169
+ end
170
+
171
+ def https_url(location)
172
+ if location.uri =~ /^\w+:\/\/(.*@)?(.*)(\.git?)/
173
+ "https://%s" % [ $2 ]
174
+ else
175
+ fail "Cannot guess http url from git remote url"
176
+ end
177
+ end
178
+
179
+ def short(location)
180
+ if location.uri =~ /([\w-]+)\/([\w-]+)(\.git)?$/
181
+ "%s/%s" % [$1,$2]
182
+ end
183
+ end
184
+
185
+ def shallow_clone(tmp_prefix, uri)
186
+ dir = Dir.mktmpdir(tmp_prefix)
187
+ @tmp_dirs << dir
188
+ clone = Mixlib::ShellOut.new("git clone --bare #{uri} bare-clone", :cwd => dir)
189
+ clone.run_command
190
+ clone.error!
191
+ ::File.join(dir, 'bare-clone')
192
+ end
193
+
194
+ end
195
+ end
@@ -1,5 +1,5 @@
1
1
  module Knife
2
2
  module Changelog
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-changelog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.pre.alpha.pre.23
4
+ version: 0.2.0.pre.alpha.pre.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregoire Seux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-03 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: mixlib-shellout
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +123,7 @@ files:
109
123
  - Rakefile
110
124
  - knife-changelog.gemspec
111
125
  - lib/chef/knife/changelog.rb
126
+ - lib/knife/changelog/changelog.rb
112
127
  - lib/knife/changelog/version.rb
113
128
  - resources/Berksfile
114
129
  - resources/Berksfile.lock