Skip to content

Commit e62dfc5

Browse files
committed
Introduce TokenDisabled event for WorkflowRuns
1 parent 7cf4296 commit e62dfc5

File tree

14 files changed

+167
-25
lines changed

14 files changed

+167
-25
lines changed

src/api/.rubocop_todo.yml

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100`
3-
# on 2026-03-06 12:51:37 UTC using RuboCop version 1.85.1.
3+
# on 2026-03-16 13:23:45 UTC using RuboCop version 1.85.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 3495
9+
# Offense count: 3493
1010
# This cop supports safe autocorrection (--autocorrect).
1111
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings.
1212
# URISchemes: http, https
@@ -47,7 +47,7 @@ Lint/UselessOr:
4747
Exclude:
4848
- 'app/lib/routes_helper/api_matcher.rb'
4949

50-
# Offense count: 923
50+
# Offense count: 928
5151
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
5252
Metrics/AbcSize:
5353
Max: 255
@@ -62,7 +62,7 @@ Metrics/BlockNesting:
6262
- 'app/models/bs_request_action.rb'
6363
- 'lib/xpath_engine.rb'
6464

65-
# Offense count: 86
65+
# Offense count: 87
6666
# Configuration parameters: CountComments, Max, CountAsOne.
6767
Metrics/ClassLength:
6868
Exclude:
@@ -89,6 +89,7 @@ Metrics/ClassLength:
8989
- 'app/controllers/webui/search_controller.rb'
9090
- 'app/controllers/webui/staging/workflows_controller.rb'
9191
- 'app/controllers/webui/users/notifications_controller.rb'
92+
- 'app/controllers/webui/users/tokens_controller.rb'
9293
- 'app/controllers/webui/users_controller.rb'
9394
- 'app/controllers/webui/webui_controller.rb'
9495
- 'app/lib/backend/connection.rb'
@@ -244,7 +245,7 @@ Metrics/CyclomaticComplexity:
244245
- 'test/functional/zzz_post_consistency_test.rb'
245246
- 'test/node_matcher.rb'
246247

247-
# Offense count: 1004
248+
# Offense count: 1007
248249
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
249250
Metrics/MethodLength:
250251
Max: 218
@@ -452,13 +453,13 @@ RSpec/AnyInstance:
452453
- 'spec/models/package_spec.rb'
453454
- 'spec/models/project_spec.rb'
454455

455-
# Offense count: 1245
456+
# Offense count: 1236
456457
# Configuration parameters: Prefixes, AllowedPatterns.
457458
# Prefixes: when, with, without
458459
RSpec/ContextWording:
459460
Enabled: false
460461

461-
# Offense count: 404
462+
# Offense count: 405
462463
# This cop supports unsafe autocorrection (--autocorrect-all).
463464
# Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants.
464465
# SupportedStyles: described_class, explicit
@@ -566,16 +567,11 @@ RSpec/IndexedLet:
566567
- 'spec/shared/contexts/a_user_and_subscriptions.rb'
567568
- 'spec/shared/contexts/a_user_and_subscriptions_with_defaults.rb'
568569

569-
# Offense count: 1728
570+
# Offense count: 1715
570571
# Configuration parameters: AllowSubject.
571572
RSpec/MultipleMemoizedHelpers:
572573
Max: 28
573574

574-
# Offense count: 1
575-
RSpec/SpecFilePathSuffix:
576-
Exclude:
577-
- 'spec/models/package_datatable_spec_backup.rb'
578-
579575
# Offense count: 1
580576
# This cop supports unsafe autocorrection (--autocorrect-all).
581577
Rails/ApplicationController:
@@ -733,7 +729,7 @@ Rails/TimeZone:
733729
- 'test/unit/binary_release_test.rb'
734730
- 'test/unit/package_remove_test.rb'
735731

736-
# Offense count: 160
732+
# Offense count: 161
737733
# This cop supports unsafe autocorrection (--autocorrect-all).
738734
# Configuration parameters: EnforcedStyle, EnforcedStyleForClasses, EnforcedStyleForModules.
739735
# SupportedStyles: nested, compact
@@ -766,7 +762,7 @@ Style/ConditionalAssignment:
766762
Exclude:
767763
- 'app/models/bs_request_action_submit.rb'
768764

769-
# Offense count: 1165
765+
# Offense count: 1169
770766
# Configuration parameters: AllowedConstants.
771767
Style/Documentation:
772768
Enabled: false
@@ -779,7 +775,7 @@ Style/FileOpen:
779775
- 'script/start_test_backend'
780776
- 'test/test_helper.rb'
781777

782-
# Offense count: 1928
778+
# Offense count: 1933
783779
# This cop supports unsafe autocorrection (--autocorrect-all).
784780
# Configuration parameters: EnforcedStyle.
785781
# SupportedStyles: always, always_true, never

src/api/app/controllers/person/token_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def update
4646
xml_attributes = xml.xpath('/token').first.to_h.slice('enabled', 'description', 'scm_token', 'workflow_configuration_path', 'workflow_configuration_url')
4747

4848
token = @user.tokens.find(params[:id])
49+
xml_attributes['reason'] = "Disabled by #{User.session.login}." if token.is_a?(Token::Workflow)
4950
if token.update(xml_attributes)
5051
render_ok
5152
else

src/api/app/controllers/webui/users/tokens_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def update
5151
end
5252
end
5353
format.html do
54-
if @token.update(update_parameters)
54+
if @token.update(update_parameters.merge({ reason: "Disabled by #{User.session.login}." }))
5555
flash[:success] = 'Token successfully updated'
5656
else
5757
flash[:error] = "Failed to update Token: #{@token.errors.full_messages.to_sentence}"

src/api/app/models/event/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def notification_events
2222
Event::CommentForRequest, Event::CommentForReport,
2323
Event::RelationshipCreate, Event::RelationshipDelete,
2424
Event::Report, Event::Decision, Event::AppealCreated,
25-
Event::WorkflowRunFail,
25+
Event::WorkflowRunFail, Event::TokenDisabled,
2626
Event::AddedUserToGroup, Event::RemovedUserFromGroup,
2727
Event::Assignment, Event::UpstreamPackageVersionChanged]
2828
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Event
2+
class TokenDisabled < Base
3+
self.description = 'Workflow token disabled'
4+
payload_keys :id, :token_id, :reason
5+
6+
receiver_roles :token_executor, :token_member
7+
delegate :members, to: :token, prefix: true
8+
9+
self.notification_explanation = 'Receive a notification when a workflow token is disabled.'
10+
11+
def subject
12+
"Workflow Token '#{token.description.presence || token.id}' was disabled"
13+
end
14+
15+
def token_executors
16+
[token&.executor].compact
17+
end
18+
19+
def parameters_for_notification
20+
super.merge(notifiable_type: 'WorkflowRun', notifiable_id: payload['id'], type: 'NotificationWorkflowRun')
21+
end
22+
23+
def event_object
24+
WorkflowRun.find(payload['id'])
25+
end
26+
27+
private
28+
29+
def token
30+
Token.find(payload['token_id'])
31+
end
32+
end
33+
end
34+
35+
# == Schema Information
36+
#
37+
# Table name: events
38+
#
39+
# id :bigint not null, primary key
40+
# eventtype :string(255) not null, indexed
41+
# mails_sent :boolean default(FALSE), indexed
42+
# payload :text(16777215)
43+
# undone_jobs :integer default(0)
44+
# created_at :datetime indexed
45+
# updated_at :datetime
46+
#
47+
# Indexes
48+
#
49+
# index_events_on_created_at (created_at)
50+
# index_events_on_eventtype (eventtype)
51+
# index_events_on_mails_sent (mails_sent)
52+
#

src/api/app/models/event_subscription/for_channel_form.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class ForChannelForm
77
'Event::WorkflowRunFail', 'Event::AppealCreated',
88
'Event::FavoredDecision', 'Event::ClearedDecision',
99
'Event::AddedUserToGroup', 'Event::RemovedUserFromGroup',
10-
'Event::Assignment', 'Event::UpstreamPackageVersionChanged'].freeze
10+
'Event::Assignment', 'Event::UpstreamPackageVersionChanged',
11+
'Event::TokenDisabled'].freeze
1112

1213
attr_reader :name, :subscription
1314

src/api/app/models/notification_workflow_run.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
class NotificationWorkflowRun < Notification
22
def description
3-
''
3+
case event_type
4+
when 'Event::TokenDisabled'
5+
event_payload['reason']
6+
else
7+
''
8+
end
49
end
510

611
def excerpt
@@ -12,13 +17,24 @@ def avatar_objects
1217
end
1318

1419
def link_text
15-
'Workflow Run'
20+
case event_type
21+
when 'Event::TokenDisabled'
22+
token = Token.find(event_payload['token_id'])
23+
"Token '#{token.description.presence || token.id}' was disabled"
24+
else
25+
'Workflow Run'
26+
end
1627
end
1728

1829
def link_path
1930
return if notifiable.blank?
2031

21-
Rails.application.routes.url_helpers.token_workflow_run_path(notifiable.token_id, notifiable.id, notification_id: id)
32+
case event_type
33+
when 'Event::TokenDisabled'
34+
Rails.application.routes.url_helpers.token_path(notifiable.token_id, notification_id: id)
35+
else
36+
Rails.application.routes.url_helpers.token_workflow_run_path(notifiable.token_id, notifiable.id, notification_id: id)
37+
end
2238
end
2339
end
2440

src/api/app/models/token/workflow.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ class Token::Workflow < Token
1515
dependent: :destroy,
1616
inverse_of: :groups
1717

18+
attr_writer :reason
19+
1820
validates :scm_token, presence: true
1921
# Either a url referring to the worklflow configuration file or a filepath to the config inside the
2022
# SCM repository has to be provided
2123
validates :workflow_configuration_path, presence: true, unless: -> { workflow_configuration_url.present? }
2224
validates :workflow_configuration_url, presence: true, unless: -> { workflow_configuration_path.present? }
2325

26+
after_save :disabled_event, if: -> { enabled_previously_changed?(from: true, to: false) }
27+
2428
def call(options)
2529
set_triggered_at
2630
workflow_run = options[:workflow_run]
@@ -81,6 +85,10 @@ def validation_errors
8185
error_messages.flatten
8286
end
8387
end
88+
89+
def disabled_event
90+
Event::TokenDisabled.create(id: workflow_runs.last&.id, token_id: id, reason: @reason)
91+
end
8492
end
8593

8694
# == Schema Information

src/api/app/models/workflow_run.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def update_as_failed(message)
8383
# "Failed to report back to GitHub: Unauthorized request. Please check your credentials again."
8484
# "Failed to report back to GitHub: Request is forbidden."
8585

86-
token.update(enabled: false) if message.include?('Unauthorized request') || /Request (is )?forbidden/.match?(message)
86+
return unless message.include?('Unauthorized request') || /Request (is )?forbidden/.match?(message)
87+
88+
token.update(enabled: false, reason: "Authentication to #{scm_vendor.titleize} failed.")
8789
end
8890

8991
# Stores debug info to help figure out what went wrong when trying to save a Status in the SCM.

src/api/app/services/notification_service/notifier.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class Notifier
2323
'Event::RemovedUserFromGroup',
2424
'Event::AssignmentCreate',
2525
'Event::AssignmentDelete',
26-
'Event::UpstreamPackageVersionChanged'].freeze
26+
'Event::UpstreamPackageVersionChanged',
27+
'Event::TokenDisabled'].freeze
2728
CHANNELS = %i[web rss].freeze
2829
ALLOWED_NOTIFIABLE_TYPES = {
2930
'BsRequest' => ::BsRequest,
@@ -50,7 +51,8 @@ class Notifier
5051
'Event::FavoredDecision',
5152
'Event::WorkflowRunFail',
5253
'Event::AddedUserToGroup',
53-
'Event::RemovedUserFromGroup'].freeze
54+
'Event::RemovedUserFromGroup',
55+
'Event::TokenDisabled'].freeze
5456

5557
def initialize(event)
5658
@event = event

0 commit comments

Comments
 (0)