Skip to content

Commit 4e4020e

Browse files
Merge pull request #8354 from opf/fix/no_mails_when_seeding
fix/no mails when seeding [ci skip]
2 parents 184d255 + 3ccfb86 commit 4e4020e

File tree

20 files changed

+273
-157
lines changed

20 files changed

+273
-157
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#-- encoding: UTF-8
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) 2012-2020 the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2017 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See docs/COPYRIGHT.rdoc for more details.
29+
#++
30+
31+
class Journal::NotificationConfiguration
32+
class << self
33+
# Allows controlling whether notifications are sent out for created journals.
34+
# After the block is executed, the setting is returned to its original state which is true by default.
35+
# In case the method is called multiple times within itself, the first setting prevails.
36+
# This allows to control the setting globally without having to pass the setting down the call stack in
37+
# order to ensure all subsequent code follows the provided setting.
38+
def with(send_notifications, &block)
39+
if already_set?
40+
log_warning(send_notifications)
41+
yield
42+
else
43+
with_first(send_notifications, &block)
44+
end
45+
end
46+
47+
def active?
48+
active.value
49+
end
50+
51+
protected
52+
53+
def with_first(send_notifications)
54+
old_value = active?
55+
self.already_set = true
56+
57+
self.active = send_notifications
58+
59+
yield
60+
ensure
61+
self.active = old_value
62+
self.already_set = false
63+
end
64+
65+
def log_warning(send_notifications)
66+
return if active == send_notifications
67+
68+
message = <<~MSG
69+
Ignoring setting journal notifications to '#{send_notifications}' as a parent block already set it to #{active}"
70+
MSG
71+
Rails.logger.debug message
72+
end
73+
74+
def active
75+
@active ||= Concurrent::ThreadLocalVar.new(true)
76+
end
77+
78+
def already_set
79+
@already_set ||= Concurrent::ThreadLocalVar.new(false)
80+
end
81+
82+
def already_set?
83+
already_set.value
84+
end
85+
86+
def active=(value)
87+
@active.value = value
88+
end
89+
90+
def already_set=(value)
91+
@already_set.value = value
92+
end
93+
end
94+
end

app/models/journal_manager.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
class JournalManager
3131
class << self
32-
attr_accessor :send_notification
33-
3432
def changes_on_association(current, predecessor, association, key, value)
3533
merged_journals = merge_reference_journals_by_id(current, predecessor, key.to_s, value.to_s)
3634

@@ -41,10 +39,6 @@ def changes_on_association(current, predecessor, association, key, value)
4139
to_changes_format(changes, association.to_s)
4240
end
4341

44-
def reset_notification
45-
@send_notification = true
46-
end
47-
4842
private
4943

5044
def merge_reference_journals_by_id(new_journals, old_journals, id_key, value)
@@ -152,8 +146,6 @@ def select_and_combine(journals, id, key, value)
152146
end
153147
end
154148

155-
self.send_notification = true
156-
157149
def self.journalized?(obj)
158150
not obj.nil? and obj.respond_to? :journals
159151
end
@@ -349,16 +341,4 @@ def self.normalize_newlines(data)
349341
h[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n") : e[1])
350342
end
351343
end
352-
353-
def self.with_send_notifications(send_notifications, &block)
354-
old_value = send_notification
355-
356-
self.send_notification = send_notifications
357-
358-
result = block.call
359-
ensure
360-
self.send_notification = old_value
361-
362-
result
363-
end
364344
end

app/seeders/demo_data/version_builder.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def set_wiki!(version, config)
7171
page = WikiPage.create! wiki: version.project.wiki, title: version.wiki_page_title
7272

7373
content = with_references config[:content], project
74-
WikiContent.create! page: page, author: User.admin.first, text: content
74+
Journal::NotificationConfiguration.with false do
75+
WikiContent.create! page: page, author: User.admin.first, text: content
76+
end
7577

7678
version.save!
7779
end

app/seeders/demo_data/work_package_board_seeder.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ def seed_basic_board_queries
161161
query.save!
162162
end
163163
end
164-
165164
end
166165

167166
def scrum_query_work_packages

app/seeders/demo_data/work_package_seeder.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#-- encoding: UTF-8
2+
23
#-- copyright
34
# OpenProject is an open source project management software.
45
# Copyright (C) 2012-2020 the OpenProject GmbH
@@ -117,7 +118,7 @@ def base_work_package_attributes(attributes)
117118

118119
def find_principal(name)
119120
if name
120-
group_assignee = Group.find_by(lastname: name)
121+
group_assignee = Group.find_by(lastname: name)
121122
return group_assignee unless group_assignee.nil?
122123
end
123124

@@ -175,7 +176,7 @@ def create_attachments!(work_package, attributes)
175176
end
176177

177178
def set_workpackage_relations
178-
work_packages_data = project_data_for(key, 'work_packages')
179+
work_packages_data = project_data_for(key, 'work_packages')
179180

180181
work_packages_data.each do |attributes|
181182
create_relations attributes

app/seeders/demo_data_seeder.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#-- encoding: UTF-8
2+
23
#-- copyright
34
# OpenProject is an open source project management software.
45
# Copyright (C) 2012-2020 the OpenProject GmbH

app/seeders/seeder.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#-- encoding: UTF-8
2+
23
#-- copyright
34
# OpenProject is an open source project management software.
45
# Copyright (C) 2012-2020 the OpenProject GmbH
@@ -30,7 +31,9 @@
3031
class Seeder
3132
def seed!
3233
if applicable?
33-
seed_data!
34+
without_notifications do
35+
seed_data!
36+
end
3437
else
3538
puts " *** #{not_applicable_message}"
3639
end
@@ -76,4 +79,8 @@ def project_data_for(project, key)
7679
def project_has_data_for?(project, key)
7780
I18n.exists?("seeders.#{OpenProject::Configuration['edition']}.demo_data.projects.#{project}.#{key}")
7881
end
82+
83+
def without_notifications(&block)
84+
Journal::NotificationConfiguration.with(false, &block)
85+
end
7986
end

app/services/add_work_package_note_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def initialize(user:, work_package:)
4242
end
4343

4444
def call(notes, send_notifications: true)
45-
JournalManager.with_send_notifications send_notifications do
45+
Journal::NotificationConfiguration.with send_notifications do
4646
work_package.add_journal(user, notes)
4747

4848
success, errors = validate_and_yield(work_package, user) do

app/services/shared/service_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def in_user_context(send_notifications = true)
5151

5252
ActiveRecord::Base.transaction do
5353
User.execute_as user do
54-
JournalManager.with_send_notifications(send_notifications) do
54+
Journal::NotificationConfiguration.with(send_notifications) do
5555
result = yield
5656

5757
if result.failure?

app/services/work_packages/copy_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def initialize(user:, work_package:, contract_class: WorkPackages::CreateContrac
4343
end
4444

4545
def call(send_notifications: true, **attributes)
46-
in_context(work_package) do
46+
in_context(work_package, send_notifications) do
4747
copy(attributes, send_notifications)
4848
end
4949
end

0 commit comments

Comments
 (0)