gitlab-dangerfiles 2.10.2 → 3.1.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: ce597e38c3466f9c039d8b7938c2eb3e8fcf01142608211636f6106310c793ec
4
- data.tar.gz: f8d79cb8a4019d70cb0cb206386b219ae4e37074694f26f39f35f8e8a93523ac
3
+ metadata.gz: f4d0b46a9dcbc098c76a6acb7eda61791470b8a8914e47650532f6fe44158d0f
4
+ data.tar.gz: f9aa9d41af6ae110acc46de51c9f19457555567ef520aa5b65d91e5fc85eb916
5
5
  SHA512:
6
- metadata.gz: 00b61a080b6872088e2ded726bd0c4af98935c07177fabeaa082c2f09bfc8542cf8d0c69ed01d7384d71726062ede02642cf7e52a19c46f6e8b6c7e4e7eb79b3
7
- data.tar.gz: 32acb334ddc1b186b91efd0d550bc303613d37bfea89a7ae38ea3ab3f29937b72b9b81e88ed2f7a80c96181cc6ec110c836a9368667695073b2e77b2268ee5af
6
+ metadata.gz: 778edd2df54298a54b37218e6479b6566016248aed13f68a58d1fd43df6ac3bf4e0aee4d47494e0771899be984e5b0cfa667cfb99033f0c9cefde5c4cbe29aa9
7
+ data.tar.gz: 740003a2197153bcfb8431329e6eb3653707f34276a3cd34afb9cba1b60e6e8b08fe6884a46a14abb70869b957a2f5510ddc765bf5018885021fba571f1e0890
data/README.md CHANGED
@@ -67,6 +67,8 @@ Gitlab::Dangerfiles.for_project(self, 'my-project') do |dangerfiles|
67
67
  end
68
68
  ```
69
69
 
70
+ Note that your custom plugins and rules (unless you exclude them with `except`) are automatically imported by the gem.
71
+
70
72
  ### Plugins
71
73
 
72
74
  Danger plugins are located under `lib/danger/plugins`.
@@ -85,8 +87,10 @@ Alternatively, you can also get/set configuration on the engine directly via `Gi
85
87
 
86
88
  #### Available general configurations
87
89
 
90
+ - `project_root`: The project root path. You shouldn't have to override it.
88
91
  - `project_name`: The project name. Currently used by the Roulette plugin to fetch relevant
89
92
  reviewers/maintainers based on the project name. Default to `ENV["CI_PROJECT_NAME"]`.
93
+ - `ci_only_rules`: A list of rules that cannot run locally.
90
94
  - `files_to_category`: A hash of the form `{ filename_regex => categories, [filename_regex, changes_regex] => categories }`.
91
95
  `filename_regex` is the regex pattern to match file names. `changes_regex` is the regex pattern to
92
96
  match changed lines in files that match `filename_regex`. Used in `helper.changes_by_category`, `helper.changes`, and `helper.categories_for_file`.
@@ -131,7 +135,9 @@ project. To use it in your project, perform the following steps:
131
135
 
132
136
  #### `type_label`
133
137
 
134
- This rule ensures the merge request has a proper [type label](https://about.gitlab.com/handbook/engineering/metrics/#work-type-classification) set.
138
+ This rule warns when the merge request is missing a [type label](https://about.gitlab.com/handbook/engineering/metrics/#work-type-classification).
139
+
140
+ If the `DANGER_ERROR_WHEN_TYPE_LABEL_IS_MISSING` environment variable evaluates to `true`, Danger will error when a type label is missing.
135
141
 
136
142
  If the `changelog` plugin is available, it also tries to infer a type label from the `Changelog` trailer of the MR.
137
143
 
@@ -166,6 +172,18 @@ include:
166
172
 
167
173
  See a [real world example](https://gitlab.com/gitlab-org/ruby/gems/gitlab-styles/-/merge_requests/105).
168
174
 
175
+ ## Rake tasks
176
+
177
+ You can import this gem's Rake tasks by adding the following to your project's `Rakefile`:
178
+
179
+ ```ruby
180
+ require 'gitlab-dangerfiles'
181
+
182
+ Gitlab::Dangerfiles.load_tasks
183
+ ```
184
+
185
+ That will add the `danger_local` Rake task that allows to run Danger locally.
186
+
169
187
  ## Documentation
170
188
 
171
189
  Latest documentation can be found at <https://www.rubydoc.info/gems/gitlab-dangerfiles>.
@@ -27,8 +27,9 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
+ spec.add_dependency "rake", ">= 0"
30
31
  spec.add_dependency "danger-gitlab", ">= 8.0.0"
31
- spec.add_dependency "danger", ">= 8.3.1"
32
+ spec.add_dependency "danger", ">= 8.4.5"
32
33
 
33
34
  spec.add_development_dependency "rspec", "~> 3.0"
34
35
  spec.add_development_dependency "rspec-parameterized"
@@ -4,9 +4,9 @@ module Danger
4
4
  # Contains method to check the presense and validity of changelogs.
5
5
  class Changelog < Danger::Plugin
6
6
  NO_CHANGELOG_LABELS = [
7
- "type::tooling",
8
- "tooling::pipelines",
9
- "tooling::workflow",
7
+ "maintenance::refactor",
8
+ "maintenance::pipelines",
9
+ "maintenance::workflow",
10
10
  "ci-build",
11
11
  "meta",
12
12
  ].freeze
@@ -40,7 +40,6 @@ 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
- CHANGELOG_CONFIG_FILE = "#{ENV["CI_PROJECT_DIR"]}/.gitlab/changelog_config.yml"
44
43
  DEFAULT_CHANGELOG_CATEGORIES = %w[
45
44
  added
46
45
  fixed
@@ -236,21 +235,25 @@ module Danger
236
235
 
237
236
  private
238
237
 
238
+ def changelog_config_file
239
+ @changelog_config_file ||= File.join(helper.config.project_root, ".gitlab/changelog_config.yml")
240
+ end
241
+
239
242
  def valid_categories
240
243
  return @categories if defined?(@categories)
241
244
 
242
- @categories = if File.exist?(CHANGELOG_CONFIG_FILE)
245
+ @categories = if File.exist?(changelog_config_file)
243
246
  begin
244
247
  YAML
245
- .load_file(CHANGELOG_CONFIG_FILE)
248
+ .load_file(changelog_config_file)
246
249
  .fetch("categories")
247
250
  .keys
248
251
  .freeze
249
252
  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}"
253
+ 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
254
  DEFAULT_CHANGELOG_CATEGORIES
252
255
  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}"
256
+ 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
257
  DEFAULT_CHANGELOG_CATEGORIES
255
258
  end
256
259
  else
@@ -20,7 +20,7 @@ module Danger
20
20
  test: "~test ~Quality for `spec/features/*`",
21
21
  # Deprecated as of 2.3.0 in favor of tooling
22
22
  engineering_productivity: '~"Engineering Productivity" for CI, Danger',
23
- tooling: '~"type::tooling" for CI, Danger',
23
+ tooling: '~"maintenance::workflow" / ~"maintenance::pipelines" for CI, Danger',
24
24
  ci_template: '~"ci::templates"',
25
25
  product_intelligence: '~"product intelligence"',
26
26
  integrations_be: '~"group::integrations" (backend)',
@@ -142,7 +142,7 @@ module Danger
142
142
  end
143
143
 
144
144
  def release_automation?
145
- gitlab_helper&.mr_author == RELEASE_TOOLS_BOT
145
+ mr_author == RELEASE_TOOLS_BOT
146
146
  end
147
147
 
148
148
  # @param items [Array<String>] An array of items to transform into a bullet list.
@@ -97,7 +97,7 @@ def lint_commits(commits)
97
97
  warn_or_fail_commits(multi_line_commit_linter)
98
98
  commit_linters.delete(multi_line_commit_linter) # Don't show an error (here) and a warning (below)
99
99
  elsif helper.ci? # We don't have access to the MR title locally
100
- title_linter = lint_mr_title(gitlab.mr_json['title'])
100
+ title_linter = lint_mr_title(helper.mr_title)
101
101
  if title_linter.failed?
102
102
  warn_or_fail_commits(title_linter)
103
103
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ TYPE_LABEL_MISSING_MESSAGE = "Please add a [merge request type](https://about.gitlab.com/handbook/engineering/metrics/#work-type-classification) to this merge request."
4
+
3
5
  require_relative "../../../gitlab/dangerfiles/type_label_guesser"
4
6
 
5
7
  if respond_to?(:changelog) && !helper.has_scoped_label_with_scope?("type")
@@ -7,6 +9,10 @@ if respond_to?(:changelog) && !helper.has_scoped_label_with_scope?("type")
7
9
  helper.labels_to_add.concat(type_label_guesser.labels_from_changelog_categories(changelog.categories))
8
10
  end
9
11
 
10
- unless helper.has_scoped_label_with_scope?("type")
11
- warn "Please add a [merge request type](https://about.gitlab.com/handbook/engineering/metrics/#work-type-classification) to this merge request."
12
+ return if helper.has_scoped_label_with_scope?("type")
13
+
14
+ if ENV["DANGER_ERROR_WHEN_TYPE_LABEL_IS_MISSING"]
15
+ fail TYPE_LABEL_MISSING_MESSAGE
16
+ else
17
+ warn TYPE_LABEL_MISSING_MESSAGE
12
18
  end
@@ -3,10 +3,18 @@
3
3
  module Gitlab
4
4
  module Dangerfiles
5
5
  class Config
6
+ # @!attribute project_root
7
+ # @return [String] the project root folder path.
8
+ attr_accessor :project_root
9
+
6
10
  # @!attribute project_name
7
11
  # @return [String] the project name. Currently used by the Roulette plugin to fetch relevant reviewers/maintainers based on the project name. Default to +ENV["CI_PROJECT_NAME"]+.
8
12
  attr_accessor :project_name
9
13
 
14
+ # @!attribute ci_only_rules
15
+ # @return [Array<String>] rules that cannot be run locally.
16
+ attr_accessor :ci_only_rules
17
+
10
18
  # @!attribute files_to_category
11
19
  # @return [{Regexp => Array<Symbol>}, {Array<Regexp> => Array<Symbol>}] A hash of the form +{ filename_regex => categories, [filename_regex, changes_regex] => categories }+.
12
20
  # +filename_regex+ is the regex pattern to match file names. +changes_regex+ is the regex pattern to
@@ -26,7 +34,9 @@ module Gitlab
26
34
 
27
35
  def initialize
28
36
  @files_to_category = {}
37
+ @project_root = nil
29
38
  @project_name = ENV["CI_PROJECT_NAME"]
39
+ @ci_only_rules = []
30
40
  @code_size_thresholds = DEFAULT_CHANGES_SIZE_THRESHOLDS
31
41
  @max_commits_count = DEFAULT_COMMIT_MESSAGES_MAX_COMMITS_COUNT
32
42
  end
@@ -24,7 +24,9 @@ module DangerSpecHelper
24
24
  # A stubbed out Dangerfile for use in tests
25
25
  def self.testing_dangerfile
26
26
  env = Danger::EnvironmentManager.new(testing_env)
27
- Danger::Dangerfile.new(env, testing_ui)
27
+ Danger::Dangerfile.new(env, testing_ui).tap do |dangerfile|
28
+ dangerfile.defined_in_file = Dir.pwd
29
+ end
28
30
  end
29
31
 
30
32
  def self.fake_danger
@@ -0,0 +1,15 @@
1
+ require "rake"
2
+
3
+ module Gitlab
4
+ module Dangerfiles
5
+ module TaskLoader
6
+ module_function
7
+
8
+ TASKS_DIR = File.expand_path("tasks", __dir__)
9
+
10
+ def load_tasks
11
+ Rake.application.add_import(*Dir.glob(File.join(TASKS_DIR, "*.rake")))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ desc "Run local Danger rules"
4
+ task :danger_local do
5
+ require "open3"
6
+
7
+ stdout, stderr, status = Open3.capture3({}, *%w{bundle exec danger dry_run})
8
+
9
+ puts("#{stdout}#{stderr}")
10
+
11
+ exit(status.exitstatus.to_i)
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Gitlab
2
2
  module Dangerfiles
3
- VERSION = "2.10.2"
3
+ VERSION = "3.1.0"
4
4
  end
5
5
  end
@@ -1,18 +1,9 @@
1
1
  require "gitlab/dangerfiles/version"
2
+ require "gitlab/dangerfiles/task_loader"
2
3
 
3
4
  module Gitlab
4
5
  module Dangerfiles
5
6
  RULES_DIR = File.expand_path("../danger/rules", __dir__)
6
- EXISTING_RULES = Dir.glob(File.join(RULES_DIR, "*")).each_with_object([]) do |path, memo|
7
- if File.directory?(path)
8
- memo << File.basename(path)
9
- end
10
- end
11
- LOCAL_RULES = %w[
12
- changelog
13
- changes_size
14
- commit_messages
15
- ].freeze
16
7
  CI_ONLY_RULES = %w[
17
8
  simple_roulette
18
9
  type_label
@@ -20,6 +11,10 @@ module Gitlab
20
11
  z_retry_link
21
12
  ].freeze
22
13
 
14
+ def self.load_tasks
15
+ TaskLoader.load_tasks
16
+ end
17
+
23
18
  # Utility method to construct a [Gitlab::Dangerfiles::Engine] instance,
24
19
  # which is yielded to the given block.
25
20
  #
@@ -29,6 +24,7 @@ module Gitlab
29
24
  # @return [Gitlab::Dangerfiles::Engine]
30
25
  def self.for_project(dangerfile, project_name = nil)
31
26
  Engine.new(dangerfile).tap do |engine|
27
+ engine.config.project_root = Pathname.new(File.dirname(dangerfile.defined_in_file))
32
28
  engine.config.project_name = project_name if project_name
33
29
 
34
30
  yield engine
@@ -60,15 +56,15 @@ module Gitlab
60
56
  # end
61
57
  def import_plugins
62
58
  danger_plugin.import_plugin(File.expand_path("../danger/plugins/*.rb", __dir__))
59
+
60
+ Dir.glob(File.expand_path("danger/plugins/*.rb", config.project_root)).sort.each do |path|
61
+ puts "Importing plugin at #{path}" if dangerfile.verbose
62
+ danger_plugin.import_plugin(path)
63
+ end
63
64
  end
64
65
 
65
66
  # Import available Dangerfiles.
66
67
  #
67
- # @deprecated
68
- # @param rules [Symbol, Array<String>] Can be either +:all+ (default) to import all rules,
69
- # or an array of rules.
70
- # Available rules are: +changes_size+.
71
- #
72
68
  # @param only [Symbol, Array<String>] An array of rules to import (defaults to all rules).
73
69
  # Available rules are: +changes_size+.
74
70
  #
@@ -87,14 +83,15 @@ module Gitlab
87
83
  # # Or import only a subset of rules, except a subset of rules
88
84
  # dangerfiles.import_dangerfiles(only: %w[changes_size], except: %w[commit_messages])
89
85
  # end
90
- def import_dangerfiles(rules: nil, only: nil, except: [])
91
- puts "The `:rules` parameter is deprecated in favor of `:only`." unless rules.nil?
86
+ def import_dangerfiles(only: nil, except: [])
87
+ return if helper_plugin.release_automation?
92
88
 
93
- only ||= EXISTING_RULES if rules == :all
94
- only ||= rules || EXISTING_RULES
89
+ rules = filtered_rules(only, except)
90
+ puts "Running rules: #{rules}\n" if dangerfile.verbose
95
91
 
96
- filtered_rules(only, except).each do |rule|
97
- danger_plugin.import_dangerfile(path: File.join(RULES_DIR, rule))
92
+ rules.each do |rule, path|
93
+ puts "Importing rule #{rule} at #{path}" if dangerfile.verbose
94
+ danger_plugin.import_dangerfile(path: path)
98
95
  end
99
96
  end
100
97
 
@@ -119,14 +116,43 @@ module Gitlab
119
116
 
120
117
  attr_reader :dangerfile
121
118
 
122
- def allowed_rules
123
- return LOCAL_RULES unless helper_plugin.respond_to?(:ci?)
119
+ def all_gem_rules
120
+ @all_gem_rules ||= Dir.glob(File.join(RULES_DIR, "*")).sort.each_with_object({}) do |path, memo|
121
+ rule_name = File.basename(path)
122
+ memo[rule_name] = path if File.directory?(path) && File.exist?(File.join(path, "Dangerfile"))
123
+ end
124
+ end
125
+
126
+ def custom_rules
127
+ @custom_rules ||= Dir.glob(File.expand_path("danger/*", config.project_root)).sort.each_with_object({}) do |path, memo|
128
+ rule_name = File.basename(path)
129
+ memo[rule_name] = path if File.directory?(path) && File.exist?(File.join(path, "Dangerfile"))
130
+ end
131
+ end
132
+
133
+ def all_rules
134
+ all_gem_rules.merge(custom_rules)
135
+ end
124
136
 
125
- helper_plugin.ci? ? LOCAL_RULES | CI_ONLY_RULES : LOCAL_RULES
137
+ def local_rules
138
+ ci_only_rules = CI_ONLY_RULES | config.ci_only_rules
139
+ all_rules.reject { |rule, _v| ci_only_rules.include?(rule) }
126
140
  end
127
141
 
128
- def filtered_rules(only, except)
129
- (Array(only).map(&:to_s) & EXISTING_RULES & allowed_rules) - except
142
+ def allowed_rules_based_on_context
143
+ helper_plugin.ci? ? all_rules : local_rules
144
+ end
145
+
146
+ def filtered_rules(only_rules, except_rules)
147
+ only_rules = Array(only_rules).compact.map(&:to_s)
148
+
149
+ rules = allowed_rules_based_on_context.reject { |rule, _v| except_rules.include?(rule) }
150
+
151
+ if only_rules.any?
152
+ rules.select! { |rule, _v| only_rules.include?(rule) }
153
+ end
154
+
155
+ rules
130
156
  end
131
157
 
132
158
  def danger_plugin
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-dangerfiles
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.2
4
+ version: 3.1.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-03-02 00:00:00.000000000 Z
11
+ date: 2022-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: danger-gitlab
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -30,14 +44,14 @@ dependencies:
30
44
  requirements:
31
45
  - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: 8.3.1
47
+ version: 8.4.5
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: 8.3.1
54
+ version: 8.4.5
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -169,6 +183,8 @@ files:
169
183
  - lib/gitlab/dangerfiles/emoji_checker.rb
170
184
  - lib/gitlab/dangerfiles/merge_request_linter.rb
171
185
  - lib/gitlab/dangerfiles/spec_helper.rb
186
+ - lib/gitlab/dangerfiles/task_loader.rb
187
+ - lib/gitlab/dangerfiles/tasks/main.rake
172
188
  - lib/gitlab/dangerfiles/teammate.rb
173
189
  - lib/gitlab/dangerfiles/title_linting.rb
174
190
  - lib/gitlab/dangerfiles/type_label_guesser.rb