##// END OF EJS Templates
Automatic closing of duplicate issues....
Jean-Philippe Lang -
r657:4fa51992b5bf
parent child
Show More
@@ -1,142 +1,164
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Issue < ActiveRecord::Base
18 class Issue < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 belongs_to :tracker
20 belongs_to :tracker
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27
27
28 has_many :journals, :as => :journalized, :dependent => :destroy
28 has_many :journals, :as => :journalized, :dependent => :destroy
29 has_many :attachments, :as => :container, :dependent => :destroy
29 has_many :attachments, :as => :container, :dependent => :destroy
30 has_many :time_entries, :dependent => :nullify
30 has_many :time_entries, :dependent => :nullify
31 has_many :custom_values, :dependent => :delete_all, :as => :customized
31 has_many :custom_values, :dependent => :delete_all, :as => :customized
32 has_many :custom_fields, :through => :custom_values
32 has_many :custom_fields, :through => :custom_values
33 has_and_belongs_to_many :changesets, :order => "revision ASC"
33 has_and_belongs_to_many :changesets, :order => "revision ASC"
34
34
35 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
35 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
36 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
36 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
37
37
38 acts_as_watchable
38 acts_as_watchable
39
39
40 validates_presence_of :subject, :description, :priority, :tracker, :author, :status
40 validates_presence_of :subject, :description, :priority, :tracker, :author, :status
41 validates_length_of :subject, :maximum => 255
41 validates_length_of :subject, :maximum => 255
42 validates_inclusion_of :done_ratio, :in => 0..100
42 validates_inclusion_of :done_ratio, :in => 0..100
43 validates_associated :custom_values, :on => :update
43 validates_associated :custom_values, :on => :update
44
44
45 # set default status for new issues
45 # set default status for new issues
46 def before_validation
46 def before_validation
47 self.status = IssueStatus.default if status.nil?
47 self.status = IssueStatus.default if status.nil?
48 end
48 end
49
49
50 def validate
50 def validate
51 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
51 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
52 errors.add :due_date, :activerecord_error_not_a_date
52 errors.add :due_date, :activerecord_error_not_a_date
53 end
53 end
54
54
55 if self.due_date and self.start_date and self.due_date < self.start_date
55 if self.due_date and self.start_date and self.due_date < self.start_date
56 errors.add :due_date, :activerecord_error_greater_than_start_date
56 errors.add :due_date, :activerecord_error_greater_than_start_date
57 end
57 end
58
58
59 if start_date && soonest_start && start_date < soonest_start
59 if start_date && soonest_start && start_date < soonest_start
60 errors.add :start_date, :activerecord_error_invalid
60 errors.add :start_date, :activerecord_error_invalid
61 end
61 end
62
62
63 # validate assignment
63 # validate assignment
64 if assigned_to && !assignable_users.include?(assigned_to)
64 if assigned_to && !assignable_users.include?(assigned_to)
65 errors.add :assigned_to_id, :activerecord_error_invalid
65 errors.add :assigned_to_id, :activerecord_error_invalid
66 end
66 end
67 end
67 end
68
68
69 def before_create
69 def before_create
70 # default assignment based on category
70 # default assignment based on category
71 if assigned_to.nil? && category && category.assigned_to
71 if assigned_to.nil? && category && category.assigned_to
72 self.assigned_to = category.assigned_to
72 self.assigned_to = category.assigned_to
73 end
73 end
74 end
74 end
75
75
76 def before_save
76 def before_save
77 if @current_journal
77 if @current_journal
78 # attributes changes
78 # attributes changes
79 (Issue.column_names - %w(id description)).each {|c|
79 (Issue.column_names - %w(id description)).each {|c|
80 @current_journal.details << JournalDetail.new(:property => 'attr',
80 @current_journal.details << JournalDetail.new(:property => 'attr',
81 :prop_key => c,
81 :prop_key => c,
82 :old_value => @issue_before_change.send(c),
82 :old_value => @issue_before_change.send(c),
83 :value => send(c)) unless send(c)==@issue_before_change.send(c)
83 :value => send(c)) unless send(c)==@issue_before_change.send(c)
84 }
84 }
85 # custom fields changes
85 # custom fields changes
86 custom_values.each {|c|
86 custom_values.each {|c|
87 @current_journal.details << JournalDetail.new(:property => 'cf',
87 @current_journal.details << JournalDetail.new(:property => 'cf',
88 :prop_key => c.custom_field_id,
88 :prop_key => c.custom_field_id,
89 :old_value => @custom_values_before_change[c.custom_field_id],
89 :old_value => @custom_values_before_change[c.custom_field_id],
90 :value => c.value) unless @custom_values_before_change[c.custom_field_id]==c.value
90 :value => c.value) unless @custom_values_before_change[c.custom_field_id]==c.value
91 }
91 }
92 @current_journal.save unless @current_journal.details.empty? and @current_journal.notes.empty?
92 @current_journal.save unless @current_journal.details.empty? and @current_journal.notes.empty?
93 end
93 end
94 end
94 end
95
95
96 def after_save
96 def after_save
97 # Update start/due dates of following issues
97 relations_from.each(&:set_issue_to_dates)
98 relations_from.each(&:set_issue_to_dates)
99
100 # Close duplicates if the issue was closed
101 if @issue_before_change && !@issue_before_change.closed? && self.closed?
102 duplicates.each do |duplicate|
103 # Don't re-close it if it's already closed
104 next if duplicate.closed?
105 # Same user and notes
106 duplicate.init_journal(@current_journal.user, @current_journal.notes)
107 duplicate.update_attribute :status, self.status
108 end
109 end
98 end
110 end
99
111
100 def custom_value_for(custom_field)
112 def custom_value_for(custom_field)
101 self.custom_values.each {|v| return v if v.custom_field_id == custom_field.id }
113 self.custom_values.each {|v| return v if v.custom_field_id == custom_field.id }
102 return nil
114 return nil
103 end
115 end
104
116
105 def init_journal(user, notes = "")
117 def init_journal(user, notes = "")
106 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
118 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
107 @issue_before_change = self.clone
119 @issue_before_change = self.clone
108 @custom_values_before_change = {}
120 @custom_values_before_change = {}
109 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
121 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
110 @current_journal
122 @current_journal
111 end
123 end
112
124
125 # Return true if the issue is closed, otherwise false
126 def closed?
127 self.status.is_closed?
128 end
129
113 # Users the issue can be assigned to
130 # Users the issue can be assigned to
114 def assignable_users
131 def assignable_users
115 project.members.select {|m| m.role.assignable?}.collect {|m| m.user}
132 project.members.select {|m| m.role.assignable?}.collect {|m| m.user}
116 end
133 end
117
134
118 def spent_hours
135 def spent_hours
119 @spent_hours ||= time_entries.sum(:hours) || 0
136 @spent_hours ||= time_entries.sum(:hours) || 0
120 end
137 end
121
138
122 def relations
139 def relations
123 (relations_from + relations_to).sort
140 (relations_from + relations_to).sort
124 end
141 end
125
142
126 def all_dependent_issues
143 def all_dependent_issues
127 dependencies = []
144 dependencies = []
128 relations_from.each do |relation|
145 relations_from.each do |relation|
129 dependencies << relation.issue_to
146 dependencies << relation.issue_to
130 dependencies += relation.issue_to.all_dependent_issues
147 dependencies += relation.issue_to.all_dependent_issues
131 end
148 end
132 dependencies
149 dependencies
133 end
150 end
134
151
152 # Returns an array of the duplicate issues
153 def duplicates
154 relations.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.other_issue(self)}
155 end
156
135 def duration
157 def duration
136 (start_date && due_date) ? due_date - start_date : 0
158 (start_date && due_date) ? due_date - start_date : 0
137 end
159 end
138
160
139 def soonest_start
161 def soonest_start
140 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
162 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
141 end
163 end
142 end
164 end
@@ -1,27 +1,52
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class IssueTest < Test::Unit::TestCase
20 class IssueTest < Test::Unit::TestCase
21 fixtures :projects, :users, :members, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues
21 fixtures :projects, :users, :members, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues
22
22
23 def test_category_based_assignment
23 def test_category_based_assignment
24 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
24 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
25 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
25 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
26 end
26 end
27
28 def test_close_duplicates
29 # Create 3 issues
30 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test')
31 assert issue1.save
32 issue2 = issue1.clone
33 assert issue2.save
34 issue3 = issue1.clone
35 assert issue3.save
36
37 # 2 is a dupe of 1
38 IssueRelation.create(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
39 # And 3 is a dupe of 2
40 IssueRelation.create(:issue_from => issue2, :issue_to => issue3, :relation_type => IssueRelation::TYPE_DUPLICATES)
41
42 assert issue1.reload.duplicates.include?(issue2)
43
44 # Closing issue 1
45 issue1.init_journal(User.find(:first), "Closing issue1")
46 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
47 assert issue1.save
48 # 2 and 3 should be also closed
49 assert issue2.reload.closed?
50 assert issue3.reload.closed?
51 end
27 end
52 end
General Comments 0
You need to be logged in to leave comments. Login now