gitlab-dangerfiles 2.11.0 → 3.0.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: 708f86af0c7d76e5df58d4ea85223461f39f1f7fb4248b3236d7baa92b626ccf
4
- data.tar.gz: 22f2ee0d777dbc7ab95956f5bf2191c03449bc170baf9c9bacbd1e1e4a32ed69
3
+ metadata.gz: 1bcd8631096b4c3a5c7c6df1c8a1c49fda0a4b4936c9f815996ca7702be99f3e
4
+ data.tar.gz: ce6a7cc2c2b69e49a262882eddba550d4245c578bfc235be594a27cd0d04d737
5
5
  SHA512:
6
- metadata.gz: ad25c4c671fcde850a9c1740fb4e8dc847e4593cff3a55b4ef502bcb902cf84d5ddb4c7a0e64063371d252624f506b47047d32d292efa6ebee92f6db948162c5
7
- data.tar.gz: '066975079aae0a2307c5b2a5734f4ff43d6556bd149c4dcdcac3586646e416c2abc1234ff3833359f24b4e5f19a1eece8af66c7c32b5b813df79cc5fb93bfbe6'
6
+ metadata.gz: 60b36a45528577103064018e02315f365f729c4842f77ef794001fe4697760f2baf60ecaf29a19c5a1c0be25c239832b5d93908a4d6ddb5df7dc22380968f317
7
+ data.tar.gz: f1591f53f037986f147f9549bfe98faeecee825c20d916e4370d9f76d562a18456fe8ac993e507987013bdaa52b95845776bac3c66fe6ab844d2f74b13084339
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`.
@@ -166,6 +170,18 @@ include:
166
170
 
167
171
  See a [real world example](https://gitlab.com/gitlab-org/ruby/gems/gitlab-styles/-/merge_requests/105).
168
172
 
173
+ ## Rake tasks
174
+
175
+ You can import this gem's Rake tasks by adding the following to your project's `Rakefile`:
176
+
177
+ ```ruby
178
+ require 'gitlab-dangerfiles'
179
+
180
+ Gitlab::Dangerfiles.load_tasks
181
+ ```
182
+
183
+ That will add the `danger_local` Rake task that allows to run Danger locally.
184
+
169
185
  ## Documentation
170
186
 
171
187
  Latest documentation can be found at <https://www.rubydoc.info/gems/gitlab-dangerfiles>.
@@ -27,6 +27,7 @@ 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
32
  spec.add_dependency "danger", ">= 8.4.5"
32
33
 
@@ -4,6 +4,7 @@ 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
+ "maintenance::refactor",
7
8
  "maintenance::pipelines",
8
9
  "maintenance::workflow",
9
10
  "ci-build",
@@ -39,7 +40,6 @@ module Danger
39
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).
40
41
  MSG
41
42
  }.freeze
42
- CHANGELOG_CONFIG_FILE = "#{ENV["CI_PROJECT_DIR"]}/.gitlab/changelog_config.yml"
43
43
  DEFAULT_CHANGELOG_CATEGORIES = %w[
44
44
  added
45
45
  fixed
@@ -235,21 +235,25 @@ module Danger
235
235
 
236
236
  private
237
237
 
238
+ def changelog_config_file
239
+ @changelog_config_file ||= File.join(helper.config.project_root, ".gitlab/changelog_config.yml")
240
+ end
241
+
238
242
  def valid_categories
239
243
  return @categories if defined?(@categories)
240
244
 
241
- @categories = if File.exist?(CHANGELOG_CONFIG_FILE)
245
+ @categories = if File.exist?(changelog_config_file)
242
246
  begin
243
247
  YAML
244
- .load_file(CHANGELOG_CONFIG_FILE)
248
+ .load_file(changelog_config_file)
245
249
  .fetch("categories")
246
250
  .keys
247
251
  .freeze
248
252
  rescue Psych::SyntaxError, Psych::DisallowedClass => ex
249
- 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}"
250
254
  DEFAULT_CHANGELOG_CATEGORIES
251
255
  rescue => ex
252
- 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}"
253
257
  DEFAULT_CHANGELOG_CATEGORIES
254
258
  end
255
259
  else
@@ -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
@@ -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.11.0"
3
+ VERSION = "3.0.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.11.0
4
+ version: 3.0.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-15 00:00:00.000000000 Z
11
+ date: 2022-03-22 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
@@ -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