lib/gitlab/dangerfiles.rb in gitlab-dangerfiles-2.11.0 vs lib/gitlab/dangerfiles.rb in gitlab-dangerfiles-3.0.0

- old
+ new

@@ -1,36 +1,32 @@ require "gitlab/dangerfiles/version" +require "gitlab/dangerfiles/task_loader" module Gitlab module Dangerfiles RULES_DIR = File.expand_path("../danger/rules", __dir__) - EXISTING_RULES = Dir.glob(File.join(RULES_DIR, "*")).each_with_object([]) do |path, memo| - if File.directory?(path) - memo << File.basename(path) - end - end - LOCAL_RULES = %w[ - changelog - changes_size - commit_messages - ].freeze CI_ONLY_RULES = %w[ simple_roulette type_label z_add_labels z_retry_link ].freeze + def self.load_tasks + TaskLoader.load_tasks + end + # Utility method to construct a [Gitlab::Dangerfiles::Engine] instance, # which is yielded to the given block. # # @param dangerfile [Danger::Dangerfile] A +Danger::Dangerfile+ object. # @param project_name An option string to set the project name. Defaults to +ENV['CI_PROJECT_NAME']+. # # @return [Gitlab::Dangerfiles::Engine] def self.for_project(dangerfile, project_name = nil) Engine.new(dangerfile).tap do |engine| + engine.config.project_root = Pathname.new(File.dirname(dangerfile.defined_in_file)) engine.config.project_name = project_name if project_name yield engine end end @@ -58,19 +54,19 @@ # Gitlab::Dangerfiles.for_project(self) do |dangerfiles| # dangerfiles.import_plugins # end def import_plugins danger_plugin.import_plugin(File.expand_path("../danger/plugins/*.rb", __dir__)) + + Dir.glob(File.expand_path("danger/plugins/*.rb", config.project_root)).sort.each do |path| + puts "Importing plugin at #{path}" if dangerfile.verbose + danger_plugin.import_plugin(path) + end end # Import available Dangerfiles. # - # @deprecated - # @param rules [Symbol, Array<String>] Can be either +:all+ (default) to import all rules, - # or an array of rules. - # Available rules are: +changes_size+. - # # @param only [Symbol, Array<String>] An array of rules to import (defaults to all rules). # Available rules are: +changes_size+. # # @param except [Symbol, Array<String>] An array of rules to not import (defaults to []). # Available rules are: +changes_size+. @@ -85,18 +81,19 @@ # # Or import all rules except a subset of rules # dangerfiles.import_dangerfiles(except: %w[commit_messages]) # # Or import only a subset of rules, except a subset of rules # dangerfiles.import_dangerfiles(only: %w[changes_size], except: %w[commit_messages]) # end - def import_dangerfiles(rules: nil, only: nil, except: []) - puts "The `:rules` parameter is deprecated in favor of `:only`." unless rules.nil? + def import_dangerfiles(only: nil, except: []) + return if helper_plugin.release_automation? - only ||= EXISTING_RULES if rules == :all - only ||= rules || EXISTING_RULES + rules = filtered_rules(only, except) + puts "Running rules: #{rules}\n" if dangerfile.verbose - filtered_rules(only, except).each do |rule| - danger_plugin.import_dangerfile(path: File.join(RULES_DIR, rule)) + rules.each do |rule, path| + puts "Importing rule #{rule} at #{path}" if dangerfile.verbose + danger_plugin.import_dangerfile(path: path) end end # Proxy method to +helper_plugin.config+. def config @@ -117,17 +114,46 @@ private attr_reader :dangerfile - def allowed_rules - return LOCAL_RULES unless helper_plugin.respond_to?(:ci?) + def all_gem_rules + @all_gem_rules ||= Dir.glob(File.join(RULES_DIR, "*")).sort.each_with_object({}) do |path, memo| + rule_name = File.basename(path) + memo[rule_name] = path if File.directory?(path) && File.exist?(File.join(path, "Dangerfile")) + end + end - helper_plugin.ci? ? LOCAL_RULES | CI_ONLY_RULES : LOCAL_RULES + def custom_rules + @custom_rules ||= Dir.glob(File.expand_path("danger/*", config.project_root)).sort.each_with_object({}) do |path, memo| + rule_name = File.basename(path) + memo[rule_name] = path if File.directory?(path) && File.exist?(File.join(path, "Dangerfile")) + end end - def filtered_rules(only, except) - (Array(only).map(&:to_s) & EXISTING_RULES & allowed_rules) - except + def all_rules + all_gem_rules.merge(custom_rules) + end + + def local_rules + ci_only_rules = CI_ONLY_RULES | config.ci_only_rules + all_rules.reject { |rule, _v| ci_only_rules.include?(rule) } + end + + def allowed_rules_based_on_context + helper_plugin.ci? ? all_rules : local_rules + end + + def filtered_rules(only_rules, except_rules) + only_rules = Array(only_rules).compact.map(&:to_s) + + rules = allowed_rules_based_on_context.reject { |rule, _v| except_rules.include?(rule) } + + if only_rules.any? + rules.select! { |rule, _v| only_rules.include?(rule) } + end + + rules end def danger_plugin @danger_plugin ||= dangerfile.plugins[Danger::DangerfileDangerPlugin] end