gitlab-triage 1.56.2 → 1.58.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.
@@ -2,10 +2,12 @@
2
2
 
3
3
  require 'active_support/all'
4
4
 
5
+ require_relative '../milestone_wildcards'
5
6
  require_relative '../url_builders/url_builder'
6
7
  require_relative '../normalizers/work_item_normalizer'
7
8
  require_relative '../errors'
8
9
  require_relative '../ui'
10
+ require_relative '../validators/policy_validator'
9
11
 
10
12
  module Gitlab
11
13
  module Triage
@@ -19,12 +21,12 @@ module Gitlab
19
21
  #
20
22
  # The source is a self-contained peer to the engine's inline issues /
21
23
  # merge_requests / branches fetch: it owns list URL building, filter
22
- # push-down, normalization, and the PATCH update path. Comment creation
23
- # is not yet available on the Work Items REST API, so the `comment`
24
- # action is rejected at load time by Validators::PolicyValidator.
24
+ # push-down, normalization, the PATCH update path, and the notes /
25
+ # discussions write path used by the `comment` action.
25
26
  class WorkItemsRestSource
26
27
  UnavailableError = Class.new(StandardError)
27
28
  ScopeError = Class.new(StandardError)
29
+ NoteNotCreatedError = Class.new(StandardError)
28
30
 
29
31
  # Base fields opted into on the list endpoint. The endpoint is sparse
30
32
  # by default (id, iid, global_id, title only); every field a filter or
@@ -38,15 +40,22 @@ module Gitlab
38
40
  author
39
41
  namespace
40
42
  work_item_type
43
+ reference
44
+ closed_at
45
+ closed_by
41
46
  ].freeze
42
47
 
43
- # Widget payloads opted into via `features`.
48
+ # Widget payloads opted into via `features`. status and health_status are EE
49
+ # only: CE accepts them on read endpoints and leaves them out of the response,
50
+ # as an unlicensed EE namespace does, so their values are then nil.
44
51
  LIST_FEATURES = %w[
45
52
  labels
46
53
  assignees
47
54
  milestone
48
55
  status
49
56
  start_and_due_date
57
+ award_emoji
58
+ health_status
50
59
  ].freeze
51
60
 
52
61
  def initialize(network:, options:, per_page: 100, normalizer: Normalizers::WorkItemNormalizer.new)
@@ -68,6 +77,16 @@ module Gitlab
68
77
  @network.patch_api(work_item_url(resource), features: features)
69
78
  end
70
79
 
80
+ # The adapter does not raise on a 4xx. Check for an error message, not a
81
+ # missing id, since a quick-action-only note returns 202 with no id.
82
+ def create_note(resource, body, sub_resource_type: 'notes')
83
+ response = @network.post_api(note_url(resource, sub_resource_type), body)
84
+ error = response[:id].blank? && (response[:message] || response[:error])
85
+ return response unless error
86
+
87
+ raise NoteNotCreatedError, "The note was not posted on #{resource[:web_url] || resource[:iid]} (#{error})."
88
+ end
89
+
71
90
  # Returns [source, source_id] for the namespace the resource lives in.
72
91
  # A group run can return work items from descendant projects, and an
73
92
  # iid is only unique within its own namespace, so writes and label
@@ -168,12 +187,26 @@ module Gitlab
168
187
  ).build
169
188
  end
170
189
 
190
+ def note_url(resource, sub_resource_type)
191
+ scope, scope_id = scope_for(resource)
192
+
193
+ UrlBuilders::UrlBuilder.new(
194
+ network_options: @options,
195
+ source: scope,
196
+ source_id: scope_id,
197
+ resource_type: '-/work_items',
198
+ resource_id: resource[:iid],
199
+ sub_resource_type: sub_resource_type
200
+ ).build
201
+ end
202
+
171
203
  def list_params(conditions)
172
204
  params = {
173
205
  per_page: @per_page,
174
206
  fields: LIST_FIELDS.join(','),
175
207
  features: LIST_FEATURES.join(',')
176
208
  }
209
+ params[:include_descendants] = true if @options.source == 'groups'
177
210
 
178
211
  apply_filters(params, conditions)
179
212
 
@@ -184,6 +217,8 @@ module Gitlab
184
217
  # `type` uses `work_item_type_names`, which resolves names (including
185
218
  # custom types) case-insensitively server-side - no id resolution
186
219
  # needed.
220
+ # A new work_items condition must also be added to
221
+ # Validators::PolicyValidator::API_WORK_ITEM_CONDITIONS.
187
222
  def apply_filters(params, conditions)
188
223
  params[:iids] = Array(conditions[:iids]).join(',') if conditions[:iids].present?
189
224
  params[:work_item_type_names] = Array(conditions[:type]).join(',') if conditions[:type]
@@ -193,30 +228,39 @@ module Gitlab
193
228
  params[:assignee_usernames] = Array(conditions[:assignees]).join(',') if conditions[:assignees]
194
229
  params['status[name]'] = conditions[:status] if conditions[:status]
195
230
 
231
+ apply_milestone_filter(params, conditions[:milestone]) if conditions[:milestone]
196
232
  apply_date_filter(params, conditions[:date]) if conditions[:date]
197
233
  end
198
234
 
199
- def apply_date_filter(params, date_condition)
200
- attribute = date_condition[:attribute]
201
- condition = date_condition[:condition]
202
- return unless %w[created_at updated_at].include?(attribute)
235
+ # PolicyValidator rejects more than one value, so a list here has at most one.
236
+ # The Issues REST API keeps the legacy Started/Upcoming logic and this API does
237
+ # not, so the same wildcard can match different milestones (see README).
238
+ def apply_milestone_filter(params, milestone_condition)
239
+ milestone = Array(milestone_condition).first.to_s
240
+ return if milestone.empty?
241
+
242
+ wildcard = MilestoneWildcards.api_value(milestone)
203
243
 
204
- boundary = date_boundary(date_condition)
205
- return unless boundary
244
+ if wildcard
245
+ params[:milestone_wildcard_id] = wildcard
246
+ else
247
+ params[:milestone_title] = milestone
248
+ end
249
+ end
206
250
 
207
- direction = condition == 'older_than' ? 'before' : 'after'
208
- params["#{attribute.delete_suffix('_at')}_#{direction}"] = boundary.iso8601
251
+ # A bad date condition would otherwise send no date filter, or the opposite one,
252
+ # and widen the matched set, so raise even if load-time validation was skipped.
253
+ def apply_date_filter(params, date_condition)
254
+ Validators::PolicyValidator.validate_work_items_date!(date_condition)
255
+
256
+ attribute = date_condition[:attribute]
257
+ direction = date_condition[:condition] == 'older_than' ? 'before' : 'after'
258
+ params["#{attribute.delete_suffix('_at')}_#{direction}"] = date_boundary(date_condition).iso8601
209
259
  end
210
260
 
211
261
  def date_boundary(date_condition)
212
- interval = date_condition[:interval]
213
- interval_type = date_condition[:interval_type]
214
- return unless interval && interval_type
215
-
216
- # A malformed interval_type raises here rather than silently dropping
217
- # the date bound, which would widen the matched set - dangerous in an
218
- # automation tool. interval_type is also validated by the date filter.
219
- interval.public_send(interval_type).ago # rubocop:disable GitlabSecurity/PublicSend -- interval_type constrained to a known set
262
+ # validate_work_items_date! limits interval_type to the date filter's INTERVAL_TYPES.
263
+ date_condition[:interval].public_send(date_condition[:interval_type]).ago # rubocop:disable GitlabSecurity/PublicSend -- interval_type constrained to a known set
220
264
  end
221
265
  end
222
266
  end
@@ -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,15 +13,77 @@ 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)
18
+ ConflictingActionError = Class.new(StandardError)
19
+
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
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 Work Items REST API as query params by WorkItemsRestSource#apply_filters.
40
+ API_WORK_ITEM_CONDITIONS = %i[type state status labels forbidden_labels assignees milestone date].freeze
13
41
 
14
- COMMENT_NOT_SUPPORTED_MESSAGE =
15
- 'Commenting on work_items is not yet supported by the Work Items REST API ' \
16
- '(blocked on https://gitlab.com/gitlab-org/gitlab/-/work_items/604028). ' \
17
- 'Use the issues policy type for comment actions.'
42
+ # Applied in Ruby to the fetched work items through Engine::FILTER_MAP.
43
+ RUBY_WORK_ITEM_CONDITIONS = %i[author_member assignee_member ruby no_additional_labels work_item_status].freeze
18
44
 
19
- # Action keys Action.actions_for dispatches for work_items policies.
20
- # Anything outside this list would silently do nothing, so reject it up front.
21
- SUPPORTED_WORK_ITEM_ACTIONS = %i[labels assignees summarize comment_on_summary].freeze
45
+ # Any other key would be silently ignored and widen the matched set, so reject it up front.
46
+ SUPPORTED_WORK_ITEM_CONDITIONS = (API_WORK_ITEM_CONDITIONS + RUBY_WORK_ITEM_CONDITIONS).freeze
47
+
48
+ # States the Work Items REST API filters on. Engine::ALLOWED_STATE_VALUES reads
49
+ # this too, since the engine requires this file and not the other way round.
50
+ WORK_ITEM_STATES = %w[opened closed].freeze
51
+
52
+ # WorkItemsRestSource#apply_date_filter would send no date filter for a bad attribute
53
+ # or a missing interval, so the rule would match every item, and reads any condition
54
+ # other than older_than as newer_than. Check the values against the same parameters
55
+ # the Ruby date filter uses, so filter_in_ruby: true accepts exactly the same ones.
56
+ # The source calls this too, so a condition that skipped load-time validation still
57
+ # cannot reach the API unchecked.
58
+ def self.validate_work_items_date!(date_condition)
59
+ # Despite the name, filter_parameters returns the definitions of the date
60
+ # condition's keys (name, type, allowed values), not API query params.
61
+ date_parameters = Filters::WorkItemDateConditionsFilter.filter_parameters
62
+
63
+ unless date_condition.is_a?(Hash)
64
+ raise InvalidConditionError,
65
+ "The date condition on work_items policies takes " \
66
+ "#{date_parameters.pluck(:name).to_sentence}, but got #{date_condition.inspect}."
67
+ end
68
+
69
+ date_condition = date_condition.with_indifferent_access
70
+ ParamsValidator.new(date_parameters, date_condition).validate!
71
+ validate_date_interval!(date_condition)
72
+ rescue ParamsValidator::InvalidParameter => e
73
+ raise InvalidConditionError,
74
+ "Invalid date condition on work_items policies: #{e.message}, but got #{date_condition.inspect}."
75
+ end
76
+
77
+ # A zero or negative interval puts the boundary at or after now, so older_than
78
+ # matches every item; YAML's .inf and .nan load as Floats and raise only when
79
+ # the boundary is computed, partway through a run.
80
+ def self.validate_date_interval!(date_condition)
81
+ interval = date_condition[:interval]
82
+ return if interval.finite? && interval.positive?
83
+
84
+ raise ParamsValidator::InvalidParameter, 'interval must be a positive, finite number'
85
+ end
86
+ private_class_method :validate_date_interval!
22
87
 
23
88
  def initialize(resource_rules)
24
89
  @resource_rules = resource_rules || {}
@@ -28,35 +93,109 @@ module Gitlab
28
93
  @resource_rules.each do |resource_type, policy_definition|
29
94
  next unless resource_type.to_s == 'work_items'
30
95
 
96
+ validate_work_items_conditions!(policy_definition)
31
97
  validate_work_items_actions!(policy_definition)
32
98
  end
33
99
  end
34
100
 
35
101
  private
36
102
 
103
+ def validate_work_items_conditions!(policy_definition)
104
+ condition_sets_from(policy_definition).each do |conditions|
105
+ unsupported = conditions.keys.map(&:to_sym) - SUPPORTED_WORK_ITEM_CONDITIONS
106
+ unless unsupported.none?
107
+ raise UnsupportedConditionError,
108
+ "Unsupported condition(s) for work_items policies: #{unsupported.join(', ')}. " \
109
+ "Supported conditions: #{SUPPORTED_WORK_ITEM_CONDITIONS.join(', ')}."
110
+ end
111
+
112
+ validate_state!(conditions[:state]) if conditions.key?(:state)
113
+ validate_milestone!(conditions[:milestone]) if conditions.key?(:milestone)
114
+ self.class.validate_work_items_date!(conditions[:date]) if conditions.key?(:date)
115
+ end
116
+ end
117
+
118
+ # Checked here rather than when the rule fetches, so a typo such as `open`
119
+ # fails before any earlier rule has acted. `merged` is valid for merge
120
+ # requests but not for work items.
121
+ def validate_state!(state)
122
+ return if WORK_ITEM_STATES.include?(state)
123
+
124
+ raise InvalidConditionError,
125
+ "The state condition on work_items policies takes one of #{WORK_ITEM_STATES.join(', ')}, " \
126
+ "but got #{state.inspect}."
127
+ end
128
+
129
+ # The issues policy type silently uses the first of several milestones, and
130
+ # skips the filter for a blank one, so the rule matches every item. work_items
131
+ # has no old policies to stay compatible with, so it rejects both.
132
+ def validate_milestone!(milestone_condition)
133
+ milestones = Array(milestone_condition)
134
+ if milestones.size > 1
135
+ raise InvalidConditionError,
136
+ "The milestone condition takes one value on work_items policies, " \
137
+ "but got #{milestones.size}: #{milestones.join(', ')}."
138
+ end
139
+
140
+ milestone = milestones.first
141
+ return if milestone.is_a?(String) && milestone.strip.present?
142
+
143
+ raise InvalidConditionError, invalid_milestone_message(milestone)
144
+ end
145
+
146
+ # YAML reads an unquoted 19.10 as the number 19.1, so a version-like title
147
+ # must be quoted to reach the API intact.
148
+ def invalid_milestone_message(milestone)
149
+ message = "The milestone condition on work_items policies takes a milestone title " \
150
+ "or one of #{MilestoneWildcards::VALUES.join(', ')}, but got #{milestone.inspect}."
151
+ return message unless milestone.is_a?(Numeric)
152
+
153
+ "#{message} Quote titles that look like numbers, for example milestone: '19.10'."
154
+ end
155
+
37
156
  def validate_work_items_actions!(policy_definition)
38
- action_sets_from(policy_definition).each do |actions|
39
- raise UnsupportedActionError, COMMENT_NOT_SUPPORTED_MESSAGE if actions.key?(:comment)
157
+ action_sets_from(policy_definition).each do |actions, supported, scope|
158
+ # GitLab applies remove_label_ids after add_label_ids, so a label in
159
+ # both lists is always removed. Catch the typo here rather than let the
160
+ # run fail later with a misleading "token may lack permission" error.
161
+ conflicting = Array(actions[:labels]).map(&:to_s) & Array(actions[:remove_labels]).map(&:to_s)
162
+ unless conflicting.none?
163
+ raise ConflictingActionError,
164
+ "Label(s) in both labels and remove_labels for a work_items rule: #{conflicting.join(', ')}. " \
165
+ "A label cannot be added and removed in the same rule."
166
+ end
40
167
 
41
- unsupported = actions.keys.map(&:to_sym) - SUPPORTED_WORK_ITEM_ACTIONS
168
+ unsupported = actions.keys.map(&:to_sym) - supported
42
169
  next if unsupported.none?
43
170
 
44
171
  raise UnsupportedActionError,
45
- "Unsupported action(s) for work_items policies: #{unsupported.join(', ')}. " \
46
- "Supported actions: #{SUPPORTED_WORK_ITEM_ACTIONS.join(', ')}."
172
+ "Unsupported action(s) for work_items #{scope}: #{unsupported.join(', ')}. " \
173
+ "Supported actions: #{supported.join(', ')}."
47
174
  end
48
175
  end
49
176
 
50
- # Every place a policy definition can carry actions: top-level rules,
51
- # rules nested in summaries, and the summaries' own actions.
177
+ # Every place a policy definition can carry actions, with the actions
178
+ # allowed there: top-level rules, rules nested in summaries, and the
179
+ # summaries' own actions.
52
180
  def action_sets_from(policy_definition)
53
181
  policy_definition ||= {}
54
182
 
55
183
  summaries = Array(policy_definition[:summaries])
56
184
  summary_rules = summaries.flat_map { |summary| Array(summary[:rules]) }
57
185
 
58
- (Array(policy_definition[:rules]) + summary_rules + summaries)
59
- .map { |definition| definition[:actions] || {} }
186
+ Array(policy_definition[:rules]).map { |rule| [rule[:actions] || {}, SUPPORTED_WORK_ITEM_ACTIONS, 'policies'] } +
187
+ summary_rules.map { |rule| [rule[:actions] || {}, SUPPORTED_SUMMARY_RULE_ACTIONS, 'summary rules'] } +
188
+ summaries.map { |summary| [summary[:actions] || {}, SUPPORTED_SUMMARY_ACTIONS, 'summaries'] }
189
+ end
190
+
191
+ # Conditions live on top-level rules and rules nested in summaries.
192
+ def condition_sets_from(policy_definition)
193
+ policy_definition ||= {}
194
+
195
+ summary_rules = Array(policy_definition[:summaries]).flat_map { |summary| Array(summary[:rules]) }
196
+
197
+ (Array(policy_definition[:rules]) + summary_rules)
198
+ .map { |definition| definition[:conditions] || {} }
60
199
  end
61
200
  end
62
201
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gitlab
4
4
  module Triage
5
- VERSION = '1.56.2'
5
+ VERSION = '1.58.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.56.2
4
+ version: 1.58.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-05 00:00:00.000000000 Z
11
+ date: 2026-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -50,28 +50,34 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0.16'
53
+ version: '0.23'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0.16'
60
+ version: '0.23'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: graphql
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.4.13
65
68
  - - "<"
66
69
  - !ruby/object:Gem::Version
67
- version: 2.1.0
70
+ version: 2.7.0
68
71
  type: :runtime
69
72
  prerelease: false
70
73
  version_requirements: !ruby/object:Gem::Requirement
71
74
  requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.4.13
72
78
  - - "<"
73
79
  - !ruby/object:Gem::Version
74
- version: 2.1.0
80
+ version: 2.7.0
75
81
  - !ruby/object:Gem::Dependency
76
82
  name: httparty
77
83
  requirement: !ruby/object:Gem::Requirement
@@ -212,7 +218,6 @@ executables:
212
218
  extensions: []
213
219
  extra_rdoc_files: []
214
220
  files:
215
- - ".codeclimate.yml"
216
221
  - ".git-blame-ignore-revs"
217
222
  - ".gitignore"
218
223
  - ".gitlab-ci.yml"
@@ -247,6 +252,7 @@ files:
247
252
  - lib/gitlab/triage/action/summarize.rb
248
253
  - lib/gitlab/triage/action/update.rb
249
254
  - lib/gitlab/triage/action/work_item.rb
255
+ - lib/gitlab/triage/action/work_item_comment.rb
250
256
  - lib/gitlab/triage/action/work_item_status.rb
251
257
  - lib/gitlab/triage/api_observability.rb
252
258
  - lib/gitlab/triage/api_query_builders/base_query_param_builder.rb
@@ -289,6 +295,7 @@ files:
289
295
  - lib/gitlab/triage/graphql_queries/query_param_builders/date_param_builder.rb
290
296
  - lib/gitlab/triage/limiters/base_limiter.rb
291
297
  - lib/gitlab/triage/limiters/date_field_limiter.rb
298
+ - lib/gitlab/triage/milestone_wildcards.rb
292
299
  - lib/gitlab/triage/network.rb
293
300
  - lib/gitlab/triage/network_adapters/base_adapter.rb
294
301
  - lib/gitlab/triage/network_adapters/graphql_adapter.rb
data/.codeclimate.yml DELETED
@@ -1,19 +0,0 @@
1
- ---
2
- engines:
3
- bundler-audit:
4
- enabled: true
5
- duplication:
6
- enabled: true
7
- config:
8
- languages:
9
- - ruby
10
- rubocop-gitlab:
11
- enabled: true
12
- ratings:
13
- paths:
14
- - Gemfile.lock
15
- - "**.rb"
16
- exclude_paths:
17
- - spec/
18
- - vendor/
19
- - tmp/