summaryrefslogtreecommitdiff
path: root/tooling/danger/changelog.rb
blob: f7f505f51a6394fde6cb469a6e9cea921d7ca16a (plain)
1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
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
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true

require_relative 'title_linting'

module Tooling
  module Danger
    module Changelog
      NO_CHANGELOG_LABELS = [
        'tooling',
        'tooling::pipelines',
        'tooling::workflow',
        'ci-build',
        'meta'
      ].freeze
      NO_CHANGELOG_CATEGORIES = %i[docs none].freeze
      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"

      OPTIONAL_CHANGELOG_MESSAGE = <<~MSG
      If you want to create a changelog entry for GitLab FOSS, run the following:

          #{CREATE_CHANGELOG_COMMAND}

      If you want to create a changelog entry for GitLab EE, run the following instead:

          #{CREATE_EE_CHANGELOG_COMMAND}

      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

      REQUIRED_CHANGELOG_MESSAGE = <<~MSG
      To create a changelog entry, run the following:

          #{CREATE_CHANGELOG_COMMAND}

      This merge request requires a changelog entry because it [introduces a database migration](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry).
      MSG

      def required?
        git.added_files.any? { |path| path =~ %r{\Adb/(migrate|post_migrate)/} }
      end
      alias_method :db_changes?, :required?

      def optional?
        categories_need_changelog? && without_no_changelog_label?
      end

      def found
        @found ||= git.added_files.find { |path| path =~ %r{\A(ee/)?(changelogs/unreleased)(-ee)?/} }
      end

      def ee_changelog?
        found.start_with?('ee/')
      end

      def modified_text
        CHANGELOG_MODIFIED_URL_TEXT +
          format(OPTIONAL_CHANGELOG_MESSAGE, mr_iid: mr_iid, mr_title: sanitized_mr_title)
      end

      def required_text
        CHANGELOG_MISSING_URL_TEXT +
          format(REQUIRED_CHANGELOG_MESSAGE, mr_iid: mr_iid, mr_title: sanitized_mr_title)
      end

      def optional_text
        CHANGELOG_MISSING_URL_TEXT +
          format(OPTIONAL_CHANGELOG_MESSAGE, mr_iid: mr_iid, mr_title: sanitized_mr_title)
      end

      private

      def mr_iid
        gitlab.mr_json["iid"]
      end

      def sanitized_mr_title
        TitleLinting.sanitize_mr_title(gitlab.mr_json["title"])
      end

      def categories_need_changelog?
        (helper.changes_by_category.keys - NO_CHANGELOG_CATEGORIES).any?
      end

      def without_no_changelog_label?
        (gitlab.mr_labels & NO_CHANGELOG_LABELS).empty?
      end
    end
  end
end