]> source.dussan.org Git - redmine.git/commitdiff
Auto watch issues on issue creation (#38238).
authorGo MAEDA <maeda@farend.jp>
Sun, 26 Feb 2023 07:46:35 +0000 (07:46 +0000)
committerGo MAEDA <maeda@farend.jp>
Sun, 26 Feb 2023 07:46:35 +0000 (07:46 +0000)
Patch by Felix Schäfer.

git-svn-id: https://svn.redmine.org/redmine/trunk@22115 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
app/models/user_preference.rb
config/locales/en.yml
test/unit/issue_test.rb
test/unit/user_preference_test.rb

index 6451146179850331e1f7e43cb4011e48376ee961..4edf4fa1b814fd0b00beee2eab6cba81a3a8ea68 100644 (file)
@@ -121,7 +121,11 @@ class Issue < ActiveRecord::Base
   # Should be after_create but would be called before previous after_save callbacks
   after_save :after_create_from_copy, :create_parent_issue_journal
   after_destroy :update_parent_attributes, :create_parent_issue_journal
+  # add_auto_watcher needs to run before sending notifications, thus it needs
+  # to be added after send_notification (after_ callbacks are run in inverse order)
+  # https://api.rubyonrails.org/v5.2.3/classes/ActiveSupport/Callbacks/ClassMethods.html#method-i-set_callback
   after_create_commit :send_notification
+  after_create_commit :add_auto_watcher
 
   # Returns a SQL conditions string used to find all issues visible by the specified user
   def self.visible_condition(user, options={})
@@ -2020,6 +2024,15 @@ class Issue < ActiveRecord::Base
     end
   end
 
+  def add_auto_watcher
+    if author &&
+        author.allowed_to?(:add_issue_watchers, project) &&
+        author.pref.auto_watch_on?('issue_created') &&
+        self.watcher_user_ids.exclude?(author.id)
+      self.set_watcher(author, true)
+    end
+  end
+
   def send_notification
     if notify? && Setting.notified_events.include?('issue_added')
       Mailer.deliver_issue_add(self)
index 61c70909332c9da19d13a9bf3e05d21a2eed0401..21992f94540fccdeb6712873a7456ea43ab514cd 100644 (file)
@@ -44,7 +44,7 @@ class UserPreference < ActiveRecord::Base
 
   TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
   DEFAULT_TOOLBAR_LANGUAGE_OPTIONS = %w[c cpp csharp css diff go groovy html java javascript objc perl php python r ruby sass scala shell sql swift xml yaml]
-  AUTO_WATCH_ON_OPTIONS = ['issue_contributed_to']
+  AUTO_WATCH_ON_OPTIONS = %w[issue_created issue_contributed_to]
 
   def initialize(attributes=nil, *args)
     super
index 8713f31df0cdb094a5d3eebeb4cf39c67a5ed150..2e1378ff7b463baead645d1f917d9016c6477597 100644 (file)
@@ -961,6 +961,7 @@ en:
   label_optional_description: Optional description
   label_add_another_file: Add another file
   label_auto_watch_on: Auto watch
+  label_auto_watch_on_issue_created: Issues I created
   label_auto_watch_on_issue_contributed_to: Issues I contributed to
   label_preferences: Preferences
   label_chronological_order: In chronological order
index 14dca00e363fc3004c5d47ed7fabc771b0b7abd6..5795d2bb1af262e8b781dadabf2b88e106e4a3cc 100644 (file)
@@ -3445,6 +3445,40 @@ class IssueTest < ActiveSupport::TestCase
     assert_equal [5], issue2.filter_projects_scope('').ids.sort
   end
 
+  def test_create_should_add_watcher
+    user = User.first
+    user.pref.auto_watch_on=['issue_created']
+    user.pref.save
+    issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher')
+
+    assert_difference 'Watcher.count', 1 do
+      assert_equal true, issue.save
+    end
+  end
+
+  def test_create_should_add_author_watcher_only_once
+    user = User.first
+    user.pref.auto_watch_on=['issue_created']
+    user.pref.save
+    issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher')
+    issue.watcher_user_ids = [user.id]
+
+    assert_difference 'Watcher.count', 1 do
+      assert_equal true, issue.save
+    end
+  end
+
+  def test_create_should_not_add_watcher
+    user = User.first
+    user.pref.auto_watch_on=[]
+    user.pref.save
+    issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_not_add_watcher')
+
+    assert_no_difference 'Watcher.count' do
+      assert_equal true, issue.save
+    end
+  end
+
   def test_like_should_escape_query
     issue = Issue.generate!(:subject => "asdf")
     r = Issue.like('as_f')
index 67157d5a22680f5c915cecd1d05cbd13ada15afe..6a74dde0a9e2f7cd3be4199082943e4d43e1ae29 100644 (file)
@@ -59,7 +59,7 @@ class UserPreferenceTest < ActiveSupport::TestCase
 
   def test_auto_watch_on_should_default_to_setting
     preference = UserPreference.new
-    assert_equal ['issue_contributed_to'], preference.auto_watch_on
+    assert_equal %w[issue_created issue_contributed_to], preference.auto_watch_on
   end
 
   def test_create