Skip to content

Commit

Permalink
(SIMP-3372) Create rake task for tag compare (#83)
Browse files Browse the repository at this point in the history
Created a rake task to identify simp puppet modules for which
a new version is warranted.  This task fails on a variety of
version problems, so it be used as a lint task in CI.

SIMP-3372 #close
  • Loading branch information
lnemsick-simp authored and trevor-vaughan committed Jul 20, 2017
1 parent 6ee9615 commit fa66e82
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 3.7.0 / 2017-07-10
* Added compare_latest_tag task

### 3.6.0 / 2017-07-03
* Added changelog_annotation task

Expand Down
2 changes: 1 addition & 1 deletion lib/simp/rake/helpers/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module Simp; end
module Simp::Rake; end

class Simp::Rake::Helpers
VERSION = '3.6.0'
VERSION = '3.7.0'
end
95 changes: 95 additions & 0 deletions lib/simp/rake/pupmod/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,101 @@ def define_tasks
puts changelog[module_version]
end

desc <<-EOM
Compare to latest tag.
ARGS:
* :tags_source => Set to the remote from which the tags for this
project can be fetched, e.g. 'upstream' for a
forked project. Defaults to 'origin'.
* :ignore_owner => Execute comparison even if the project owner
is not 'simp'.
* :verbose => Set to 'true' if you want to see detailed messages
NOTES:
Compares mission-impacting (significant) files with the latest
tag and identifies the relevant files that have changed.
Does nothing if the project owner, as specified in the
metadata.json file, is not 'simp'.
When mission-impacting files have changed, fails if
(1) Latest version cannot be extracted from the top-most
CHANGELOG entry.
(2) The latest version in the CHANGELOG (minus the release
qualifier) does not match the version in the metadata.json
file.
(3) A version bump is required but not recorded in both the
CHANGELOG and metadata.json files.
(4) The latest version is < latest tag.
Changes to the following files/directories are not considered
significant:
- Any hidden file/directory (entry that begins with a '.')
- Gemfile
- Gemfile.lock
- Rakefile
- spec directory
- doc directory
EOM
task :compare_latest_tag, [:tags_source, :ignore_owner, :verbose] do |t,args|
require 'json'
require 'puppet/util/package'

tags_source = args[:tags_source].nil? ? 'origin' : args[:tags_source]
ignore_owner = true if args[:ignore_owner].to_s == 'true'
verbose = true if args[:verbose].to_s == 'true'

metadata = JSON.load(File.read('metadata.json'))
module_version = metadata['version']
owner = metadata['name'].split('-')[0]

if (owner == 'simp') or ignore_owner
# determine last tag
`git fetch -t #{tags_source} 2>/dev/null`
tags = `git tag -l`.split("\n")
puts "Available tags from #{tags_source} = #{tags}" if verbose
tags.delete_if { |tag| tag.include?('-') or (tag =~ /^v/) }

if tags.empty?
puts "No tags exist from #{tags_source}"
else
last_tag = (tags.sort { |a,b| Puppet::Util::Package::versioncmp(a,b) })[-1]

# determine mission-impacting files that have changed
files_changed = `git diff tags/#{last_tag} --name-only`.strip.split("\n")
files_changed.delete_if do |file|
file[0] == '.' or file =~ /^Gemfile/ or file == 'Rakefile' or file =~/^spec\// or file =~/^doc/
end

if files_changed.empty?
puts " No new tag required: No significant files have changed since '#{last_tag}' tag"
else
# determine latest CHANGELOG version
line = IO.readlines('CHANGELOG')[0]
match = line.match(/^\*\s+((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{2} \d{4})\s+(.+<.+>)(?:\s+|\s*-\s*)?(\d+\.\d+\.\d+)/)
unless match
fail("ERROR: Invalid CHANGELOG entry. Unable to extract version from '#{line}'")
end

changelog_version = match[3]
unless module_version == changelog_version
fail("ERROR: Version mismatch. module version=#{module_version} changelog version=#{changelog_version}")
end

cmp_result = Puppet::Util::Package::versioncmp(module_version, last_tag)
if cmp_result < 0
fail("ERROR: Version regression. '#{module_version}' < last tag '#{last_tag}'")
elsif cmp_result == 0
fail("ERROR: Version update beyond last tag '#{last_tag}' is required for changes to #{files_changed}")
else
puts " New tag of version '#{module_version}' is required for changes to #{files_changed}"
end
end
end
else
puts " Not evaluating module owned by '#{owner}'"
end
end

desc "Run syntax, lint, and spec tests."
task :test => [
Expand Down

0 comments on commit fa66e82

Please sign in to comment.