Class: Gitlab::Triage::Validators::PolicyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/triage/validators/policy_validator.rb

Overview

Validates policy definitions at load time, before any resource is fetched or acted on. Triage typically runs unattended and scheduled, so an unsupported policy must be rejected up front rather than raising partway through a run (which could leave a rule half-applied and fail where nobody sees it).

Constant Summary collapse

UnsupportedActionError =
Class.new(StandardError)
COMMENT_NOT_SUPPORTED_MESSAGE =
'Commenting on work_items is not yet supported by the Work Items REST API ' \
'(blocked on https://gitlab.com/gitlab-org/gitlab/-/work_items/604028). ' \
'Use the issues policy type for comment actions.'
SUPPORTED_WORK_ITEM_ACTIONS =

Action keys Action.actions_for dispatches for work_items policies. Unlike the legacy resource types, work_items have no quick-actions comment fallback for unrecognized keys, so anything outside this list would silently do nothing - reject it up front instead.

%i[labels assignees summarize comment_on_summary].freeze

Instance Method Summary collapse

Constructor Details

#initialize(resource_rules) ⇒ PolicyValidator

Returns a new instance of PolicyValidator.



25
26
27
# File 'lib/gitlab/triage/validators/policy_validator.rb', line 25

def initialize(resource_rules)
  @resource_rules = resource_rules || {}
end

Instance Method Details

#action_sets_from(policy_definition) ⇒ Object (private)

Every place a policy definition can carry actions: top-level rules, rules nested in summaries, and the summaries' own actions.



54
55
56
57
58
59
60
61
62
# File 'lib/gitlab/triage/validators/policy_validator.rb', line 54

def action_sets_from(policy_definition)
  policy_definition ||= {}

  summaries = Array(policy_definition[:summaries])
  summary_rules = summaries.flat_map { |summary| Array(summary[:rules]) }

  (Array(policy_definition[:rules]) + summary_rules + summaries)
    .map { |definition| definition[:actions] || {} }
end

#validate!Object



29
30
31
32
33
34
35
# File 'lib/gitlab/triage/validators/policy_validator.rb', line 29

def validate!
  @resource_rules.each do |resource_type, policy_definition|
    next unless resource_type.to_s == 'work_items'

    validate_work_items_actions!(policy_definition)
  end
end

#validate_work_items_actions!(policy_definition) ⇒ Object (private)



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/triage/validators/policy_validator.rb', line 39

def validate_work_items_actions!(policy_definition)
  action_sets_from(policy_definition).each do |actions|
    raise UnsupportedActionError, COMMENT_NOT_SUPPORTED_MESSAGE if actions.key?(:comment)

    unsupported = actions.keys.map(&:to_sym) - SUPPORTED_WORK_ITEM_ACTIONS
    next if unsupported.none?

    raise UnsupportedActionError,
      "Unsupported action(s) for work_items policies: #{unsupported.join(', ')}. " \
        "Supported actions: #{SUPPORTED_WORK_ITEM_ACTIONS.join(', ')}."
  end
end