diff options
author | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-02-27 22:57:53 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-02-28 13:57:21 +0300 |
commit | 9a36582c77764f12173261a3476cda2446e54e33 (patch) | |
tree | 0161ba6c2dbc81687eb813f08c9a08a4a586cd38 /danger/changelog | |
parent | d92c70a3e87a9fabccdbff5e88548b62e4f77efb (diff) |
Have GitalyBot apply labels throught Danger
Using Danger a small rule book, @GitalyBot will apply labels and have a
few more rules on the changelog.
Diffstat (limited to 'danger/changelog')
-rw-r--r-- | danger/changelog/Dangerfile | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/danger/changelog/Dangerfile b/danger/changelog/Dangerfile new file mode 100644 index 000000000..af8e6b93d --- /dev/null +++ b/danger/changelog/Dangerfile @@ -0,0 +1,60 @@ +require 'yaml' + +NO_CHANGELOG_LABELS = %w[backstage Documentation test].freeze +CREATE_CHANGELOG_MESSAGE = <<~MSG.freeze +You can create one with: + +``` +_support/changelog -m %<mr_iid>s "%<mr_title>s" +``` + +If your merge request doesn't warrant a CHANGELOG entry, +consider adding any of the %<labels>s labels. +MSG + +def check_changelog(path) + yaml = YAML.safe_load(File.read(path)) + + fail "`title` should be set, in #{gitlab.html_link(path)}! #{SEE_DOC}" if yaml["title"].nil? + fail "`type` should be set, in #{gitlab.html_link(path)}! #{SEE_DOC}" if yaml["type"].nil? + + if yaml["merge_request"].nil? + message "Consider setting `merge_request` to #{gitlab.mr_json["iid"]} in #{gitlab.html_link(path)}. #{SEE_DOC}" + elsif yaml["merge_request"] != gitlab.mr_json["iid"] + fail "Merge request ID was not set to #{gitlab.mr_json["iid"]}! #{SEE_DOC}" + end + + if yaml["type"].nil? + fail "No type was set in the changelog" + end +rescue Psych::SyntaxError, Psych::DisallowedClass, Psych::BadAlias + # YAML could not be parsed, fail the build. + fail "#{gitlab.html_link(path)} isn't valid YAML! #{SEE_DOC}" +rescue StandardError => e + warn "There was a problem trying to check the Changelog. Exception: #{e.name} - #{e.message}" +end + +def presented_no_changelog_labels + NO_CHANGELOG_LABELS.map { |label| "~#{label}" }.join(', ') +end + +changelog_needed = (gitlab.mr_labels & NO_CHANGELOG_LABELS).empty? +changelog_found = git.added_files.find { |path| path =~ %r{\Achangelogs/unreleased/} } + +mr_title = gitlab.mr_json["title"].gsub(/^WIP: */, '') + +if git.modified_files.include?("CHANGELOG.md") + fail "**CHANGELOG.md was edited.** Please remove the additions and create a CHANGELOG entry.\n\n" + + format(CREATE_CHANGELOG_MESSAGE, mr_iid: gitlab.mr_json["iid"], mr_title: mr_title, labels: presented_no_changelog_labels) +end + +if changelog_needed + if changelog_found + check_changelog(changelog_found) + else + warn "**[CHANGELOG missing](https://docs.gitlab.com/ce/development/changelog.html).**\n\n" + + format(CREATE_CHANGELOG_MESSAGE, mr_iid: gitlab.mr_json["iid"], mr_title: mr_title, labels: presented_no_changelog_labels) + end +end + +# vim: ft=ruby |