shipnotes 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +10 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +265 -0
  5. data/app/controllers/shipnotes/releases_controller.rb +61 -0
  6. data/app/helpers/shipnotes/releases_helper.rb +38 -0
  7. data/app/views/layouts/shipnotes/application.html.erb +24 -0
  8. data/app/views/shipnotes/releases/index.atom.builder +20 -0
  9. data/app/views/shipnotes/releases/index.html.erb +51 -0
  10. data/config/routes.rb +7 -0
  11. data/exe/shipnotes +7 -0
  12. data/lib/shipnotes/check.rb +218 -0
  13. data/lib/shipnotes/cli.rb +312 -0
  14. data/lib/shipnotes/config.rb +278 -0
  15. data/lib/shipnotes/differs/json_schema.rb +45 -0
  16. data/lib/shipnotes/differs/mcp_tools.rb +50 -0
  17. data/lib/shipnotes/differs/openapi.rb +55 -0
  18. data/lib/shipnotes/differs.rb +33 -0
  19. data/lib/shipnotes/engine.rb +44 -0
  20. data/lib/shipnotes/engine_configuration.rb +21 -0
  21. data/lib/shipnotes/entry.rb +18 -0
  22. data/lib/shipnotes/entry_writer.rb +116 -0
  23. data/lib/shipnotes/field.rb +55 -0
  24. data/lib/shipnotes/finding.rb +7 -0
  25. data/lib/shipnotes/git_diff.rb +65 -0
  26. data/lib/shipnotes/github_labels.rb +28 -0
  27. data/lib/shipnotes/init.rb +69 -0
  28. data/lib/shipnotes/publication.rb +46 -0
  29. data/lib/shipnotes/release.rb +32 -0
  30. data/lib/shipnotes/releaser.rb +128 -0
  31. data/lib/shipnotes/renderer.rb +92 -0
  32. data/lib/shipnotes/repository.rb +100 -0
  33. data/lib/shipnotes/schema.rb +82 -0
  34. data/lib/shipnotes/semver.rb +83 -0
  35. data/lib/shipnotes/source.rb +47 -0
  36. data/lib/shipnotes/surface.rb +32 -0
  37. data/lib/shipnotes/templates/changelog.md.erb +11 -0
  38. data/lib/shipnotes/templates/release_notes.md.erb +1 -0
  39. data/lib/shipnotes/version.rb +5 -0
  40. data/lib/shipnotes/version_bump.rb +34 -0
  41. data/lib/shipnotes/version_targets.rb +70 -0
  42. data/lib/shipnotes/yaml_file.rb +22 -0
  43. data/lib/shipnotes.rb +52 -0
  44. metadata +91 -0
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ Field = Data.define(:name, :type, :required, :values, :default, :description)
5
+
6
+ class Field
7
+ TYPES = %w[string text enum list date boolean].freeze
8
+ DATE = /\A\d{4}-\d{2}-\d{2}\z/
9
+
10
+ def self.blank?(value)
11
+ value.nil? || (value.respond_to?(:empty?) && value.empty?) || (value.is_a?(String) && value.strip.empty?)
12
+ end
13
+
14
+ def self.date?(value)
15
+ value.is_a?(String) && value.match?(DATE) && Date.iso8601(value) && true
16
+ rescue Date::Error
17
+ false
18
+ end
19
+
20
+ def errors(value)
21
+ case type
22
+ when "string", "text"
23
+ value.is_a?(String) ? [] : [ "#{name} must be a string" ]
24
+ when "enum"
25
+ values.include?(value) ? [] : [ "#{name} must be one of #{values.join(', ')}" ]
26
+ when "list" then list_errors(value)
27
+ when "date"
28
+ self.class.date?(value) ? [] : [ "#{name} must be a YYYY-MM-DD date" ]
29
+ when "boolean"
30
+ [ true, false ].include?(value) ? [] : [ "#{name} must be true or false" ]
31
+ end
32
+ end
33
+
34
+ # Turns text typed at a prompt or passed as --field into the stored value.
35
+ def coerce(text)
36
+ case type
37
+ when "list" then text.split(",").map(&:strip).reject(&:empty?)
38
+ when "boolean" then %w[true yes y 1].include?(text.strip.downcase)
39
+ else text.strip
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def list_errors(value)
46
+ unless value.is_a?(Array) && value.any? && value.all?(String)
47
+ return [ "#{name} must be a non-empty list#{" of #{values.join(', ')}" if values.any?}" ]
48
+ end
49
+ return [ "#{name} lists a value twice" ] unless value.uniq.size == value.size
50
+
51
+ unknown = values.any? ? value - values : []
52
+ unknown.any? ? [ "#{name} has unknown values: #{unknown.join(', ')} (allowed: #{values.join(', ')})" ] : []
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ Finding = Data.define(:subject, :breaking, :internal, :message) do
5
+ def to_s = "#{breaking ? 'BREAKING' : 'change'}#{internal ? ' [internal]' : ''} #{subject}: #{message}"
6
+ end
7
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ class GitDiff
5
+ Error = Class.new(Shipnotes::Error)
6
+ Change = Data.define(:status, :path)
7
+
8
+ attr_reader :root, :base
9
+
10
+ def initialize(root, base_ref)
11
+ @root = File.expand_path(root.to_s)
12
+ @base = git("merge-base", base_ref, "HEAD").strip
13
+ end
14
+
15
+ def changes
16
+ @changes ||= git("diff", "--name-status", "--no-renames", base, "HEAD").lines.map do |line|
17
+ status, path = line.chomp.split("\t", 2)
18
+ Change.new(status: status[0], path: path)
19
+ end
20
+ end
21
+
22
+ def base_source = Source::GitRevision.new(root, base)
23
+ def head_source = Source::GitRevision.new(root, "HEAD")
24
+
25
+ # Added and removed lines per file, without the diff's own +++/--- headers.
26
+ def changed_lines
27
+ @changed_lines ||= begin
28
+ lines = Hash.new { |hash, key| hash[key] = [] }
29
+ current = old_path = nil
30
+ header = false
31
+ git("diff", "--unified=0", "--no-renames", "--no-color", base, "HEAD").each_line do |line|
32
+ if line.start_with?("diff --git ")
33
+ header = true
34
+ current = nil
35
+ elsif header && line.start_with?("--- ")
36
+ old_path = diff_path(line)
37
+ elsif header && line.start_with?("+++ ")
38
+ new_path = diff_path(line)
39
+ current = new_path == "/dev/null" ? old_path : new_path
40
+ elsif line.start_with?("@@")
41
+ header = false
42
+ elsif current && !header && line.match?(/\A[+-]/)
43
+ lines[current] << line.chomp
44
+ end
45
+ end
46
+ lines
47
+ end
48
+ end
49
+
50
+ def uncommitted(directory)
51
+ git("status", "--porcelain", "--untracked-files=all", "--", "#{directory}/").lines.map { |line| line.chomp[3..] }
52
+ end
53
+
54
+ private
55
+
56
+ def diff_path(line) = line.chomp[4..].sub(%r{\A[ab]/}, "")
57
+
58
+ def git(*args)
59
+ output, error, status = Open3.capture3("git", *args, chdir: root)
60
+ raise Error, "git #{args.join(' ')} failed: #{error.strip}" unless status.success?
61
+
62
+ output
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ # Reads the current pull request's labels through the GitHub CLI. In GitHub
5
+ # Actions it uses PR_NUMBER (or the event's number) and GITHUB_REPOSITORY.
6
+ class GitHubLabels
7
+ def initialize(root, env: ENV, runner: Open3.method(:capture3))
8
+ @root = root
9
+ @env = env
10
+ @runner = runner
11
+ end
12
+
13
+ # nil when the labels cannot be read: no gh, no PR, or no access.
14
+ def labels
15
+ args = [ "gh", "pr", "view" ]
16
+ number = @env["PR_NUMBER"].to_s[/\A\d+\z/]
17
+ args << number if number
18
+ args.push("--repo", @env["GITHUB_REPOSITORY"]) if number && @env["GITHUB_REPOSITORY"]
19
+ args.push("--json", "labels", "--jq", ".labels[].name")
20
+ output, _error, status = @runner.call(*args, chdir: @root)
21
+ status.success? ? output.lines.map(&:strip).reject(&:empty?) : nil
22
+ rescue SystemCallError
23
+ nil
24
+ end
25
+
26
+ def include?(name) = Array(labels).include?(name)
27
+ end
28
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ # Writes a starter .shipnotes.yml and the entry folders.
5
+ class Init
6
+ RAILS_SURFACES = <<~YAML
7
+ surfaces:
8
+ app:
9
+ title: App
10
+ description: Pages, routes and anything a signed-in user sees.
11
+ paths:
12
+ - app/views/**
13
+ - app/javascript/**
14
+ - config/routes.rb
15
+ - config/routes/**
16
+ exclude:
17
+ - app/views/admin/**
18
+ YAML
19
+
20
+ def initialize(root, rails: false, version_file: nil)
21
+ @root = File.expand_path(root.to_s)
22
+ @rails = rails
23
+ @version_file = version_file
24
+ end
25
+
26
+ def call
27
+ path = File.join(@root, Config::FILE)
28
+ raise Error, "#{Config::FILE} already exists" if File.exist?(path)
29
+
30
+ File.write(path, template)
31
+ config = Config.load(@root)
32
+ [ config.unreleased_dir, config.releases_dir ].each do |directory|
33
+ FileUtils.mkdir_p(File.join(@root, directory))
34
+ keep = File.join(@root, directory, ".keep")
35
+ File.write(keep, "") unless File.exist?(keep)
36
+ end
37
+ [ Config::FILE, "#{config.unreleased_dir}/", "#{config.releases_dir}/" ]
38
+ end
39
+
40
+ private
41
+
42
+ def template
43
+ <<~YAML
44
+ # Shipnotes configuration. Every key is optional; see the README for the full list.
45
+ directory: changelog
46
+
47
+ fields:
48
+ title: { type: string, required: true, description: One line that names the change }
49
+ kind: { type: enum, required: true, values: [added, changed, deprecated, removed, fixed, security] }
50
+ body: { type: text, description: Details for the reader }
51
+
52
+ #{@rails ? RAILS_SURFACES : "surfaces: {}\n"}
53
+ versioning:
54
+ scheme: semver
55
+ release_id: #{@rails && !@version_file ? 'date' : 'version'}
56
+ files:#{version_files}
57
+
58
+ render:
59
+ output: CHANGELOG.md
60
+ YAML
61
+ end
62
+
63
+ def version_files
64
+ return " []" unless @version_file
65
+
66
+ "\n - path: #{@version_file}\n pattern: 'VERSION = \"(?<version>[^\"]+)\"'"
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ # The read side for anything that shows the changelog to people: the Rails
5
+ # engine, a rendered CHANGELOG.md, or an app's own page.
6
+ class Publication
7
+ def initialize(config, repository: Repository.new(config), flag_enabled: nil)
8
+ @config = config
9
+ @repository = repository
10
+ @flag_enabled = flag_enabled
11
+ end
12
+
13
+ # Newest first. Releases with no public entry for the surface are left out.
14
+ def releases(surface: nil)
15
+ keys = surface && Array(surface).map(&:to_s)
16
+ @repository.releases.reverse.filter_map do |release|
17
+ entries = public_entries(release.entries)
18
+ entries = entries.select { |entry| (entry.surfaces & keys).any? } if keys
19
+ release.with(entries: entries) if entries.any?
20
+ end
21
+ end
22
+
23
+ def entries(surface: nil) = releases(surface: surface).flat_map(&:entries)
24
+
25
+ def public_entries(entries) = entries.select { |entry| public?(entry) }
26
+
27
+ # An entry without an audience takes the field's default; with no default
28
+ # it stays unpublished, since leaking an internal note is the worse mistake.
29
+ def public?(entry)
30
+ field = @config.publication.audience_field
31
+ return true if field.nil?
32
+
33
+ value = Field.blank?(entry[field]) ? @config.field(field).default : entry[field]
34
+ @config.publication.public_values.include?(value.to_s)
35
+ end
36
+
37
+ # An entry behind a feature flag that is still off has shipped as code but
38
+ # not to users yet.
39
+ def status(entry)
40
+ flag = @config.publication.flag_field && entry[@config.publication.flag_field]
41
+ return "released" if Field.blank?(flag) || @flag_enabled.nil?
42
+
43
+ @flag_enabled.call(flag.to_s) ? "released" : "pending"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ Release = Data.define(:id, :date, :version, :entries)
5
+
6
+ class Release
7
+ DATE_ID = /\A(\d{4})\.(\d{2})\.(\d{2})\.(\d+)\z/
8
+
9
+ def self.from_h(hash)
10
+ new(id: hash.fetch("release").to_s, date: hash.fetch("date").to_s, version: hash["version"]&.to_s,
11
+ entries: hash.fetch("entries").map { |entry| Entry.from_h(entry) })
12
+ end
13
+
14
+ def self.sort_key(release)
15
+ if (match = release.id.match(DATE_ID))
16
+ [ release.date, 0, match[4].to_i ]
17
+ elsif Semver.valid?(release.id)
18
+ [ release.date, 1, Semver.parse(release.id) ]
19
+ else
20
+ [ release.date, 2, release.id ]
21
+ end
22
+ end
23
+
24
+ def includes_entry?(entry_id) = entries.any? { |entry| entry.id == entry_id }
25
+
26
+ def to_h
27
+ hash = { "release" => id, "date" => date }
28
+ hash["version"] = version if version
29
+ hash.merge("entries" => entries.map(&:to_h))
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ # Moves every unreleased entry into one release file, bumps the version
5
+ # files and re-renders the changelog.
6
+ class Releaser
7
+ Error = Class.new(Shipnotes::Error)
8
+ Result = Data.define(:id, :date, :version, :previous_version, :level, :entries, :written, :deleted, :dry_run)
9
+
10
+ # The one rule for which version a release may carry, shared with the PR
11
+ # check so a release PR is held to what `release` would have written. The
12
+ # first release has nothing to bump from, so it may keep the starting version.
13
+ def self.version_errors(config:, current:, computed:, chosen:, first_release:)
14
+ return chosen ? [ "this project does not version releases (versioning.scheme: none)" ] : [] unless config.versioning.semver?
15
+ return [] unless chosen
16
+ return [ "#{chosen.inspect} is not a MAJOR.MINOR.PATCH version" ] unless Semver.valid?(chosen)
17
+
18
+ version = Semver.parse(chosen)
19
+ base = Semver.parse(current)
20
+ errors = []
21
+ errors << "version #{chosen} must be greater than #{current}" if first_release ? version < base : version <= base
22
+ if computed && !first_release && version.core < Semver.parse(computed).core
23
+ errors << "version #{chosen} is below #{computed}, which the entries require"
24
+ end
25
+ errors
26
+ end
27
+
28
+ def self.current_version(config, repository, versions)
29
+ repository.last_version || versions.values.compact.first || config.versioning.initial
30
+ end
31
+
32
+ def initialize(config, today: Date.today)
33
+ @config = config
34
+ @repository = Repository.new(config)
35
+ @targets = VersionTargets.new(config)
36
+ @today = today
37
+ end
38
+
39
+ def call(version: nil, date: nil, dry_run: false)
40
+ problems = @repository.problems
41
+ raise Error, "fix the changelog first:\n#{problems.join("\n")}" if problems.any?
42
+
43
+ entries = @repository.entries
44
+ return nil if entries.empty?
45
+
46
+ date = parse_date(date)
47
+ last = @repository.releases.last
48
+ raise Error, "release date #{date.iso8601} is before the last release (#{last.id}, #{last.date})" if last && date.iso8601 < last.date
49
+ current = self.class.current_version(@config, @repository, versions)
50
+ check_targets(current)
51
+ final = final_version(current, entries, version)
52
+ id = release_id(date, final)
53
+ path = File.join(@config.releases_dir, "#{id}.yml")
54
+ raise Error, "#{path} already exists" if File.exist?(File.join(@config.root, path))
55
+
56
+ release = Release.new(id: id, date: date.iso8601, version: @config.versioning.semver? ? (final || current) : nil, entries: entries)
57
+ result = Result.new(id: id, date: date.iso8601, version: release.version, previous_version: (current if @config.versioning.semver?),
58
+ level: @config.versioning.semver? ? VersionBump.new(@config.versioning).level(entries) : nil,
59
+ entries: entries.map(&:id), written: [ path, *written_extras(final, current) ],
60
+ deleted: entries.map { |entry| entry_path(entry) }, dry_run: dry_run)
61
+ write(release, path, final, current) unless dry_run
62
+ result
63
+ end
64
+
65
+ private
66
+
67
+ def versions = @targets.read { |path| read(path) }
68
+
69
+ def read(path)
70
+ full = File.join(@config.root, path)
71
+ File.file?(full) ? File.read(full) : nil
72
+ end
73
+
74
+ def check_targets(current)
75
+ return unless @config.versioning.semver?
76
+
77
+ missing = @targets.paths.reject { |path| File.file?(File.join(@config.root, path)) }
78
+ raise Error, "version files not found: #{missing.join(', ')}" if missing.any?
79
+
80
+ wrong = versions.reject { |_label, value| value == current }
81
+ return if wrong.empty?
82
+
83
+ raise Error, wrong.map { |label, value| "#{label} is #{value.inspect}, but the release history is at #{current}" }.join("\n")
84
+ end
85
+
86
+ def final_version(current, entries, chosen)
87
+ semver = @config.versioning.semver?
88
+ computed = VersionBump.new(@config.versioning).next_version(current, entries) if semver
89
+ errors = self.class.version_errors(config: @config, current: current, computed: computed, chosen: chosen,
90
+ first_release: @repository.releases.empty?)
91
+ raise Error, errors.join("\n") if errors.any?
92
+
93
+ chosen || computed if semver
94
+ end
95
+
96
+ def release_id(date, version)
97
+ return version if @config.versioning.version_ids?
98
+
99
+ prefix = date.strftime("%Y.%m.%d")
100
+ taken = @repository.releases.map(&:id).filter_map { |id| id.match(Release::DATE_ID) && id.start_with?("#{prefix}.") && id.split(".").last.to_i }
101
+ "#{prefix}.#{taken.max.to_i + 1}"
102
+ end
103
+
104
+ def write(release, path, final, current)
105
+ @targets.write(@config.root, final) if final && final != current
106
+ full = File.join(@config.root, path)
107
+ FileUtils.mkdir_p(File.dirname(full))
108
+ File.write(full, YamlFile.dump(release.to_h))
109
+ release.entries.each { |entry| File.delete(File.join(@config.root, entry_path(entry))) }
110
+ Renderer.new(@config).write
111
+ end
112
+
113
+ def written_extras(final, current)
114
+ extras = final && final != current ? @targets.paths : []
115
+ extras + [ @config.render.output ].compact
116
+ end
117
+
118
+ def entry_path(entry) = File.join(@config.unreleased_dir, "#{entry.id}.yml")
119
+
120
+ def parse_date(value)
121
+ return @today if value.nil?
122
+ return value if value.is_a?(Date)
123
+ raise Error, "--date must be YYYY-MM-DD" unless Field.date?(value.to_s)
124
+
125
+ Date.iso8601(value.to_s)
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ class Renderer
5
+ TEMPLATES = File.expand_path("templates", __dir__)
6
+
7
+ # The binding every template renders against; its public methods are the
8
+ # helpers a custom template can call.
9
+ class View
10
+ attr_reader :config, :title, :releases, :unreleased, :release
11
+
12
+ def initialize(config:, title: nil, releases: [], unreleased: [], release: nil)
13
+ @config = config
14
+ @title = title
15
+ @releases = releases
16
+ @unreleased = unreleased
17
+ @release = release
18
+ end
19
+
20
+ def release_heading(release)
21
+ label = release.version && release.version != release.id ? " (#{release.version})" : ""
22
+ "[#{release.id}] - #{release.date}#{label}"
23
+ end
24
+
25
+ def groups(entries)
26
+ field = config.render.group_by && config.field(config.render.group_by)
27
+ return [ [ nil, entries ] ] unless field
28
+
29
+ grouped = entries.group_by { |entry| entry[field.name].to_s }
30
+ order = field.values.any? ? field.values : grouped.keys.sort
31
+ (order & grouped.keys).map { |value| [ heading(value), grouped[value] ] } +
32
+ (grouped.keys - order).sort.map { |value| [ heading(value), grouped[value] ] }
33
+ end
34
+
35
+ def heading(value)
36
+ config.render.headings.fetch(value.to_s) { value.to_s.tr("_-", " ").capitalize }
37
+ end
38
+
39
+ def section(entries, level: 3)
40
+ groups(entries).map do |name, items|
41
+ head = name ? "\n#{'#' * level} #{name}\n" : ""
42
+ "#{head}\n#{items.map { |entry| entry_markdown(entry) }.join("\n")}\n"
43
+ end.join
44
+ end
45
+
46
+ def entry_markdown(entry)
47
+ details = config.render.details.filter_map { |name| entry[name] }.map(&:to_s).reject { |text| text.strip.empty? }
48
+ lines = [ "- #{entry.title}" ]
49
+ details.each { |text| lines << "" << text.strip.gsub(/^(?=.)/, " ") }
50
+ "#{lines.join("\n")}\n"
51
+ end
52
+
53
+ def render(template) = ERB.new(template, trim_mode: "-").result(binding).gsub(/\n{3,}/, "\n\n")
54
+ end
55
+
56
+ def initialize(config, repository = Repository.new(config))
57
+ @config = config
58
+ @repository = repository
59
+ @publication = Publication.new(config, repository: repository)
60
+ end
61
+
62
+ def changelog(unreleased: false)
63
+ entries = unreleased ? @publication.public_entries(@repository.entries) : []
64
+ View.new(config: @config, title: @config.render.title, releases: @publication.releases, unreleased: entries)
65
+ .render(template(@config.render.template, "changelog.md.erb"))
66
+ end
67
+
68
+ def notes(release)
69
+ public_release = release.with(entries: @publication.public_entries(release.entries))
70
+ View.new(config: @config, release: public_release).render(template(nil, "release_notes.md.erb")).sub(/\A\n+/, "").sub(/\n+\z/, "\n")
71
+ end
72
+
73
+ def write(unreleased: false)
74
+ output = @config.render.output
75
+ return nil unless output
76
+
77
+ path = File.join(@config.root, output)
78
+ FileUtils.mkdir_p(File.dirname(path))
79
+ File.write(path, changelog(unreleased: unreleased))
80
+ output
81
+ end
82
+
83
+ private
84
+
85
+ def template(custom, default)
86
+ path = custom ? File.join(@config.root, custom) : File.join(TEMPLATES, default)
87
+ raise Error, "render template #{custom} does not exist" unless File.file?(path)
88
+
89
+ File.read(path)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipnotes
4
+ class Repository
5
+ Problem = Data.define(:path, :message) do
6
+ def to_s = "#{path}: #{message}"
7
+ end
8
+
9
+ attr_reader :config, :source
10
+
11
+ def initialize(config, source: Source::Filesystem.new(config.root))
12
+ @config = config
13
+ @source = source
14
+ @schema = Schema.new(config)
15
+ end
16
+
17
+ def entries
18
+ @entries ||= files(config.unreleased_dir).filter_map do |path|
19
+ data = parse(path)
20
+ next unless data.is_a?(Hash)
21
+
22
+ Entry.new(id: File.basename(path, ".yml"), data: data)
23
+ end
24
+ end
25
+
26
+ def releases
27
+ @releases ||= files(config.releases_dir).filter_map do |path|
28
+ data = parse(path)
29
+ next unless data.is_a?(Hash) && @schema.release_errors(data, File.basename(path)).empty?
30
+
31
+ Release.from_h(data)
32
+ end.sort_by { |release| Release.sort_key(release) }
33
+ end
34
+
35
+ def released_entries
36
+ releases.flat_map(&:entries).to_h { |entry| [ entry.id, entry ] }
37
+ end
38
+
39
+ def last_version = releases.reverse_each.find(&:version)&.version
40
+
41
+ def problems
42
+ problems = source.list(config.directory).filter_map do |path|
43
+ name = File.basename(path)
44
+ next if name.start_with?(".")
45
+
46
+ file_problems(path).map { |message| Problem.new(path, message) }
47
+ end.flatten
48
+ problems + duplicate_problems
49
+ end
50
+
51
+ private
52
+
53
+ def files(directory)
54
+ source.list(directory).select { |path| File.dirname(path) == directory && path.end_with?(".yml") }
55
+ end
56
+
57
+ def parse(path)
58
+ YamlFile.parse(source.read(path))
59
+ rescue Psych::Exception
60
+ nil
61
+ end
62
+
63
+ def file_problems(path)
64
+ return [ "only .yml files belong in #{config.directory}/" ] unless path.end_with?(".yml")
65
+
66
+ data = YamlFile.parse(source.read(path))
67
+ case File.dirname(path)
68
+ when config.unreleased_dir
69
+ slug = File.basename(path, ".yml")
70
+ (slug.match?(Entry::SLUG) ? [] : [ "file name must be a lowercase slug (a-z, 0-9, -)" ]) + @schema.entry_errors(data)
71
+ when config.releases_dir
72
+ @schema.release_errors(data, File.basename(path))
73
+ else
74
+ [ "belongs in #{config.unreleased_dir}/ or #{config.releases_dir}/" ]
75
+ end
76
+ rescue Psych::Exception => error
77
+ [ "is not valid YAML: #{error.message}" ]
78
+ end
79
+
80
+ def duplicate_problems
81
+ seen = {}
82
+ problems = []
83
+ releases.each do |release|
84
+ release.entries.each do |entry|
85
+ if seen[entry.id]
86
+ problems << Problem.new("#{config.releases_dir}/#{release.id}.yml", "entry #{entry.id} is already in release #{seen[entry.id]}")
87
+ end
88
+ seen[entry.id] ||= release.id
89
+ end
90
+ end
91
+ entries.each do |entry|
92
+ next unless seen[entry.id]
93
+
94
+ problems << Problem.new("#{config.unreleased_dir}/#{entry.id}.yml",
95
+ "id #{entry.id} is already in release #{seen[entry.id]}; pick a new file name")
96
+ end
97
+ problems
98
+ end
99
+ end
100
+ end