gitlab-dangerfiles 2.9.3 → 2.10.2

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: 51f1b9ce0d37840b9810a3bc19cdf3c6e35ed8d14f7cf0495a52e1410468b34e
4
- data.tar.gz: 763783874f9e0e72b33408f2718d6151a7530526ab3498529ea40d2c02fa4f25
3
+ metadata.gz: ce597e38c3466f9c039d8b7938c2eb3e8fcf01142608211636f6106310c793ec
4
+ data.tar.gz: f8d79cb8a4019d70cb0cb206386b219ae4e37074694f26f39f35f8e8a93523ac
5
5
  SHA512:
6
- metadata.gz: b204d42cc13b8b06801f4da675617e63b56fba631b4122ed4140e0188ea5143d5ec07732b41c6581426d4a5e9e48939f2e260b9f3636afcaac1a96f2aaf78e29
7
- data.tar.gz: 6bc734af3d2148cbfb67e23e2cef84f7b36ffb7b82f82b8b9d9d2ca410f95a72d5f72b68dc4d0e59d4f8811df61a536e3282793a6c22f37058e1038e0bc0b6db
6
+ metadata.gz: 00b61a080b6872088e2ded726bd0c4af98935c07177fabeaa082c2f09bfc8542cf8d0c69ed01d7384d71726062ede02642cf7e52a19c46f6e8b6c7e4e7eb79b3
7
+ data.tar.gz: 32acb334ddc1b186b91efd0d550bc303613d37bfea89a7ae38ea3ab3f29937b72b9b81e88ed2f7a80c96181cc6ec110c836a9368667695073b2e77b2268ee5af
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020-2021 GitLab
3
+ Copyright (c) 2020-present GitLab
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -131,7 +131,17 @@ project. To use it in your project, perform the following steps:
131
131
 
132
132
  #### `type_label`
133
133
 
134
- This rule ensures the merge request has a proper [type label](https://about.gitlab.com/handbook/engineering/metrics/#work-type-classification) set..
134
+ This rule ensures the merge request has a proper [type label](https://about.gitlab.com/handbook/engineering/metrics/#work-type-classification) set.
135
+
136
+ If the `changelog` plugin is available, it also tries to infer a type label from the `Changelog` trailer of the MR.
137
+
138
+ #### `z_add_labels`
139
+
140
+ This rule adds labels set from other rules (via `helper.labels_to_add`), with a single API request.
141
+
142
+ #### `z_retry_link`
143
+
144
+ This rule adds a retry link to the job where Danger ran at the end of the Danger message, only if there's any other message to post.
135
145
 
136
146
  ### CI configuration
137
147
 
@@ -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
@@ -120,7 +120,7 @@ module Danger
120
120
  # +modified_files+ might contain paths that already have been renamed,
121
121
  # so we need to remove them from the list.
122
122
  def all_changed_files
123
- changes.files - changes.renamed_before.files
123
+ changes.files - changes.deleted.files - changes.renamed_before.files
124
124
  end
125
125
 
126
126
  # @param filename [String] A file name for which we want the diff.
@@ -282,7 +282,7 @@ module Danger
282
282
  def mr_labels
283
283
  return [] unless ci?
284
284
 
285
- (gitlab_helper.mr_labels + labels_to_add.to_a).uniq
285
+ (gitlab_helper.mr_labels + labels_to_add).uniq
286
286
  end
287
287
 
288
288
  # @return [String] +`git rev-parse --abbrev-ref HEAD`+ when not in the CI context, and the MR source branch otherwise.
@@ -409,9 +409,9 @@ module Danger
409
409
  # itself. Without this method, the first rule wouldn't know that the label would be applied and would ask
410
410
  # for it anyway.
411
411
  #
412
- # @return [Set<String>] the list of labels that Danger will add
412
+ # @return [Array<String>] the list of labels that Danger will add
413
413
  def labels_to_add
414
- @labels_to_add ||= Set.new
414
+ @labels_to_add ||= []
415
415
  end
416
416
 
417
417
  private
@@ -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.concat(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
@@ -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.3"
3
+ VERSION = "2.10.2"
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.3
4
+ version: 2.10.2
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-24 00:00:00.000000000 Z
11
+ date: 2022-03-02 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