Class: Releasinator::ValidatorChangelog

Inherits:
Object
  • Object
show all
Defined in:
lib/validator_changelog.rb

Constant Summary collapse

RELEASE_REGEX =
/\d+\.\d+\.\d+/

Instance Method Summary collapse

Constructor Details

#initialize(releasinator_config) ⇒ ValidatorChangelog

Returns a new instance of ValidatorChangelog.



12
13
14
# File 'lib/validator_changelog.rb', line 12

def initialize(releasinator_config)
  @releasinator_config = releasinator_config
end

Instance Method Details

#check_semver_criteria(condition, message) ⇒ Object



61
62
63
64
65
66
# File 'lib/validator_changelog.rb', line 61

def check_semver_criteria(condition, message)
  if !condition
    Printer.fail(message)
    abort()
  end
end

#validate_changelog_contents(changelog_contents) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/validator_changelog.rb', line 68

def validate_changelog_contents(changelog_contents)
  version_header_regexes = [
    ## h2 using --- separator.  Example:
    #  1.0.0
    #  -----
    #  First release!
    '(^#{RELEASE_REGEX}).*\n----.*',

    # h1/h2 header retrieved from https://github.com/tech-angels/vandamme/#format and modified to skip headers with dots in the name
    '^#{0,3} ?([\w\d\.-]+\.[\d\.-]+[a-zA-Z0-9])(?: \W (\w+ \d{1,2}(?:st|nd|rd|th)?,\s\d{4}|\d{4}-\d{2}-\d{2}|\w+))?\n?[=-]*'
  ]

  changelog_hash = nil
  version_header_regexes.each do |version_header_regex|
    parser = Vandamme::Parser.new(changelog: changelog_contents, version_header_exp: version_header_regex, format: 'markdown')
    changelog_hash = parser.parse

    break if !changelog_hash.empty?
  end
  
  if changelog_hash.empty?
    Printer.fail("Unable to find any releases in the CHANGELOG.md.  Please check that your formatting is correct.")
    abort()
  end
  
  Printer.success("Found " + changelog_hash.count.to_s.bold + " release(s) in CHANGELOG.md.")

  validate_semver(changelog_hash)

  changelog_hash.each { |release, changelog| 
    validate_single_changelog_entry(changelog)
  }

  latest_release, latest_release_changelog = changelog_hash.first
  CurrentRelease.new(latest_release, latest_release_changelog)
end

#validate_semver(changelog_hash) ⇒ Object

assume changelog_hash is not empty



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/validator_changelog.rb', line 17

def validate_semver(changelog_hash)
  latest_release, latest_release_changelog = changelog_hash.first
  # extract prefix from first release to use on all subsequent releases
  latest_release_prefix = latest_release.partition(RELEASE_REGEX)[0]

  newer_version = nil
  changelog_hash.each do |key,value|
    prefix, version, suffix = key.partition(RELEASE_REGEX)
    puts "Checking version with prefix:'#{prefix}', version:'#{version}', suffix:'#{suffix}'." if @releasinator_config[:verbose]
    if prefix != latest_release_prefix
      Printer.fail("version #{key} does not start with extracted prefix '#{latest_release_prefix}'.")
      abort()
    end
    older_version = Semantic::Version.new version

    if nil != newer_version
      version_comp = newer_version <=> older_version
      if version_comp < 0
        Printer.fail("Semver releases out of order: #{older_version} should be smaller than #{newer_version}")
        abort()
      elsif version_comp == 0
        # this case cannot be found with Vandamme library because 2 versions in a row get overwritten in the underlying hash
        if suffix.empty?
          Printer.fail("2 semver releases in a row without a suffix (like -beta, -rc1, etc...) is not allowed.")
          abort()
        end
      else
        error_suffix = "version increment error - comparing #{newer_version} to #{older_version} does not pass semver validation."
        # validate the next sequence in semver
        if newer_version.major == older_version.major
          if newer_version.minor == older_version.minor
            check_semver_criteria(newer_version.patch == older_version.patch + 1, "patch #{error_suffix}")
          else
            check_semver_criteria(newer_version.minor == older_version.minor + 1 && newer_version.patch == 0, "minor #{error_suffix}")
          end
        else
          check_semver_criteria(newer_version.major == older_version.major + 1 && newer_version.minor == 0 && newer_version.patch == 0, "major #{error_suffix}")
        end
      end
    end
    newer_version = older_version
  end
end

#validate_single_changelog_entry(entry) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/validator_changelog.rb', line 105

def validate_single_changelog_entry(entry)
  lines = entry.split(/\r?\n/)
  lines.each{ |line| 
    if line.match /^\s*\*\s+.*$/ # regex matches bulleted points
      if !line.match /^\s*\*\s+.*[\!,\?:\.]$/ # regex matches bullet points with punctuation
        Printer.fail("'#{line}' is invalid.  Bulleted points should end in punctuation.")
        abort()
      end
    end
  }
end