-
Notifications
You must be signed in to change notification settings - Fork 464
Add token circuit breaker notification (issue #18886) #18954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||
| module Event | ||||||
| class TokenDisabled < Base | ||||||
| self.description = 'SCM/CI Token disabled due to authorization failure' | ||||||
| payload_keys :id, :token_id, :scm_vendor, :summary, :token_description | ||||||
|
|
||||||
| receiver_roles :token_executor, :token_member | ||||||
| delegate :members, to: :token, prefix: true | ||||||
|
|
||||||
| self.notification_explanation = 'Receive notifications when an SCM/CI integration token is disabled due to authorization problems.' | ||||||
|
|
||||||
| # Example of subject: | ||||||
| # GitHub workflow token disabled | ||||||
| def subject | ||||||
| vendor_map = { 'github' => 'GitHub', 'gitlab' => 'GitLab', 'gitea' => 'Gitea' } | ||||||
| vendor = vendor_map[payload['scm_vendor']] || payload['scm_vendor']&.capitalize || 'SCM' | ||||||
| "#{vendor} workflow token disabled" | ||||||
| end | ||||||
|
|
||||||
| def token_executors | ||||||
| [token&.executor].compact | ||||||
| end | ||||||
|
|
||||||
| def parameters_for_notification | ||||||
| super.merge(notifiable_type: 'Token::Workflow', notifiable_id: payload['token_id'], type: 'NotificationToken') | ||||||
| end | ||||||
|
|
||||||
| def event_object | ||||||
| Token.find_by(id: payload['token_id']) | ||||||
|
||||||
| Token.find_by(id: payload['token_id']) | |
| token |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| class NotificationToken < Notification | ||
| def description | ||
| "Token #{notifiable.description.presence || ''} was disabled".strip | ||
| end | ||
|
|
||
| def excerpt | ||
| event_payload['summary'] || 'Token was disabled due to authorization failure' | ||
| end | ||
|
|
||
| def avatar_objects | ||
| [notifiable&.executor].compact | ||
| end | ||
|
|
||
| def link_text | ||
| 'Token' | ||
| end | ||
|
|
||
| def link_path | ||
| return if notifiable.blank? | ||
|
|
||
| Rails.application.routes.url_helpers.token_path(notifiable) | ||
| end | ||
| end | ||
|
|
||
| # == Schema Information | ||
| # | ||
| # Table name: notifications | ||
| # | ||
| # id :bigint not null, primary key | ||
| # bs_request_oldstate :string(255) | ||
| # bs_request_state :string(255) | ||
| # delivered :boolean default(FALSE), indexed | ||
| # event_payload :text(16777215) not null | ||
| # event_type :string(255) not null, indexed | ||
| # last_seen_at :datetime | ||
| # notifiable_type :string(255) indexed => [notifiable_id] | ||
| # rss :boolean default(FALSE), indexed | ||
| # subscriber_type :string(255) indexed => [subscriber_id] | ||
| # subscription_receiver_role :string(255) not null | ||
| # title :string(255) | ||
| # type :string(255) indexed | ||
| # web :boolean default(FALSE), indexed | ||
| # created_at :datetime not null, indexed | ||
| # updated_at :datetime not null | ||
| # notifiable_id :integer indexed => [notifiable_type] | ||
| # subscriber_id :integer indexed => [subscriber_type] | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_notifications_on_created_at (created_at) | ||
| # index_notifications_on_delivered (delivered) | ||
| # index_notifications_on_event_type (event_type) | ||
| # index_notifications_on_notifiable_type_and_notifiable_id (notifiable_type,notifiable_id) | ||
| # index_notifications_on_rss (rss) | ||
| # index_notifications_on_subscriber_type_and_subscriber_id (subscriber_type,subscriber_id) | ||
| # index_notifications_on_type (type) | ||
| # index_notifications_on_web (web) | ||
| # |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class CreateDefaultTokenDisabledSubscriptions < ActiveRecord::Migration[7.2] | ||
| def up | ||
| # Create default subscriptions for Event::TokenDisabled | ||
| # This event is triggered when a workflow token is disabled due to authorization failures | ||
| create_default_subscription('token_executor', :instant_email) | ||
| create_default_subscription('token_executor', :web) | ||
| create_default_subscription('token_member', :instant_email) | ||
| create_default_subscription('token_member', :web) | ||
| end | ||
|
|
||
| def down | ||
| raise ActiveRecord::IrreversibleMigration | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_default_subscription(receiver_role, channel) | ||
| EventSubscription.find_or_create_by!( | ||
| eventtype: 'Event::TokenDisabled', | ||
| receiver_role: receiver_role, | ||
| channel: channel, | ||
| user_id: nil, | ||
| group_id: nil | ||
| ) do |subscription| | ||
| subscription.enabled = true | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| RSpec.describe Event::TokenDisabled do | ||
| describe '#token_executors' do | ||
| subject { event.token_executors } | ||
|
|
||
| let(:token) { create(:workflow_token) } | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: 'github', | ||
| summary: 'Failed to report back to GitHub: Unauthorized request.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| it { expect(subject).to contain_exactly(token.executor) } | ||
|
|
||
| context 'when the token does not exist' do | ||
| before do | ||
| event | ||
| token.destroy | ||
| end | ||
|
|
||
| it { expect(subject).to be_empty } | ||
| end | ||
| end | ||
|
Comment on lines
+1
to
+25
|
||
|
|
||
| describe '#subject' do | ||
| subject { event.subject } | ||
|
|
||
| let(:token) { create(:workflow_token) } | ||
|
|
||
| context 'with GitHub vendor' do | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: 'github', | ||
| summary: 'Failed to report back to GitHub: Unauthorized request.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| it { expect(subject).to eq('GitHub workflow token disabled') } | ||
| end | ||
|
|
||
| context 'with GitLab vendor' do | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: 'gitlab', | ||
| summary: 'Failed to report back to GitLab: Request forbidden.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| it { expect(subject).to eq('GitLab workflow token disabled') } | ||
| end | ||
|
|
||
| context 'with nil vendor' do | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: nil, | ||
| summary: 'Failed to report back.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| it { expect(subject).to eq('SCM workflow token disabled') } | ||
| end | ||
|
|
||
| context 'with unknown vendor' do | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: 'bitbucket', | ||
| summary: 'Failed to report back to Bitbucket.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| it { expect(subject).to eq('Bitbucket workflow token disabled') } | ||
| end | ||
| end | ||
|
|
||
| describe '#parameters_for_notification' do | ||
| subject { event.parameters_for_notification } | ||
|
|
||
| let(:token) { create(:workflow_token) } | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: 'github', | ||
| summary: 'Failed to report back to GitHub: Unauthorized request.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| it 'includes the correct notifiable type' do | ||
| expect(subject[:notifiable_type]).to eq('Token::Workflow') | ||
| end | ||
|
|
||
| it 'includes the correct notifiable id' do | ||
| expect(subject[:notifiable_id]).to eq(token.id) | ||
| end | ||
|
|
||
| it 'includes the correct notification type' do | ||
| expect(subject[:type]).to eq('NotificationToken') | ||
| end | ||
| end | ||
|
|
||
| describe '#event_object' do | ||
| subject { event.event_object } | ||
|
|
||
| let(:token) { create(:workflow_token) } | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: 'github', | ||
| summary: 'Failed to report back to GitHub: Unauthorized request.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| it { expect(subject).to eq(token) } | ||
|
|
||
| context 'when the token does not exist' do | ||
| before do | ||
| event | ||
| token.destroy | ||
| end | ||
|
|
||
| it { expect(subject).to be_nil } | ||
| end | ||
| end | ||
|
|
||
| describe '#token_members' do | ||
| subject { event.token_members } | ||
|
|
||
| let(:executor) { create(:confirmed_user) } | ||
| let(:member) { create(:confirmed_user) } | ||
| let(:token) { create(:workflow_token, executor: executor) } | ||
| let(:event) do | ||
| Event::TokenDisabled.create( | ||
| token_id: token.id, | ||
| scm_vendor: 'github', | ||
| summary: 'Failed to report back to GitHub: Unauthorized request.', | ||
| token_description: 'My workflow token' | ||
| ) | ||
| end | ||
|
|
||
| before do | ||
| token.users << member | ||
| end | ||
|
|
||
| it 'returns all members including shared users' do | ||
| expect(subject).to include(member) | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geetxnshgoyal here you add the parameters for the
type: 'NotificationToken, but there is no corresponding model for this notification type. You need to add a model for it, similar to https://github.com/openSUSE/open-build-service/blob/master/src/api/app/models/notification_workflow_run.rbThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@krauselukas thanks for telling that! I've now added the NotificationToken model in [notification_token.rb] with all the required methods also i filled the data migration to create the default subscriptions the model should now handle notification rendering properly!!