lib/changelog/plugin.rb in danger-changelog-0.5.0 vs lib/changelog/plugin.rb in danger-changelog-0.6.0
- old
+ new
@@ -3,65 +3,75 @@
#
# This plugin can, for example, make sure the changes are attributes properly and that they are always terminated with a period.
#
# @example Run all checks on the default CHANGELOG.md.
#
- # changelog.check
+ # changelog.check!
#
# @example Customize the CHANGELOG file name and remind the requester to update it when necessary.
#
# changelog.filename = 'CHANGES.md'
+ # changelog.placeholder_line = "* Your contribution here.\n"
# changelog.have_you_updated_changelog?
#
# @see dblock/danger-changelog
# @tags changelog
class DangerChangelog < Plugin
- # The changelog file name, defaults to `CHANGELOG.md`.
- # @return [String]
- attr_accessor :filename
+ extend Forwardable
- def initialize(dangerfile)
- @filename = 'CHANGELOG.md'
- super
+ def_delegators Danger::Changelog.config, *Danger::Changelog::Config::DELEGATORS
+
+ # Run all checks.
+ # @return [Boolean] true when the check passes
+ def check!
+ have_you_updated_changelog? && is_changelog_format_correct?
end
# Run all checks.
# @param format [Symbol] the format to check against
# @return [Boolean] true when the check passes
- def check(format = Danger::Changelog::Parsers.default_format)
- have_you_updated_changelog? && is_changelog_format_correct?(format)
+ def check(parser = Danger::Changelog::Config.format)
+ warn '[DEPRECATION] `check` is deprecated. Set format with `.format` and use `check!` instead.'
+ config.format = parser
+ check!
end
# Has the CHANGELOG file been modified?
# @return [boolean]
def changelog_changes?
git.modified_files.include?(filename) || git.added_files.include?(filename)
end
+ # Are any files CHANGELOG cares about modified?
+ # @return [boolean]
+ def file_changes?
+ all_files = git.modified_files + git.added_files
+ Danger::Changelog::Config.ignore_files.each do |f|
+ all_files.reject! { |modified_file| f.is_a?(Regexp) ? f.match?(modified_file) : f == modified_file }
+ break if all_files.empty?
+ end
+ all_files.any?
+ end
+
# Have you updated CHANGELOG.md?
# @return [boolean]
def have_you_updated_changelog?
if changelog_changes?
true
- else
- markdown <<-MARKDOWN
-Here's an example of a #{filename} entry:
-
-```markdown
-#{Danger::Changelog::ChangelogEntryLine.example(github)}
-```
- MARKDOWN
- warn "Unless you're refactoring existing code or improving documentation, please update #{filename}.", sticky: false
+ elsif file_changes?
+ warn_update_changelog
false
+ else
+ true
end
end
# Is the CHANGELOG.md format correct?
# @return [boolean]
- def is_changelog_format_correct?(format)
- parser = Danger::Changelog::Parsers.lookup(format)
+ def is_changelog_format_correct?
+ parser = Danger::Changelog::Config.parser
changelog_file = Danger::Changelog::ChangelogFile.new(filename, parser: parser)
if changelog_file.exists?
changelog_file.parse
changelog_file.bad_lines.each do |line|
@@ -79,8 +89,21 @@
changelog_file.good?
else
messaging.fail("The #{filename} file does not exist.", sticky: false)
false
end
+ end
+
+ private
+
+ def warn_update_changelog
+ markdown <<-MARKDOWN
+Here's an example of a #{filename} entry:
+
+```markdown
+#{Danger::Changelog::ChangelogEntryLine.example(github)}
+```
+ MARKDOWN
+ warn "Unless you're refactoring existing code or improving documentation, please update #{filename}.", sticky: false
end
end
end