gitlab-triage 1.57.0 → 1.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../filters/work_item_date_conditions_filter'
4
+ require_relative '../milestone_wildcards'
5
+
3
6
  module Gitlab
4
7
  module Triage
5
8
  module Validators
@@ -10,16 +13,138 @@ module Gitlab
10
13
  # where nobody sees it).
11
14
  class PolicyValidator
12
15
  UnsupportedActionError = Class.new(StandardError)
16
+ UnsupportedConditionError = Class.new(StandardError)
17
+ InvalidConditionError = Class.new(StandardError)
13
18
  ConflictingActionError = Class.new(StandardError)
14
19
 
15
- COMMENT_NOT_SUPPORTED_MESSAGE =
16
- 'Commenting on work_items is not yet supported by the Work Items REST API ' \
17
- '(blocked on https://gitlab.com/gitlab-org/gitlab/-/work_items/604028). ' \
18
- 'Use the issues policy type for comment actions.'
20
+ # Action keys Action.actions_for dispatches for work_items policies
21
+ # (comment, mention, comment_type, comment_internal, and
22
+ # redact_confidential_resources are the comment action and its options).
23
+ # Anything outside this list would silently do nothing, so reject it up
24
+ # front.
25
+ SUPPORTED_WORK_ITEM_ACTIONS = %i[
26
+ labels remove_labels assignees state
27
+ comment mention comment_type comment_internal redact_confidential_resources
28
+ summarize comment_on_summary
29
+ ].freeze
30
+
31
+ # A summary's own actions run over the whole summary, not per item, so
32
+ # the per-item actions would crash there.
33
+ SUPPORTED_SUMMARY_ACTIONS = %i[summarize comment_on_summary].freeze
34
+
35
+ # A rule nested in a summary only feeds it: the engine reads its
36
+ # summarize action and runs nothing else.
37
+ SUPPORTED_SUMMARY_RULE_ACTIONS = %i[summarize].freeze
38
+
39
+ # Sent to the API as state_event. On issues, `status: close` does the same,
40
+ # but on work_items `status` is kept for the configurable work item status.
41
+ WORK_ITEM_STATE_EVENTS = %w[close reopen].freeze
42
+
43
+ # Sent to the Work Items REST API as query params by WorkItemsRestSource#apply_filters.
44
+ API_WORK_ITEM_CONDITIONS = %i[
45
+ type state status labels forbidden_labels assignees milestone weight iteration date
46
+ ].freeze
47
+
48
+ # Applied in Ruby to the fetched work items through Engine::FILTER_MAP.
49
+ RUBY_WORK_ITEM_CONDITIONS = %i[author_member assignee_member ruby no_additional_labels work_item_status].freeze
50
+
51
+ # Any other key would be silently ignored and widen the matched set, so reject it up front.
52
+ SUPPORTED_WORK_ITEM_CONDITIONS = (API_WORK_ITEM_CONDITIONS + RUBY_WORK_ITEM_CONDITIONS).freeze
53
+
54
+ # States the Work Items REST API filters on. Engine::ALLOWED_STATE_VALUES reads
55
+ # this too, since the engine requires this file and not the other way round.
56
+ WORK_ITEM_STATES = %w[opened closed].freeze
57
+
58
+ # Weight values the Work Items REST API's weight_wildcard_id filter takes, lowercased.
59
+ WORK_ITEM_WEIGHT_WILDCARDS = %w[none any].freeze
60
+
61
+ # The Issues REST API coerces a quoted number, so issues rules may write weight: '3'.
62
+ WORK_ITEM_QUOTED_WEIGHT_PATTERN = /\A\d+\z/
63
+
64
+ # Iteration wildcards the issues and work_items policy types take, lowercased.
65
+ # Engine::ITERATION_SELECTION_VALUES reads this too, so work_items keeps taking the same
66
+ # wildcards as issues. The APIs also take Current, which neither policy type takes as a wildcard.
67
+ ITERATION_WILDCARDS = %w[none any].freeze
68
+
69
+ # Error hints for the two rejected values that work on the issues policy type.
70
+ ITERATION_CURRENT_HINT = ' current is left out to match the issues policy type, which reads it as an ' \
71
+ 'iteration name, not the Current wildcard.'
72
+ ITERATION_NAME_HINT = ' The Work Items REST API cannot filter by iteration name.'
73
+
74
+ # WorkItemsRestSource#apply_weight_filter sends a number as weight and anything else as
75
+ # weight_wildcard_id, which the API rejects for any value but None or Any, so check before
76
+ # any earlier rule has acted. The source calls this too, so a weight that skipped load-time
77
+ # validation cannot be reported as an unavailable API, or skip the filter.
78
+ def self.validate_work_items_weight!(weight)
79
+ return if weight_number?(weight)
80
+ return if weight.is_a?(String) && WORK_ITEM_WEIGHT_WILDCARDS.include?(weight.downcase)
81
+
82
+ raise InvalidConditionError,
83
+ "The weight condition on work_items policies takes a whole number of 0 or more, " \
84
+ "or one of #{WORK_ITEM_WEIGHT_WILDCARDS.join(', ')}, but got #{weight.inspect}."
85
+ end
86
+
87
+ def self.weight_number?(weight)
88
+ return weight >= 0 if weight.is_a?(Integer)
89
+
90
+ weight.is_a?(String) && weight.match?(WORK_ITEM_QUOTED_WEIGHT_PATTERN)
91
+ end
92
+
93
+ # The Work Items REST API declares no iteration_title filter, so unlike on the issues
94
+ # policy type an iteration name is rejected rather than sent. Anything else would be
95
+ # sent as an iteration_wildcard_id the API rejects. The source calls this too, so an
96
+ # iteration that skipped load-time validation cannot be reported as an unavailable API.
97
+ def self.validate_work_items_iteration!(iteration)
98
+ return if iteration.is_a?(String) && ITERATION_WILDCARDS.include?(iteration.downcase)
99
+
100
+ message = "The iteration condition on work_items policies takes one of " \
101
+ "#{ITERATION_WILDCARDS.join(', ')}, but got #{iteration.inspect}."
102
+ message += iteration_hint(iteration).to_s
103
+
104
+ raise InvalidConditionError, message
105
+ end
106
+
107
+ def self.iteration_hint(iteration)
108
+ return unless iteration.is_a?(String) && iteration.strip.present?
109
+
110
+ iteration.casecmp?('current') ? ITERATION_CURRENT_HINT : ITERATION_NAME_HINT
111
+ end
112
+
113
+ # WorkItemsRestSource#apply_date_filter would send no date filter for a bad attribute
114
+ # or a missing interval, so the rule would match every item, and reads any condition
115
+ # other than older_than as newer_than. Check the values against the same parameters
116
+ # the Ruby date filter uses, so filter_in_ruby: true accepts exactly the same ones.
117
+ # The source calls this too, so a condition that skipped load-time validation still
118
+ # cannot reach the API unchecked.
119
+ def self.validate_work_items_date!(date_condition)
120
+ # Despite the name, filter_parameters returns the definitions of the date
121
+ # condition's keys (name, type, allowed values), not API query params.
122
+ date_parameters = Filters::WorkItemDateConditionsFilter.filter_parameters
123
+
124
+ unless date_condition.is_a?(Hash)
125
+ raise InvalidConditionError,
126
+ "The date condition on work_items policies takes " \
127
+ "#{date_parameters.pluck(:name).to_sentence}, but got #{date_condition.inspect}."
128
+ end
129
+
130
+ date_condition = date_condition.with_indifferent_access
131
+ ParamsValidator.new(date_parameters, date_condition).validate!
132
+ validate_date_interval!(date_condition)
133
+ rescue ParamsValidator::InvalidParameter => e
134
+ raise InvalidConditionError,
135
+ "Invalid date condition on work_items policies: #{e.message}, but got #{date_condition.inspect}."
136
+ end
19
137
 
20
- # Action keys Action.actions_for dispatches for work_items policies.
21
- # Anything outside this list would silently do nothing, so reject it up front.
22
- SUPPORTED_WORK_ITEM_ACTIONS = %i[labels remove_labels assignees summarize comment_on_summary].freeze
138
+ # A zero or negative interval puts the boundary at or after now, so older_than
139
+ # matches every item; YAML's .inf and .nan load as Floats and raise only when
140
+ # the boundary is computed, partway through a run.
141
+ def self.validate_date_interval!(date_condition)
142
+ interval = date_condition[:interval]
143
+ return if interval.finite? && interval.positive?
144
+
145
+ raise ParamsValidator::InvalidParameter, 'interval must be a positive, finite number'
146
+ end
147
+ private_class_method :validate_date_interval!
23
148
 
24
149
  def initialize(resource_rules)
25
150
  @resource_rules = resource_rules || {}
@@ -29,15 +154,71 @@ module Gitlab
29
154
  @resource_rules.each do |resource_type, policy_definition|
30
155
  next unless resource_type.to_s == 'work_items'
31
156
 
157
+ validate_work_items_conditions!(policy_definition)
32
158
  validate_work_items_actions!(policy_definition)
33
159
  end
34
160
  end
35
161
 
36
162
  private
37
163
 
164
+ def validate_work_items_conditions!(policy_definition)
165
+ condition_sets_from(policy_definition).each do |conditions|
166
+ unsupported = conditions.keys.map(&:to_sym) - SUPPORTED_WORK_ITEM_CONDITIONS
167
+ unless unsupported.none?
168
+ raise UnsupportedConditionError,
169
+ "Unsupported condition(s) for work_items policies: #{unsupported.join(', ')}. " \
170
+ "Supported conditions: #{SUPPORTED_WORK_ITEM_CONDITIONS.join(', ')}."
171
+ end
172
+
173
+ validate_state!(conditions[:state]) if conditions.key?(:state)
174
+ validate_milestone!(conditions[:milestone]) if conditions.key?(:milestone)
175
+ self.class.validate_work_items_weight!(conditions[:weight]) if conditions.key?(:weight)
176
+ self.class.validate_work_items_iteration!(conditions[:iteration]) if conditions.key?(:iteration)
177
+ self.class.validate_work_items_date!(conditions[:date]) if conditions.key?(:date)
178
+ end
179
+ end
180
+
181
+ # Checked here rather than when the rule fetches, so a typo such as `open`
182
+ # fails before any earlier rule has acted. `merged` is valid for merge
183
+ # requests but not for work items.
184
+ def validate_state!(state)
185
+ return if WORK_ITEM_STATES.include?(state)
186
+
187
+ raise InvalidConditionError,
188
+ "The state condition on work_items policies takes one of #{WORK_ITEM_STATES.join(', ')}, " \
189
+ "but got #{state.inspect}."
190
+ end
191
+
192
+ # The issues policy type silently uses the first of several milestones, and
193
+ # skips the filter for a blank one, so the rule matches every item. work_items
194
+ # has no old policies to stay compatible with, so it rejects both.
195
+ def validate_milestone!(milestone_condition)
196
+ milestones = Array(milestone_condition)
197
+ if milestones.size > 1
198
+ raise InvalidConditionError,
199
+ "The milestone condition takes one value on work_items policies, " \
200
+ "but got #{milestones.size}: #{milestones.join(', ')}."
201
+ end
202
+
203
+ milestone = milestones.first
204
+ return if milestone.is_a?(String) && milestone.strip.present?
205
+
206
+ raise InvalidConditionError, invalid_milestone_message(milestone)
207
+ end
208
+
209
+ # YAML reads an unquoted 19.10 as the number 19.1, so a version-like title
210
+ # must be quoted to reach the API intact.
211
+ def invalid_milestone_message(milestone)
212
+ message = "The milestone condition on work_items policies takes a milestone title " \
213
+ "or one of #{MilestoneWildcards::VALUES.join(', ')}, but got #{milestone.inspect}."
214
+ return message unless milestone.is_a?(Numeric)
215
+
216
+ "#{message} Quote titles that look like numbers, for example milestone: '19.10'."
217
+ end
218
+
38
219
  def validate_work_items_actions!(policy_definition)
39
- action_sets_from(policy_definition).each do |actions|
40
- raise UnsupportedActionError, COMMENT_NOT_SUPPORTED_MESSAGE if actions.key?(:comment)
220
+ action_sets_from(policy_definition).each do |actions, supported, scope|
221
+ validate_state_action!(actions) if supported.include?(:state)
41
222
 
42
223
  # GitLab applies remove_label_ids after add_label_ids, so a label in
43
224
  # both lists is always removed. Catch the typo here rather than let the
@@ -49,25 +230,52 @@ module Gitlab
49
230
  "A label cannot be added and removed in the same rule."
50
231
  end
51
232
 
52
- unsupported = actions.keys.map(&:to_sym) - SUPPORTED_WORK_ITEM_ACTIONS
233
+ unsupported = actions.keys.map(&:to_sym) - supported
53
234
  next if unsupported.none?
54
235
 
55
236
  raise UnsupportedActionError,
56
- "Unsupported action(s) for work_items policies: #{unsupported.join(', ')}. " \
57
- "Supported actions: #{SUPPORTED_WORK_ITEM_ACTIONS.join(', ')}."
237
+ "Unsupported action(s) for work_items #{scope}: #{unsupported.join(', ')}. " \
238
+ "Supported actions: #{supported.join(', ')}."
239
+ end
240
+ end
241
+
242
+ def validate_state_action!(actions)
243
+ if WORK_ITEM_STATE_EVENTS.include?(actions[:status].to_s)
244
+ raise UnsupportedActionError,
245
+ "`status: #{actions[:status]}` is not supported on work_items policies. " \
246
+ "Use `state: #{actions[:status]}` instead."
58
247
  end
248
+
249
+ return unless actions.key?(:state)
250
+ return if WORK_ITEM_STATE_EVENTS.include?(actions[:state].to_s)
251
+
252
+ raise UnsupportedActionError,
253
+ "Unsupported state action for work_items policies: #{actions[:state]}. " \
254
+ "Supported values: #{WORK_ITEM_STATE_EVENTS.join(', ')}."
59
255
  end
60
256
 
61
- # Every place a policy definition can carry actions: top-level rules,
62
- # rules nested in summaries, and the summaries' own actions.
257
+ # Every place a policy definition can carry actions, with the actions
258
+ # allowed there: top-level rules, rules nested in summaries, and the
259
+ # summaries' own actions.
63
260
  def action_sets_from(policy_definition)
64
261
  policy_definition ||= {}
65
262
 
66
263
  summaries = Array(policy_definition[:summaries])
67
264
  summary_rules = summaries.flat_map { |summary| Array(summary[:rules]) }
68
265
 
69
- (Array(policy_definition[:rules]) + summary_rules + summaries)
70
- .map { |definition| definition[:actions] || {} }
266
+ Array(policy_definition[:rules]).map { |rule| [rule[:actions] || {}, SUPPORTED_WORK_ITEM_ACTIONS, 'policies'] } +
267
+ summary_rules.map { |rule| [rule[:actions] || {}, SUPPORTED_SUMMARY_RULE_ACTIONS, 'summary rules'] } +
268
+ summaries.map { |summary| [summary[:actions] || {}, SUPPORTED_SUMMARY_ACTIONS, 'summaries'] }
269
+ end
270
+
271
+ # Conditions live on top-level rules and rules nested in summaries.
272
+ def condition_sets_from(policy_definition)
273
+ policy_definition ||= {}
274
+
275
+ summary_rules = Array(policy_definition[:summaries]).flat_map { |summary| Array(summary[:rules]) }
276
+
277
+ (Array(policy_definition[:rules]) + summary_rules)
278
+ .map { |definition| definition[:conditions] || {} }
71
279
  end
72
280
  end
73
281
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gitlab
4
4
  module Triage
5
- VERSION = '1.57.0'
5
+ VERSION = '1.59.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-triage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.57.0
4
+ version: 1.59.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitLab
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-19 00:00:00.000000000 Z
11
+ date: 2026-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -252,6 +252,8 @@ files:
252
252
  - lib/gitlab/triage/action/summarize.rb
253
253
  - lib/gitlab/triage/action/update.rb
254
254
  - lib/gitlab/triage/action/work_item.rb
255
+ - lib/gitlab/triage/action/work_item_comment.rb
256
+ - lib/gitlab/triage/action/work_item_state.rb
255
257
  - lib/gitlab/triage/action/work_item_status.rb
256
258
  - lib/gitlab/triage/api_observability.rb
257
259
  - lib/gitlab/triage/api_query_builders/base_query_param_builder.rb
@@ -294,6 +296,7 @@ files:
294
296
  - lib/gitlab/triage/graphql_queries/query_param_builders/date_param_builder.rb
295
297
  - lib/gitlab/triage/limiters/base_limiter.rb
296
298
  - lib/gitlab/triage/limiters/date_field_limiter.rb
299
+ - lib/gitlab/triage/milestone_wildcards.rb
297
300
  - lib/gitlab/triage/network.rb
298
301
  - lib/gitlab/triage/network_adapters/base_adapter.rb
299
302
  - lib/gitlab/triage/network_adapters/graphql_adapter.rb