gitlab-dangerfiles 2.9.1 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe54dfd04f6f991218f15df710dbc9e0669e07f4f83c07f6c62ce10048fad8fb
4
- data.tar.gz: af0c4147142df3f3f524c9c955bfd62bd2a249e04f69c72873f63db987c09b40
3
+ metadata.gz: 4a13e98277af51b86d8b4afca50ca70527b3b0ecba77e5a8123ce3a4b25466ee
4
+ data.tar.gz: 36dcfa615dad06e9cf4648ce7654f872dc79a8b1ad6d32c90d4af70f6d655e9e
5
5
  SHA512:
6
- metadata.gz: 9ce6c07674bfa305f3b95ff85d9b50e1e2bb453d14468693ffcac6a2a31375d615642ccc7387a41076335956a0079c7423e69be8ccfd9a02fde849ff6a48251b
7
- data.tar.gz: 5f938ce58903b1e77ce025059375112b54b86b8f8aa88a061c5ab3f1346d0faf2302f1a89bf4e18a3ee0921c039ebfd9763791f186695fad31c6d087e5059e06
6
+ metadata.gz: c16d35634b11646fc95c89f7868aa91541adbcc407ad923b4625dc2d55ed27a007ead9ce9f5cc1fdd1e3aecd473f566498955ffaa209b6e6d1cba93388ebdc00
7
+ data.tar.gz: 7a20be61d0f2777804315d02343d666e917c996e94b61916330bf2f7ac602ec6e35158a6768f0b73e5f4f1d420af435bae397ce72681379ce7a1cc6274c3180f
@@ -40,14 +40,17 @@ module Danger
40
40
  This merge request requires a changelog entry because it [%<reason>s](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry).
41
41
  MSG
42
42
  }.freeze
43
-
44
- def self.categories
45
- @categories ||= YAML
46
- .load_file("#{ENV["CI_PROJECT_DIR"]}/.gitlab/changelog_config.yml")
47
- .fetch("categories")
48
- .keys
49
- .freeze rescue []
50
- end
43
+ CHANGELOG_CONFIG_FILE = "#{ENV["CI_PROJECT_DIR"]}/.gitlab/changelog_config.yml"
44
+ DEFAULT_CHANGELOG_CATEGORIES = %w[
45
+ added
46
+ fixed
47
+ changed
48
+ deprecated
49
+ removed
50
+ security
51
+ performance
52
+ other
53
+ ].freeze
51
54
 
52
55
  class ChangelogCheckResult
53
56
  attr_reader :errors, :warnings, :markdowns, :messages
@@ -90,6 +93,24 @@ module Danger
90
93
  end
91
94
  end
92
95
 
96
+ class CommitWrapper
97
+ extend Forwardable
98
+
99
+ attr_reader :category, :trailer_key
100
+
101
+ def initialize(commit, trailer_key, category)
102
+ @commit = commit
103
+ @trailer_key = trailer_key
104
+ @category = category
105
+ end
106
+
107
+ delegate %i[message sha] => :@commit
108
+ end
109
+
110
+ def categories
111
+ valid_changelog_commits.map(&:category)
112
+ end
113
+
93
114
  # rubocop:disable Style/SignalException
94
115
  def check!
95
116
  if git.modified_files.include?("CHANGELOG.md")
@@ -126,17 +147,13 @@ module Danger
126
147
  end
127
148
 
128
149
  def check_changelog_trailer(commit)
129
- trailer = commit.message.match(CHANGELOG_TRAILER_REGEX)
130
- name = trailer[:name]
131
- category = trailer[:category]
132
-
133
- unless name == "Changelog"
134
- return ChangelogCheckResult.error("The changelog trailer for commit #{commit.sha} must be `Changelog` (starting with a capital C), not `#{name}`")
150
+ unless commit.trailer_key == "Changelog"
151
+ return ChangelogCheckResult.error("The changelog trailer for commit #{commit.sha} must be `Changelog` (starting with a capital C), not `#{commit.trailer_key}`")
135
152
  end
136
153
 
137
- return ChangelogCheckResult.empty if self.class.categories.include?(category)
154
+ return ChangelogCheckResult.empty if valid_categories.include?(commit.category)
138
155
 
139
- ChangelogCheckResult.error("Commit #{commit.sha} uses an invalid changelog category: #{category}")
156
+ ChangelogCheckResult.error("Commit #{commit.sha} uses an invalid changelog category: #{commit.category}")
140
157
  end
141
158
 
142
159
  def check_changelog_path
@@ -180,16 +197,16 @@ module Danger
180
197
  end
181
198
 
182
199
  def changelog_commits
183
- git.commits.select do |commit|
184
- commit.message.match?(CHANGELOG_TRAILER_REGEX)
200
+ git.commits.each_with_object([]) do |commit, memo|
201
+ trailer = commit.message.match(CHANGELOG_TRAILER_REGEX)
202
+
203
+ memo << CommitWrapper.new(commit, trailer[:name], trailer[:category]) if trailer
185
204
  end
186
205
  end
187
206
 
188
207
  def valid_changelog_commits
189
208
  changelog_commits.select do |commit|
190
- trailer = commit.message.match(CHANGELOG_TRAILER_REGEX)
191
-
192
- self.class.categories.include?(trailer[:category])
209
+ valid_categories.include?(commit.message.match(CHANGELOG_TRAILER_REGEX)[:category])
193
210
  end
194
211
  end
195
212
 
@@ -219,6 +236,28 @@ module Danger
219
236
 
220
237
  private
221
238
 
239
+ def valid_categories
240
+ return @categories if defined?(@categories)
241
+
242
+ @categories = if File.exist?(CHANGELOG_CONFIG_FILE)
243
+ begin
244
+ YAML
245
+ .load_file(CHANGELOG_CONFIG_FILE)
246
+ .fetch("categories")
247
+ .keys
248
+ .freeze
249
+ rescue Psych::SyntaxError, Psych::DisallowedClass => ex
250
+ puts "#{CHANGELOG_CONFIG_FILE} doesn't seem to be a valid YAML file:\n#{ex.message}\nFallbacking to the default categories: #{DEFAULT_CHANGELOG_CATEGORIES}"
251
+ DEFAULT_CHANGELOG_CATEGORIES
252
+ rescue => ex
253
+ puts "Received an unexpected failure while trying to fetch categories at #{CHANGELOG_CONFIG_FILE}:\n#{ex.message}\nFallbacking to the default categories: #{DEFAULT_CHANGELOG_CATEGORIES}"
254
+ DEFAULT_CHANGELOG_CATEGORIES
255
+ end
256
+ else
257
+ DEFAULT_CHANGELOG_CATEGORIES
258
+ end
259
+ end
260
+
222
261
  def read_file(path)
223
262
  File.read(path)
224
263
  end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../../gitlab/dangerfiles/type_label_guesser"
4
+
5
+ if respond_to?(:changelog) && !helper.has_scoped_label_with_scope?("type")
6
+ type_label_guesser = Gitlab::Dangerfiles::TypeLabelGuesser.new
7
+ helper.labels_to_add.merge(type_label_guesser.labels_from_changelog_categories(changelog.categories))
8
+ end
9
+
3
10
  unless helper.has_scoped_label_with_scope?("type")
4
11
  warn "Please add a [merge request type](https://about.gitlab.com/handbook/engineering/metrics/#work-type-classification) to this merge request."
5
12
  end
@@ -1,4 +1,4 @@
1
- # This rule is the last one since we want to add all the labels to add at once,
1
+ # This rule is the (almost) last one since we want to add all the labels to add at once,
2
2
  # so we let other rules adding to `helper.labels_to_add` before actually adding them via the API here.
3
3
 
4
4
  # Don't try to post anything locally.
@@ -13,9 +13,3 @@ rescue Gitlab::Error::Forbidden
13
13
  end
14
14
 
15
15
  post_labels(helper.labels_to_add) if helper.labels_to_add.any?
16
-
17
- anything_to_post = status_report.values.any?(&:any?)
18
-
19
- if anything_to_post
20
- markdown("**If needed, you can retry the [`danger-review` job](#{ENV["CI_JOB_URL"]}) that generated this comment.**")
21
- end
@@ -0,0 +1,10 @@
1
+ # This rule is the last one since we only want to post the "retry" message if there are anything to be posted.
2
+
3
+ # There's no job to retry locally.
4
+ return unless helper.ci?
5
+
6
+ anything_to_post = status_report.values.any?(&:any?)
7
+
8
+ if anything_to_post
9
+ markdown("**If needed, you can retry the [🔁 `danger-review` job](#{ENV['CI_JOB_URL']}) that generated this comment.**")
10
+ end
@@ -115,11 +115,15 @@ module Gitlab
115
115
  end
116
116
 
117
117
  def files_changed
118
- commit.diff_parent.stats[:total][:files]
118
+ # In some cases, the commits cannot be found so it's better to just move one
119
+ # See https://gitlab.com/gitlab-org/gitlab/-/issues/227814.
120
+ commit.diff_parent.size rescue 0
119
121
  end
120
122
 
121
123
  def lines_changed
122
- commit.diff_parent.stats[:total][:lines]
124
+ # In some cases, the commits cannot be found so it's better to just move one
125
+ # See https://gitlab.com/gitlab-org/gitlab/-/issues/227814.
126
+ commit.diff_parent.lines rescue 0
123
127
  end
124
128
 
125
129
  def many_changes?
@@ -63,6 +63,6 @@ RSpec.shared_context "with dangerfile" do
63
63
 
64
64
  before do
65
65
  allow(dangerfile).to receive(:git).and_return(fake_git)
66
- allow(dangerfile.helper).to receive(:changes).and_return(changes)
66
+ allow(dangerfile.helper).to receive(:changes).and_return(changes) if dangerfile.respond_to?(:helper)
67
67
  end
68
68
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ module Dangerfiles
5
+ class TypeLabelGuesser
6
+ CHANGELOG_CATEGORY_TO_TYPE_LABEL = {
7
+ fixed: %w[type::bug],
8
+ security: %w[type::bug bug::vulnerability],
9
+ performance: %w[type::bug bug::performance],
10
+ added: %w[type::feature feature::addition],
11
+ deprecated: %w[type::feature feature::removal],
12
+ removed: %w[type::feature feature::removal],
13
+ }.freeze
14
+
15
+ def labels_from_changelog_categories(categories)
16
+ categories = categories.map(&:to_sym) & CHANGELOG_CATEGORY_TO_TYPE_LABEL.keys
17
+ return [] unless categories.one?
18
+
19
+ CHANGELOG_CATEGORY_TO_TYPE_LABEL.fetch(categories.first.to_sym, [])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  module Gitlab
2
2
  module Dangerfiles
3
- VERSION = "2.9.1"
3
+ VERSION = "2.10.0"
4
4
  end
5
5
  end
@@ -17,6 +17,7 @@ module Gitlab
17
17
  simple_roulette
18
18
  type_label
19
19
  z_add_labels
20
+ z_retry_link
20
21
  ].freeze
21
22
 
22
23
  # Utility method to construct a [Gitlab::Dangerfiles::Engine] instance,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-dangerfiles
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.1
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitLab
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-23 00:00:00.000000000 Z
11
+ date: 2022-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-gitlab
@@ -158,6 +158,7 @@ files:
158
158
  - lib/danger/rules/simple_roulette/Dangerfile
159
159
  - lib/danger/rules/type_label/Dangerfile
160
160
  - lib/danger/rules/z_add_labels/Dangerfile
161
+ - lib/danger/rules/z_retry_link/Dangerfile
161
162
  - lib/gitlab-dangerfiles.rb
162
163
  - lib/gitlab/Dangerfile
163
164
  - lib/gitlab/dangerfiles.rb
@@ -170,6 +171,7 @@ files:
170
171
  - lib/gitlab/dangerfiles/spec_helper.rb
171
172
  - lib/gitlab/dangerfiles/teammate.rb
172
173
  - lib/gitlab/dangerfiles/title_linting.rb
174
+ - lib/gitlab/dangerfiles/type_label_guesser.rb
173
175
  - lib/gitlab/dangerfiles/version.rb
174
176
  - lib/gitlab/dangerfiles/weightage.rb
175
177
  - lib/gitlab/dangerfiles/weightage/maintainers.rb