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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 624c4d1977031933b3f310f7ae5c82778ac62bcda0f86a0c2c4a375504b6e715
4
- data.tar.gz: 9545979a57eea0ff733995ac553271ad310c2c2710ac4fe78645daff962449b5
3
+ metadata.gz: f0eb4b3c4c796bca65d1ec29040c8a7963cbdc4f9a7a2aca442819dd18e9473e
4
+ data.tar.gz: 5d39d860952878d6843c430e216308d72d6457cb077d4731267597832b4eb32b
5
5
  SHA512:
6
- metadata.gz: 1721670e0285a0d0fffd8cb076becb239e859267d1da55f4819526f39128dec62e884251f6ae28e9d1550eeca2eb29f8c5b08c1c8d6e16b480f22254bf20016c
7
- data.tar.gz: 7b233404c63d41de48dfadf6e7fdacadb6412c47fe00562d417626534ef21b5dd8c4c14340b1d46f27bad1c5c2e58378d55e80b6aac5c8e0f25f12d2075c3515
6
+ metadata.gz: a20e94634c5849d85e93018377672bb5070dcebe504fa5bb309ed2fd2027ccd54057be3efa9c44b3ca2e7bbe3657ce2994fd479d8017f90f70ed0f9f8acd6fac
7
+ data.tar.gz: 8a127ec91870fae1afa0337880593390ca3430506b715bcf99c948c0c0cd6a8ad74343c832624b687f20664953a86e7f31afbd5ee1fa691056ee0e6c959edeb7
data/.gitlab/CODEOWNERS CHANGED
@@ -1 +1 @@
1
- * @gl-dx
1
+ * @gl-dx/development-health @@maintainer
@@ -8,4 +8,8 @@ that reviewers can understand your intent. Keeping the description updated is
8
8
  especially important if they didn't participate in the discussion.
9
9
  -->
10
10
 
11
+ ## Checklist
12
+
13
+ - [ ] User-visible commits have a `Changelog:` trailer (see [CONTRIBUTING.md](https://gitlab.com/gitlab-org/ruby/gems/gitlab-triage/-/blob/master/CONTRIBUTING.md#changelog)).
14
+
11
15
  <!-- template sourced from https://gitlab.com/gitlab-org/ruby/gems/gitlab-triage/-/blob/master/.gitlab/merge_request_templates/Default.md -->
data/CONTRIBUTING.md CHANGED
@@ -29,3 +29,25 @@ git checkout my_branch
29
29
  bundle install --path vendor/bundle
30
30
  bundle exec rake spec
31
31
  ```
32
+
33
+ ## Changelog
34
+
35
+ Release notes are generated from `Changelog:` commit trailers using the
36
+ [GitLab changelog API](https://docs.gitlab.com/ee/api/repositories.html#generate-changelog-data).
37
+ The available categories are listed in `.gitlab/changelog_config.yml`:
38
+ `added`, `fixed`, `changed`, `deprecated`, `removed`, `security`,
39
+ `performance`, and `other`.
40
+
41
+ - Add a trailer to every commit with a user-visible change
42
+ - Dependency updates, documentation-only changes, and internal refactors
43
+ don't need one
44
+ - Use `changed` when a change makes previously-loading policies fail
45
+
46
+ ```
47
+ Add remove_labels action to the work_items policy type
48
+
49
+ The work_items type only supported labels and assignees, so
50
+ remove_labels was rejected at load time.
51
+
52
+ Changelog: added
53
+ ```
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gitlab-triage (1.57.0)
4
+ gitlab-triage (1.59.0)
5
5
  activesupport (>= 5.1)
6
6
  globalid (~> 1.0, >= 1.0.1)
7
7
  graphql (>= 2.4.13, < 2.7.0)
@@ -161,7 +161,7 @@ GEM
161
161
  method_source (~> 1.0)
162
162
  public_suffix (6.0.2)
163
163
  racc (1.8.0)
164
- rack (3.2.7)
164
+ rack (3.2.8)
165
165
  rainbow (3.1.1)
166
166
  rake (13.2.1)
167
167
  rb-fsevent (0.11.2)
data/README.md CHANGED
@@ -188,15 +188,75 @@ resource_rules:
188
188
 
189
189
  #### Supported conditions
190
190
 
191
+ Sent to the Work Items REST API:
192
+
191
193
  - `type` - match work item types by name, including custom types. Maps to the
192
- `work_item_type_names` filter (case-insensitive, server-side).
193
- - `state` - `opened` or `closed`.
194
+ `work_item_type_names` filter (case-insensitive, server-side). Any work item
195
+ type name is accepted.
196
+ - `state` - `opened` or `closed`, checked at load time.
194
197
  - `status` - custom work item status name (for example `In progress`).
195
198
  Enterprise Edition only.
196
199
  - `labels` / `forbidden_labels` - label names to require or exclude.
197
200
  - `assignees` - assignee usernames.
201
+ - `milestone` - one milestone title, or one of `none`, `any`, `upcoming`,
202
+ `started` (case-insensitive). Maps to the `milestone_title` or
203
+ `milestone_wildcard_id` filter. Same shape as the
204
+ [milestone condition](#milestone-condition). A list of more than one
205
+ milestone, or a blank one, is rejected at load time. Quote titles that look
206
+ like numbers (`milestone: '19.10'`): YAML reads an unquoted `19.10` as
207
+ `19.1`, so it is rejected too.
208
+
209
+ `upcoming` and `started` follow GitLab's current milestone logic, so they
210
+ can match different work items than the same values on `issues`. The
211
+ Issues REST API keeps the logic deprecated in GitLab 17.7 to avoid a
212
+ breaking change
213
+ ([gitlab-org/gitlab#501294](https://gitlab.com/gitlab-org/gitlab/-/issues/501294)),
214
+ and the Work Items REST API does not:
215
+
216
+ - `upcoming`: on `issues`, the one milestone per project or group with the
217
+ nearest future due date, often the milestone in progress. On
218
+ `work_items`, every milestone that has not started yet.
219
+ - `started`: on `issues`, active milestones that have started, including
220
+ overdue ones. On `work_items`, only those whose due date has not passed.
221
+
222
+ Dry-run a rule that uses `upcoming` or `started` before moving it from
223
+ `issues` to `work_items`.
224
+ - `weight` - a whole number of 0 or more, quoted or not, or `none` or `any`
225
+ (case-insensitive). Maps to the `weight` or `weight_wildcard_id` filter.
226
+ Anything else is rejected at load time. `none` matches only work items with no
227
+ weight set, and 0 is a weight, so a work item weighted 0 matches `any` and
228
+ `0` but not `none`. The API documents weight filtering as Premium and Ultimate
229
+ only. On GitLab Community Edition the API doesn't have the filter and ignores
230
+ it, so the rule matches work items of any weight.
231
+ - `iteration` - `none` or `any` (case-insensitive). Maps to the
232
+ `iteration_wildcard_id` filter. Unlike on `issues`, an iteration name is
233
+ rejected at load time, because the Work Items REST API can't filter by
234
+ iteration name. Anything else is rejected at load time too. The API documents
235
+ iteration filtering as Premium and Ultimate only. On GitLab Community Edition
236
+ the API doesn't have the filter and ignores it, so the rule matches work items
237
+ in any iteration or none.
198
238
  - `date` - `created_at` / `updated_at` staleness (same shape as the
199
- [date condition](#date-condition)).
239
+ [date condition](#date-condition)). `attribute`, `condition`,
240
+ `interval_type`, and `interval` are all required and checked at load time,
241
+ with or without `filter_in_ruby`, and `interval` must be a positive number.
242
+ A missing or unsupported value would otherwise send no date filter, so the
243
+ rule would match every work item, and a misspelled `condition` would select
244
+ the opposite work items.
245
+
246
+ Run in Ruby on the fetched work items:
247
+
248
+ - `author_member` - see [author member condition](#author-member-condition).
249
+ - `assignee_member` - see [assignee member condition](#assignee-member-condition).
250
+ - `ruby` - see [ruby condition](#ruby-condition).
251
+ - `no_additional_labels` - see [no additional labels condition](#no-additional-labels-condition).
252
+ - `work_item_status` - see [work item status condition](#work-item-status-condition).
253
+
254
+ Any other condition key (for example `issue_type` or a misspelled key) is
255
+ rejected at load time, before any resource is fetched, with an error
256
+ listing the supported conditions. Ignoring it instead would make the rule match
257
+ more work items than intended. `votes`, `upvotes`, and `discussions` are not
258
+ supported yet (tracked in
259
+ [gitlab-org/gitlab#630293](https://gitlab.com/gitlab-org/gitlab/-/work_items/630293)).
200
260
 
201
261
  #### Supported actions
202
262
 
@@ -208,26 +268,47 @@ resource_rules:
208
268
  - `assignees` - set assignees by username. If any requested username does not
209
269
  exist, the automation stops immediately rather than sending an empty
210
270
  assignee set (which would unassign everyone).
211
- - `comment` - **not yet supported.** The Work Items REST API does not yet
212
- provide a note-create endpoint (tracked in
213
- [gitlab-org/gitlab#604028](https://gitlab.com/gitlab-org/gitlab/-/work_items/604028)).
214
- A `comment` action on a `work_items` policy is rejected at load time, before
215
- any resource is fetched or acted on, with a clear error - so an unsupported
216
- policy never runs partway through. Use the `issues` policy type if you need
217
- to comment.
271
+ - `state` - close the work item with `state: close` or reopen it with
272
+ `state: reopen`, the same values as the `status` action on `issues`. The
273
+ `state` condition still takes `opened` and `closed`, so a rule that closes
274
+ open work items has `state: opened` in its conditions and `state: close` in
275
+ its actions. On `work_items`, `status` is reserved for the configurable work
276
+ item status, so `status: close` and `status: reopen` are rejected with a
277
+ pointer to `state`.
278
+ - The state change runs last, in its own update, after the rule's label,
279
+ assignee, and comment actions. If a label or assignee change fails, or the
280
+ note request keeps failing with 429 or 500 after retries, the run stops
281
+ before the state changes, so the next run retries it. Any other note error
282
+ (for example a 403 or 503) is reported as a warning, and the state still
283
+ changes.
284
+ - If the state change fails on some work items, the others still change
285
+ state, and then the run stops with an error that lists the failed ones. A
286
+ failed work item still matches the rule, so with `comment` it gets the
287
+ note again on each run until the cause is fixed.
288
+ - In a rule without `comment` or `mention`, work items already in the
289
+ requested state are skipped.
290
+ - Where configurable work item status is available, closing or reopening
291
+ also moves the status to its lifecycle default, for example the default
292
+ closed status.
293
+ - `comment` - post a comment (note) on the work item through the Work Items REST
294
+ API. Supports the same body templating as the `issues` type, along with
295
+ `mention`, `comment_type: thread`, `comment_internal`, and
296
+ `redact_confidential_resources`. The `{{closed_by}}` placeholder needs
297
+ GitLab 19.6 or later. Quick actions in the body are run by the API, but the
298
+ available command set follows the work item type's enabled widgets, so some
299
+ issue-only commands are no-ops on work items. The comment is posted before the
300
+ `state` action runs, so `state` wins over a `/close` or `/reopen` in the body.
218
301
 
219
302
  Summaries are also supported (`summarize` and `comment_on_summary`). Any other
220
- action key (for example `mention`, `move`, or `status`) is
221
- rejected at load time with an error listing the supported actions. On the
222
- older policy types these keys are applied through comment quick actions, which
223
- work items do not support yet - rejecting them up front beats silently doing
224
- nothing.
303
+ action key (for example `move` or `status`) is rejected at load time with an
304
+ error listing the supported actions, since those actions are not wired for the
305
+ `work_items` type yet - rejecting them up front beats silently doing nothing.
225
306
 
226
307
  Actions require a token with sufficient permissions (Planner or Reporter role
227
308
  and above). The update endpoint can return success without persisting a change
228
- when the token lacks permission, so after each update the applied labels and
229
- assignees are read back from the response and the run raises if the change was
230
- not reflected.
309
+ when the token lacks permission, so after each update the applied labels,
310
+ assignees, and state are read back from the response and the run raises if the
311
+ change was not reflected.
231
312
 
232
313
  The `work_items` type works against both project and group sources
233
314
  (`--source projects` or `--source groups`). Fetches use the source you run
@@ -235,7 +316,40 @@ against; updates and label lookups target each work item's own namespace,
235
316
  because a group run can return work items living in different sub-projects and
236
317
  an iid is only unique within its namespace. Project-scoped resource helpers
237
318
  (for example `project_path` in templates) likewise resolve the underlying
238
- project from the work item's namespace.
319
+ project from the work item's namespace. `full_resource_reference` and the
320
+ `{{full_reference}}` placeholder render the work item's full reference (for
321
+ example `gitlab-org/gitlab#42`), the same as on `issues`. `resource[:health_status]`
322
+ is a string such as `on_track`, `needs_attention`, or `at_risk`, the same as on
323
+ `issues`. It is `nil` when no health status is set, and always `nil` on GitLab
324
+ Community Edition or a namespace without the license for health status. After a
325
+ license downgrade, work items can keep a stored health status that the API no
326
+ longer returns, so a rule such as `resource[:health_status] != 'at_risk'` can
327
+ match work items that are at risk.
328
+ `resource[:description]` is the work item's description in Markdown, the same as
329
+ on `issues`.
330
+
331
+ `resource[:moved_to_id]` and `resource[:moved_to_work_item_url]` are the id and
332
+ URL of the work item it was moved to, so rules that skip moved items with
333
+ `resource[:moved_to_id].nil?` work the same as on `issues`. `moved_to_id` needs
334
+ GitLab 19.6 or later. Earlier versions leave out fields they do not support
335
+ without an error, so `moved_to_id` would be `nil` for every work item.
336
+ `moved_to_work_item_url` works on earlier versions too. When a `work_items`
337
+ rule or summary rule has a `ruby` condition that reads `moved_to_id`,
338
+ `gitlab-triage` checks the GitLab version before running any rule in that
339
+ policy file. On an older version, it stops with an error naming those
340
+ rules. If it cannot read the version, for example because the version request
341
+ fails with a server error or returns a page that isn't JSON, it prints a
342
+ warning and instead stops when one of those rules fetches work items that have
343
+ no `moved_to_id`, so rules before it in the file have already run. An
344
+ unauthorized token still stops the run. Templates and other conditions that
345
+ read `moved_to_id` are not checked, and on older versions they read `nil`.
346
+
347
+ On `issues`, `moved_to_id` is set for every moved issue. On `work_items`,
348
+ `moved_to_id` and `moved_to_work_item_url` are set only when the token can read
349
+ the work item it was moved to. Otherwise they're `nil`, so
350
+ `resource[:moved_to_id].nil?` also matches work items moved to a project the
351
+ token can't read. Run such rules on GitLab 19.6 or later, with a token that can
352
+ read the projects work items are moved to.
239
353
 
240
354
  The `api:` field is not applicable to the `work_items` type, since the type
241
355
  already fixes the transport to the Work Items REST API.
@@ -244,13 +358,18 @@ already fixes the transport to the Work Items REST API.
244
358
 
245
359
  The `work_items` condition names (`type`, `status`, `state`) are deliberately
246
360
  cleaner than the `issues` equivalents (`issue_type`, `work_item_status`,
247
- `state`). This is intentional: they are meant to become the shared vocabulary
248
- that `issues` converges onto over time (long term, `issues` would alias to
249
- `work_items` and eventually deprecate in a major version), not a throwaway
250
- parallel dialect. `state` (`opened` / `closed`) already means the same on both
251
- types; `status` is new on `work_items` and has no clean one-to-one equivalent
252
- on `issues` (where it is the prefixed `work_item_status`). Whether the old
253
- names are carried as aliases or dropped at a major bump is a future decision.
361
+ `state`). `issues` keeps its names as they are. It is planned to be deprecated,
362
+ and then removed in a major version once `work_items` has feature parity
363
+ (tracked in
364
+ [gitlab-org/gitlab#634685](https://gitlab.com/gitlab-org/gitlab/-/work_items/634685)).
365
+ `state` (`opened` / `closed`) means the same on both types. `status` is new on
366
+ `work_items` and has no clean one-to-one equivalent on `issues` (where it is the
367
+ prefixed `work_item_status`).
368
+
369
+ The same applies to actions. On `issues`, the `status` action closes or reopens
370
+ the item (`status: close` / `status: reopen`). On `work_items`, that is the
371
+ `state` action with the same values (`state: close` / `state: reopen`), and
372
+ `status` is reserved for the configurable work item status.
254
373
 
255
374
  ### Fields
256
375
 
@@ -1121,6 +1240,10 @@ Changes the open or closed state of the resource.
1121
1240
  This sets the open or closed state, not the work item lifecycle status. To
1122
1241
  change work item status, see the [`work_item_status` action](#work-item-status-action).
1123
1242
 
1243
+ On the `work_items` type, use the [`state` action](#supported-actions) with the
1244
+ same values (`state: close` / `state: reopen`). There, `status` is reserved for
1245
+ the configurable work item status.
1246
+
1124
1247
  Accepts a string.
1125
1248
 
1126
1249
  | State transition | Type | Value |
@@ -1526,6 +1649,10 @@ resource_rules:
1526
1649
 
1527
1650
  Delete the resource.
1528
1651
 
1652
+ When GitLab refuses a deletion, for example with 403 because the token may not
1653
+ delete the resource, a warning with the URL, the status and GitLab's message is
1654
+ printed, and the rule goes on with the next resource.
1655
+
1529
1656
  Accept a boolean. Set to `true` to enable.
1530
1657
 
1531
1658
  Example :
@@ -61,21 +61,25 @@ module Gitlab
61
61
  end
62
62
 
63
63
  def perform(resource, comment)
64
- if network.options.debug
65
- if policy.actions.fetch(:redact_confidential_resources, true) && resource[:confidential]
66
- puts Gitlab::Triage::UI.debug "Comment action resource: (confidential)"
67
- else
68
- puts Gitlab::Triage::UI.debug "Comment action resource: #{resource.inspect}"
69
- end
70
-
71
- puts Gitlab::Triage::UI.debug "Comment action content: #{comment}"
72
- end
64
+ print_debug(resource, comment)
73
65
 
74
66
  network.post_api(
75
67
  build_post_url(resource),
76
68
  post_body(comment))
77
69
  end
78
70
 
71
+ def print_debug(resource, comment)
72
+ return unless network.options.debug
73
+
74
+ if policy.actions.fetch(:redact_confidential_resources, true) && resource[:confidential]
75
+ puts Gitlab::Triage::UI.debug "Comment action resource: (confidential)"
76
+ else
77
+ puts Gitlab::Triage::UI.debug "Comment action resource: #{resource.inspect}"
78
+ end
79
+
80
+ puts Gitlab::Triage::UI.debug "Comment action content: #{comment}"
81
+ end
82
+
79
83
  def build_post_url(resource)
80
84
  url_builder_opts = {
81
85
  network_options: network.options,
@@ -15,7 +15,7 @@ module Gitlab
15
15
  private
16
16
 
17
17
  def perform(resource)
18
- puts "DELETE resource with type: #{resource[:type]} and id: #{resource_id(resource)}"
18
+ puts "DELETE #{build_delete_url(resource)}"
19
19
  end
20
20
  end
21
21
 
@@ -36,7 +36,8 @@ module Gitlab
36
36
  def build_delete_url(resource)
37
37
  delete_url = UrlBuilders::UrlBuilder.new(
38
38
  source: policy.source,
39
- source_id: network.options.source_id,
39
+ # Branches carry no project_id, so they fall back to --source-id.
40
+ source_id: resource[policy.source_id_sym] || network.options.source_id,
40
41
  resource_type: policy.type,
41
42
  resource_id: resource_id(resource),
42
43
  network_options: network.options
@@ -22,9 +22,8 @@ module Gitlab
22
22
  class WorkItem < Base
23
23
  UpdateNotAppliedError = Class.new(StandardError)
24
24
 
25
- # The dry run must not touch the network. It prints the requested
26
- # label names and assignee usernames rather than resolving them to
27
- # ids, mirroring how Comment::Dry avoids network calls.
25
+ # Prints the requested changes without writing them. verify! still
26
+ # looks the names up first, so a typo fails the dry run too.
28
27
  class Dry < WorkItem
29
28
  def act
30
29
  puts "The following work items would be updated for the rule **#{policy.name}**:\n\n"
@@ -43,6 +42,17 @@ module Gitlab
43
42
  end
44
43
  end
45
44
 
45
+ # Resolves every item's labels and assignees up front, so a name missing
46
+ # in a later item's namespace fails the run before any item is labeled.
47
+ def verify!
48
+ policy.resources.each do |resource|
49
+ label_ids_to_add(resource) if requested_labels.any?
50
+ label_ids_to_remove(resource) if requested_removed_labels.any?
51
+ end
52
+
53
+ assignee_ids if requested_assignees.any?
54
+ end
55
+
46
56
  def act
47
57
  return unless requested_labels.any? || requested_removed_labels.any? || requested_assignees.any?
48
58
 
@@ -126,7 +136,7 @@ module Gitlab
126
136
 
127
137
  def labels_by_name(scope)
128
138
  @labels_by_name_by_scope ||= {}
129
- @labels_by_name_by_scope[scope] ||= network.query_api(labels_url(scope)).index_by { |label| label[:name] }
139
+ @labels_by_name_by_scope[scope] ||= network.query_api_cached(labels_url(scope)).index_by { |label| label[:name] }
130
140
  end
131
141
 
132
142
  # The Work Items REST update endpoint can return success without
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'comment'
4
+ require_relative '../sources/work_items_rest_source'
5
+
6
+ module Gitlab
7
+ module Triage
8
+ module Action
9
+ # Posts comments on work items through the Work Items REST API notes
10
+ # endpoint rather than the issues note/quick-action path. The comment body
11
+ # (templating, redaction, and mentions) is built exactly as on the issues
12
+ # type - only the write target differs, so this reuses Comment and
13
+ # overrides just where the note is sent.
14
+ class WorkItemComment < Comment
15
+ # WorkItem writes labels before this posts, so reject an unknown
16
+ # comment_type up front rather than after the labels are written.
17
+ def verify!
18
+ sub_resource_type
19
+ end
20
+
21
+ private
22
+
23
+ # Raising would also lose every later item's note, and any labels are
24
+ # already written. Warn and move on, as issues drop a rejected note.
25
+ def perform(resource, comment)
26
+ print_debug(resource, comment)
27
+ source.create_note(resource, post_body(comment), sub_resource_type: sub_resource_type)
28
+ rescue Sources::WorkItemsRestSource::NoteNotCreatedError => e
29
+ puts Gitlab::Triage::UI.warn(e.message)
30
+ end
31
+
32
+ def source
33
+ @source ||= Sources::WorkItemsRestSource.new(
34
+ network: network, options: network.options)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'work_item'
4
+
5
+ module Gitlab
6
+ module Triage
7
+ module Action
8
+ # Sends state_event in its own PATCH, after WorkItem and WorkItemComment.
9
+ # In one combined PATCH GitLab closes first, so a dropped label or
10
+ # assignee would leave the item closed and never matched again.
11
+ class WorkItemState < WorkItem
12
+ STATES_AFTER_EVENT = { 'close' => 'closed', 'reopen' => 'opened' }.freeze
13
+
14
+ # Without this, const_get(:Dry) would resolve to WorkItem::Dry.
15
+ class Dry < WorkItemState
16
+ def act
17
+ puts "The following work items would change state for the rule **#{policy.name}**:\n\n"
18
+
19
+ super
20
+ end
21
+
22
+ private
23
+
24
+ def perform(resource)
25
+ puts "# #{resource[:web_url]}"
26
+ puts "Would #{state_event} the work item\n\n"
27
+ end
28
+ end
29
+
30
+ def verify!; end
31
+
32
+ def act
33
+ return unless state_event
34
+
35
+ failures = policy.resources.filter_map do |resource|
36
+ # A no-op close still bumps updated_at. A comment's quick action can
37
+ # change the state after the fetch, so only skip without a comment.
38
+ next if resource[:state] == target_state && !policy.comment?
39
+
40
+ perform(resource)
41
+ nil
42
+ rescue UpdateNotAppliedError => e
43
+ e.message
44
+ end
45
+
46
+ raise UpdateNotAppliedError, failures.join("\n") if failures.any?
47
+ end
48
+
49
+ private
50
+
51
+ # `fields: state` makes the response echo the resulting state.
52
+ def perform(resource)
53
+ response = source.update(resource, {}, { state_event: state_event, fields: 'state' })
54
+
55
+ verify_state_applied!(resource, response)
56
+ end
57
+
58
+ def state_event
59
+ policy.actions[:state]&.to_s
60
+ end
61
+
62
+ def target_state
63
+ STATES_AFTER_EVENT.fetch(state_event)
64
+ end
65
+
66
+ # A rejected PATCH returns an error body with no state, so show the API's
67
+ # reason when there is one instead of always blaming permissions.
68
+ def verify_state_applied!(resource, response)
69
+ return if response[:state] == target_state
70
+
71
+ reason = response[:message] || response[:error] || 'the token may lack permission'
72
+
73
+ raise UpdateNotAppliedError,
74
+ "State was not set to #{target_state} on #{resource[:web_url]} (#{reason})."
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -7,6 +7,8 @@ require_relative 'action/issue'
7
7
  require_relative 'action/delete'
8
8
  require_relative 'action/work_item_status'
9
9
  require_relative 'action/work_item'
10
+ require_relative 'action/work_item_comment'
11
+ require_relative 'action/work_item_state'
10
12
  require_relative 'action/update'
11
13
 
12
14
  module Gitlab
@@ -24,10 +26,9 @@ module Gitlab
24
26
  end
25
27
  end
26
28
 
27
- # Comment runs before the actions that write labels, so without this a
28
- # rule naming a label, user, milestone, or move destination that no
29
- # longer exists posts its note and only then raises, and the next
30
- # scheduled run posts it again.
29
+ # Without this, a rule naming a label, user, milestone, or move
30
+ # destination that no longer exists raises only after earlier actions
31
+ # wrote, such as an issues note the next scheduled run posts again.
31
32
  def self.verify!(actions, policy:, network:)
32
33
  actions.each do |action|
33
34
  action.new(policy: policy, network: network).verify!
@@ -36,13 +37,20 @@ module Gitlab
36
37
 
37
38
  # Work items are fetched and updated through the Work Items REST API,
38
39
  # which does not share the issues note/quick-action write path. Their
39
- # actions (assign, label, comment) are handled by a dedicated action
40
- # instead of the note-based Comment action.
40
+ # actions are handled by dedicated actions: WorkItem (assign, label),
41
+ # WorkItemComment, which posts through the REST notes endpoint rather than
42
+ # the issues Comment path, and WorkItemState. Unlike issues, labels are
43
+ # written before the note: a failed label write then loses a note once,
44
+ # instead of leaving unlabeled items that get the note again on every run.
45
+ # State goes last, so a label, assignee, or note write that raises leaves
46
+ # the item matching the rule, and the next run retries it.
41
47
  def self.actions_for(policy)
42
48
  if policy.type == 'work_items'
43
49
  [
44
50
  [Summarize, policy.summarize?],
45
51
  [WorkItem, policy.work_item?],
52
+ [WorkItemComment, policy.comment?],
53
+ [WorkItemState, policy.work_item_state?],
46
54
  [CommentOnSummary, policy.comment_on_summary?]
47
55
  ]
48
56
  else