Class: Releasinator::ValidatorChangelog

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

Instance Method Summary collapse

Instance Method Details

#check_semver_criteria(condition, message) ⇒ Object



42
43
44
45
46
47
# File 'lib/validator_changelog.rb', line 42

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

#validate_changelog_contents(changelog_contents, prefix = "") ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/validator_changelog.rb', line 49

def validate_changelog_contents(changelog_contents, prefix="")
  version_header_regexes = [
    ## h2 using --- separator.  Example:
    #  1.0.0
    #  -----
    #  First release!
    '(^\d+\.\d+\.\d+).*\n----.*',

    # h1/h2 header retrieved from https://github.com/tech-angels/vandamme/#format
    '^#{0,3} ?([\w\d\.-]+\.[\w\d\.-]+[a-zA-Z0-9])(?: \/ (\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, prefix)

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

#validate_semver(changelog_hash, prefix = "") ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/validator_changelog.rb', line 10

def validate_semver(changelog_hash, prefix="")
  newer_version = nil
  changelog_hash.each do |key,value|
    if !key.start_with? prefix
      Printer.fail("version #{key} does not start with prefix '#{prefix}'.")
      abort()
    end
    older_version = Semantic::Version.new key[prefix.length..-1]

    if nil != newer_version
      version_comp = newer_version <=> older_version
      if version_comp < 1
        Printer.fail("Semver releases out of order: #{older_version} should be smaller than #{newer_version}")
        abort()
      end

      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
    newer_version = older_version
  end
end