2019-12-12 10:08:41 -05:00
# frozen_string_literal: true
2021-03-12 07:09:33 -05:00
require 'gitlab/dangerfiles/title_linting'
2021-01-04 13:10:11 -05:00
2021-01-20 07:11:06 -05:00
module Tooling
2019-12-12 10:08:41 -05:00
module Danger
module Changelog
2020-06-18 11:08:45 -04:00
NO_CHANGELOG_LABELS = [
'tooling' ,
'tooling::pipelines' ,
'tooling::workflow' ,
'ci-build' ,
'meta'
] . freeze
2019-12-12 10:08:41 -05:00
NO_CHANGELOG_CATEGORIES = % i [ docs none ] . freeze
2020-08-20 23:10:16 -04:00
CREATE_CHANGELOG_COMMAND = 'bin/changelog -m %<mr_iid>s "%<mr_title>s"'
CREATE_EE_CHANGELOG_COMMAND = 'bin/changelog --ee -m %<mr_iid>s "%<mr_title>s"'
CHANGELOG_MODIFIED_URL_TEXT = " **CHANGELOG.md was edited.** Please remove the additions and create a CHANGELOG entry. \n \n "
CHANGELOG_MISSING_URL_TEXT = " **[CHANGELOG missing](https://docs.gitlab.com/ee/development/changelog.html)**: \n \n "
2019-12-12 10:08:41 -05:00
2021-03-23 20:09:26 -04:00
OPTIONAL_CHANGELOG_MESSAGE = {
local : " If this merge request [doesn't need a CHANGELOG entry](https://docs.gitlab.com/ee/development/changelog.html # what-warrants-a-changelog-entry), feel free to ignore this message. " ,
ci : << ~ MSG
If you want to create a changelog entry for GitLab FOSS , run the following :
2020-08-20 23:10:16 -04:00
2021-03-23 20:09:26 -04:00
#{CREATE_CHANGELOG_COMMAND}
2020-08-20 23:10:16 -04:00
2021-03-23 20:09:26 -04:00
If you want to create a changelog entry for GitLab EE , run the following instead :
2020-08-20 23:10:16 -04:00
2021-03-23 20:09:26 -04:00
#{CREATE_EE_CHANGELOG_COMMAND}
2020-08-20 23:10:16 -04:00
2021-03-23 20:09:26 -04:00
If this merge request [ doesn ' t need a CHANGELOG entry ] ( https : / / docs . gitlab . com / ee / development / changelog . html #what-warrants-a-changelog-entry), feel free to ignore this message.
MSG
} . freeze
2020-08-20 23:10:16 -04:00
2021-02-24 07:10:54 -05:00
REQUIRED_CHANGELOG_REASONS = {
db_changes : 'introduces a database migration' ,
feature_flag_removed : 'removes a feature flag'
} . freeze
2021-03-23 20:09:26 -04:00
REQUIRED_CHANGELOG_MESSAGE = {
local : " This merge request requires a changelog entry because it [%<reason>s](https://docs.gitlab.com/ee/development/changelog.html # what-warrants-a-changelog-entry). " ,
ci : << ~ MSG
To create a changelog entry , run the following :
2020-08-20 23:10:16 -04:00
2021-03-23 20:09:26 -04:00
#{CREATE_CHANGELOG_COMMAND}
2020-08-20 23:10:16 -04:00
2021-03-23 20:09:26 -04:00
This merge request requires a changelog entry because it [ %< reason > s ] ( https : / / docs . gitlab . com / ee / development / changelog . html #what-warrants-a-changelog-entry).
MSG
} . freeze
2020-08-20 23:10:16 -04:00
2021-02-24 07:10:54 -05:00
def required_reasons
[ ] . tap do | reasons |
2021-03-12 07:09:33 -05:00
reasons << :db_changes if project_helper . changes . added . has_category? ( :migration )
reasons << :feature_flag_removed if project_helper . changes . deleted . has_category? ( :feature_flag )
2021-02-24 07:10:54 -05:00
end
end
2020-08-20 23:10:16 -04:00
def required?
2021-02-24 07:10:54 -05:00
required_reasons . any?
2020-08-20 23:10:16 -04:00
end
def optional?
2020-07-15 14:09:09 -04:00
categories_need_changelog? && without_no_changelog_label?
2019-12-12 10:08:41 -05:00
end
def found
2021-03-12 07:09:33 -05:00
@found || = project_helper . changes . added . by_category ( :changelog ) . files . first
2019-12-12 10:08:41 -05:00
end
2020-03-23 11:09:36 -04:00
def ee_changelog?
found . start_with? ( 'ee/' )
2019-12-12 10:08:41 -05:00
end
2020-08-20 23:10:16 -04:00
def modified_text
CHANGELOG_MODIFIED_URL_TEXT +
2021-03-23 20:09:26 -04:00
( helper . ci? ? format ( OPTIONAL_CHANGELOG_MESSAGE [ :ci ] , mr_iid : helper . mr_iid , mr_title : sanitized_mr_title ) : OPTIONAL_CHANGELOG_MESSAGE [ :local ] )
2020-08-20 23:10:16 -04:00
end
2021-02-24 07:10:54 -05:00
def required_texts
required_reasons . each_with_object ( { } ) do | required_reason , memo |
memo [ required_reason ] =
CHANGELOG_MISSING_URL_TEXT +
2021-03-23 20:09:26 -04:00
( helper . ci? ? format ( REQUIRED_CHANGELOG_MESSAGE [ :ci ] , reason : REQUIRED_CHANGELOG_REASONS . fetch ( required_reason ) , mr_iid : helper . mr_iid , mr_title : sanitized_mr_title ) : REQUIRED_CHANGELOG_MESSAGE [ :local ] )
2021-02-24 07:10:54 -05:00
end
2020-08-20 23:10:16 -04:00
end
def optional_text
CHANGELOG_MISSING_URL_TEXT +
2021-03-23 20:09:26 -04:00
( helper . ci? ? format ( OPTIONAL_CHANGELOG_MESSAGE [ :ci ] , mr_iid : helper . mr_iid , mr_title : sanitized_mr_title ) : OPTIONAL_CHANGELOG_MESSAGE [ :local ] )
2020-08-20 23:10:16 -04:00
end
2019-12-12 10:08:41 -05:00
private
2020-08-20 23:10:16 -04:00
def sanitized_mr_title
2021-03-12 07:09:33 -05:00
Gitlab :: Dangerfiles :: TitleLinting . sanitize_mr_title ( helper . mr_title )
2020-08-20 23:10:16 -04:00
end
2019-12-12 10:08:41 -05:00
def categories_need_changelog?
2021-03-12 07:09:33 -05:00
( project_helper . changes . categories - NO_CHANGELOG_CATEGORIES ) . any?
2019-12-12 10:08:41 -05:00
end
2020-07-15 14:09:09 -04:00
def without_no_changelog_label?
2021-02-24 07:10:54 -05:00
( helper . mr_labels & NO_CHANGELOG_LABELS ) . empty?
2020-07-15 14:09:09 -04:00
end
2019-12-12 10:08:41 -05:00
end
end
end