gitlab-dangerfiles 2.9.2 → 2.10.1

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: 18ad65769d07e0c6739e489f3c8edf8d008a62fc25c4789476360f073d5bf3b8
4
- data.tar.gz: dd5598930037ead15d2f11bea7a0138ea374b143310adcd258da12569dcd0a21
3
+ metadata.gz: fb3512fc2c750751c44df16f13e6d8d7c47b6a87f77030852b278725503d3994
4
+ data.tar.gz: b611380c2b6a8f9cd4372e6b63b7b6c13a125373551a706b9e4e11cf552bc270
5
5
  SHA512:
6
- metadata.gz: e8c68d332a63e4c15b41e17024b5dff040db820c1cfb12929adc88daf8a1624dd1bfe76444a6423134cf301f89faad3b59ba1aa80c859342a80395c472669b72
7
- data.tar.gz: d471bed2793b86906d7f8460318c21bf2878d8c2dd4bdf381c925d5ad4d70c0bc36f5b40c63c6c17051cf07866611793baa464eab47756d227066a6f79cc8b2c
6
+ metadata.gz: ba7680e6aecd1c1231118033e95cbed567a97f638467d2ce0a131ac5def26c0746e19c8117df0629f1c61a3b14706e56bde80945fe3ff3617f0d0a5b87d5e14f
7
+ data.tar.gz: 5a1df455e26146be92687f2e416f095f6ed9f7176576d13afce1d4a2d38ad0e928b7051b091323a0dc85e5f6a7b74e4fabf09b1ec88b447d089dea9ab9260844
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.
@@ -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?
@@ -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.2"
3
+ VERSION = "2.10.1"
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.2
4
+ version: 2.10.1
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-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