Commit aec92d16 authored by Mark Chao's avatar Mark Chao

Add new_epic notification_setting

Add email_events to replace EMAIL_EVENTS because it needs to be dynamic,
allowing override for EE group to add new_epic event.
parent c59000a7
......@@ -5,14 +5,14 @@ class NotificationSettingsController < ApplicationController
return render_404 unless can_read?(resource)
@notification_setting = current_user.notification_settings_for(resource)
@saved = @notification_setting.update(notification_setting_params)
@saved = @notification_setting.update(notification_setting_params_for(resource))
render_response
end
def update
@notification_setting = current_user.notification_settings.find(params[:id])
@saved = @notification_setting.update(notification_setting_params)
@saved = @notification_setting.update(notification_setting_params_for(@notification_setting.source))
render_response
end
......@@ -42,8 +42,8 @@ class NotificationSettingsController < ApplicationController
}
end
def notification_setting_params
allowed_fields = NotificationSetting::EMAIL_EVENTS.dup
def notification_setting_params_for(source)
allowed_fields = NotificationSetting.email_events(source).dup
allowed_fields << :level
params.require(:notification_setting).permit(allowed_fields)
end
......
......@@ -83,7 +83,7 @@ module NotificationsHelper
end
def notification_event_name(event)
# All values from NotificationSetting::EMAIL_EVENTS
# All values from NotificationSetting.email_events
case event
when :success_pipeline
s_('NotificationEvent|Successful pipeline')
......
# frozen_string_literal: true
class NotificationSetting < ActiveRecord::Base
prepend EE::NotificationSetting
include IgnorableColumn
ignore_column :events
......@@ -45,6 +46,14 @@ class NotificationSetting < ActiveRecord::Base
:success_pipeline
].freeze
def self.email_events(source = nil)
EMAIL_EVENTS
end
def email_events
self.class.email_events(source)
end
EXCLUDED_PARTICIPATING_EVENTS = [
:success_pipeline
].freeze
......
......@@ -279,7 +279,7 @@ module NotificationRecipientService
end
# Build event key to search on custom notification level
# Check NotificationSetting::EMAIL_EVENTS
# Check NotificationSetting.email_events
def custom_action
@custom_action ||= "#{action}_#{target.class.model_name.name.underscore}".to_sym
end
......
......@@ -19,7 +19,7 @@
- paragraph = _('Custom notification levels are the same as participating levels. With custom notification levels you will also receive notifications for select events. To find out more, check out %{notification_link}.') % { notification_link: notification_link.html_safe }
#{ paragraph.html_safe }
.col-lg-8
- NotificationSetting::EMAIL_EVENTS.each_with_index do |event, index|
- notification_setting.email_events.each_with_index do |event, index|
- field_id = "#{notifications_menu_identifier("modal", notification_setting)}_notification_setting[#{event}]"
.form-group
.form-check{ class: ("prepend-top-0" if index == 0) }
......
......@@ -1866,6 +1866,7 @@ ActiveRecord::Schema.define(version: 20180807153545) do
t.boolean "success_pipeline"
t.boolean "push_to_merge_request"
t.boolean "issue_due"
t.boolean "new_epic"
end
add_index "notification_settings", ["source_id", "source_type"], name: "index_notification_settings_on_source_id_and_source_type", using: :btree
......
......@@ -15,7 +15,7 @@ mention
custom
```
If the `custom` level is used, specific email events can be controlled. Notification email events are defined in the `NotificationSetting::EMAIL_EVENTS` model variable. Currently, these events are recognized:
If the `custom` level is used, specific email events can be controlled. Available events are returned by `NotificationSetting.email_events`. Currently, these events are recognized:
```
new_note
......@@ -32,6 +32,7 @@ reassign_merge_request
merge_merge_request
failed_pipeline
success_pipeline
new_epic
```
## Global notification settings
......@@ -85,6 +86,7 @@ curl --request PUT --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab
| `merge_merge_request` | boolean | no | Enable/disable this notification |
| `failed_pipeline` | boolean | no | Enable/disable this notification |
| `success_pipeline` | boolean | no | Enable/disable this notification |
| `new_epic` | boolean | no | Enable/disable this notification ([Introduced][ee-6626] in 11.2) |
Example response:
......@@ -153,6 +155,7 @@ curl --request PUT --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab
| `merge_merge_request` | boolean | no | Enable/disable this notification |
| `failed_pipeline` | boolean | no | Enable/disable this notification |
| `success_pipeline` | boolean | no | Enable/disable this notification |
| `new_epic` | boolean | no | Enable/disable this notification ([Introduced][ee-6626] in 11.2) |
Example responses:
......@@ -177,9 +180,11 @@ Example responses:
"reassign_merge_request": false,
"merge_merge_request": false,
"failed_pipeline": false,
"success_pipeline": false
"success_pipeline": false,
"new_epic": false
}
}
```
[ce-5632]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5632
[ee-6626]: https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/6626
# frozen_string_literal: true
module EE
module NotificationSetting
extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
override :email_events
def email_events(source = nil)
result = super.dup
case target
when Group, :group
result << :new_epic
end
result
end
end
end
end
---
title: Allow custom notification for new epic event
merge_request: 5863
author:
type: added
# frozen_string_literal: true
class AddNewEpicToNotificationSettings < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
add_column :notification_settings, :new_epic, :boolean
end
end
# frozen_string_literal: true
require 'spec_helper'
describe NotificationSetting do
describe '.email_events' do
subject { described_class.email_events(target) }
context 'group' do
let(:target) { build_stubbed(:group) }
it 'appends EE specific events' do
expect(subject).to eq(
[
:new_note,
:new_issue,
:reopen_issue,
:close_issue,
:reassign_issue,
:issue_due,
:new_merge_request,
:push_to_merge_request,
:reopen_merge_request,
:close_merge_request,
:reassign_merge_request,
:merge_merge_request,
:failed_pipeline,
:success_pipeline,
:new_epic
]
)
end
end
context 'project' do
let(:target) { build_stubbed(:project) }
it 'returns CE list' do
expect(subject).to eq(
[
:new_note,
:new_issue,
:reopen_issue,
:close_issue,
:reassign_issue,
:issue_due,
:new_merge_request,
:push_to_merge_request,
:reopen_merge_request,
:close_merge_request,
:reassign_merge_request,
:merge_merge_request,
:failed_pipeline,
:success_pipeline
]
)
end
end
end
end
......@@ -884,7 +884,7 @@ module API
class NotificationSetting < Grape::Entity
expose :level
expose :events, if: ->(notification_setting, _) { notification_setting.custom? } do
::NotificationSetting::EMAIL_EVENTS.each do |event|
::NotificationSetting.email_events.each do |event|
expose event
end
end
......
......@@ -23,7 +23,7 @@ module API
params do
optional :level, type: String, desc: 'The global notification level'
optional :notification_email, type: String, desc: 'The email address to send notifications'
NotificationSetting::EMAIL_EVENTS.each do |event|
NotificationSetting.email_events.each do |event|
optional event, type: Boolean, desc: 'Enable/disable this notification'
end
end
......@@ -73,7 +73,7 @@ module API
end
params do
optional :level, type: String, desc: "The #{source_type} notification level"
NotificationSetting::EMAIL_EVENTS.each do |event|
NotificationSetting.email_events(source_type.to_sym).each do |event|
optional event, type: Boolean, desc: 'Enable/disable this notification'
end
end
......
......@@ -21,10 +21,11 @@ describe NotificationSettingsController do
end
context 'when authorized' do
let(:notification_setting) { user.notification_settings_for(source) }
let(:custom_events) do
events = {}
NotificationSetting::EMAIL_EVENTS.each do |event|
NotificationSetting.email_events(source).each do |event|
events[event.to_s] = true
end
......@@ -36,7 +37,7 @@ describe NotificationSettingsController do
end
context 'for projects' do
let(:notification_setting) { user.notification_settings_for(project) }
let(:source) { project }
it 'creates notification setting' do
post :create,
......@@ -67,7 +68,7 @@ describe NotificationSettingsController do
end
context 'for groups' do
let(:notification_setting) { user.notification_settings_for(group) }
let(:source) { group }
it 'creates notification setting' do
post :create,
......@@ -145,7 +146,7 @@ describe NotificationSettingsController do
let(:custom_events) do
events = {}
NotificationSetting::EMAIL_EVENTS.each do |event|
notification_setting.email_events.each do |event|
events[event] = "true"
end
end
......
......@@ -94,9 +94,39 @@ RSpec.describe NotificationSetting do
end
end
context 'email events' do
it 'includes EXCLUDED_WATCHER_EVENTS in EMAIL_EVENTS' do
expect(described_class::EMAIL_EVENTS).to include(*described_class::EXCLUDED_WATCHER_EVENTS)
describe '.email_events' do
subject { described_class.email_events }
it 'returns email events' do
expect(subject).to include(
:new_note,
:new_issue,
:reopen_issue,
:close_issue,
:reassign_issue,
:new_merge_request,
:reopen_merge_request,
:close_merge_request,
:reassign_merge_request,
:merge_merge_request,
:failed_pipeline,
:success_pipeline
)
end
it 'includes EXCLUDED_WATCHER_EVENTS' do
expect(subject).to include(*described_class::EXCLUDED_WATCHER_EVENTS)
end
end
describe '#email_events' do
let(:source) { build(:group) }
subject { build(:notification_setting, source: source) }
it 'calls email_events' do
expect(described_class).to receive(:email_events).with(source)
subject.email_events
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment