Make roulette for CODEOWNERS configurable for sections
This was brought up in gitlab-org/gitlab#32870 (comment 1065142231)
We likely don't want to strictly require documentation approvals yet, but it would be nice if we start rolling documentation reviewers in roulette so that we can utilize the configurations in the CODEOWNERS.
The GitLab project can configure this like:
diff --git a/Dangerfile b/Dangerfile
index 280a73d432c8..33aba47ed866 100644
--- a/Dangerfile
+++ b/Dangerfile
@@ -13,6 +13,10 @@ Gitlab::Dangerfiles.for_project(self, project_name) do |gitlab_dangerfiles|
gitlab_dangerfiles.import_plugins
gitlab_dangerfiles.config.ci_only_rules = ProjectHelper::CI_ONLY_RULES
gitlab_dangerfiles.config.files_to_category = ProjectHelper::CATEGORIES
+ gitlab_dangerfiles.config.roulette_for_codeowners_sections.push(
+ 'Documentation Directories',
+ 'Documentation Pages'
+ )
gitlab_dangerfiles.import_dangerfiles(except: %w[simple_roulette])
end
And we can implement this like:
diff --git a/lib/danger/plugins/roulette.rb b/lib/danger/plugins/roulette.rb
index dfc3ac2..369ef59 100644
--- a/lib/danger/plugins/roulette.rb
+++ b/lib/danger/plugins/roulette.rb
@@ -98,7 +98,7 @@ module Danger
spins
end
- def required_approvals
+ def approvals_from_codeowners
approval_rules = helper.mr_approval_state["rules"]
return [] unless approval_rules
@@ -106,13 +106,21 @@ module Danger
required_approval_rules = unique_approval_rules(approval_rules)
required_approval_rules.filter_map do |rule|
rule["rule_type"] == "code_owner" &&
- rule["approvals_required"] > 0 &&
+ spin_for_approval_rule?(rule) &&
Approval.from_approval_rule(rule, spin_for_approver(rule))
end
end
+ # Backward compatibility
+ alias_method :required_approvals, :approvals_from_codeowners
+
private
+ def spin_for_approval_rule?(rule)
+ rule["approvals_required"] > 0 ||
+ helper.config.roulette_for_codeowners_sections.include?(rule["section"])
+ end
+
# Returns an array containing all unique approval rules, based on on the section and eligible_approvers of the rules
#
# @param [Array<Hash>] approval rules
diff --git a/lib/gitlab/dangerfiles/config.rb b/lib/gitlab/dangerfiles/config.rb
index aa9541c..9f1e43d 100644
--- a/lib/gitlab/dangerfiles/config.rb
+++ b/lib/gitlab/dangerfiles/config.rb
@@ -21,6 +21,8 @@ module Gitlab
# match changed lines in files that match +filename_regex+. Used in `helper.changes_by_category`, `helper.changes`, and `helper.categories_for_file`.
attr_accessor :files_to_category
+ attr_accessor :roulette_for_codeowners_sections
+
# @!attribute code_size_thresholds
# @return [{ high: Integer, medium: Integer }] a hash of the form +{ high: 42, medium: 12 }+ where +:high+ is the lines changed threshold which triggers an error, and +:medium+ is the lines changed threshold which triggers a warning. Also, see +DEFAULT_CHANGES_SIZE_THRESHOLDS+ for the format of the hash.
attr_accessor :code_size_thresholds
@@ -37,6 +39,7 @@ module Gitlab
@project_root = nil
@project_name = ENV["CI_PROJECT_NAME"]
@ci_only_rules = []
+ @roulette_for_codeowners_sections = []
@code_size_thresholds = DEFAULT_CHANGES_SIZE_THRESHOLDS
@max_commits_count = DEFAULT_COMMIT_MESSAGES_MAX_COMMITS_COUNT
end
Edited by Lin Jen-Shin