@@ -1,118 +1,120 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Changeset < ActiveRecord::Base |
|
19 | 19 | belongs_to :repository |
|
20 | 20 | has_many :changes, :dependent => :delete_all |
|
21 | 21 | has_and_belongs_to_many :issues |
|
22 | 22 | |
|
23 | 23 | acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.revision}" + (o.comments.blank? ? '' : (': ' + o.comments))}, |
|
24 | 24 | :description => :comments, |
|
25 | 25 | :datetime => :committed_on, |
|
26 | 26 | :author => :committer, |
|
27 | 27 | :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project_id, :rev => o.revision}} |
|
28 | 28 | |
|
29 | 29 | acts_as_searchable :columns => 'comments', |
|
30 | 30 | :include => :repository, |
|
31 | 31 | :project_key => "#{Repository.table_name}.project_id", |
|
32 | 32 | :date_column => 'committed_on' |
|
33 | 33 | |
|
34 | 34 | validates_presence_of :repository_id, :revision, :committed_on, :commit_date |
|
35 | 35 | validates_numericality_of :revision, :only_integer => true |
|
36 | 36 | validates_uniqueness_of :revision, :scope => :repository_id |
|
37 | 37 | validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true |
|
38 | 38 | |
|
39 | 39 | def comments=(comment) |
|
40 | 40 | write_attribute(:comments, comment.strip) |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def committed_on=(date) |
|
44 | 44 | self.commit_date = date |
|
45 | 45 | super |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | def after_create |
|
49 | 49 | scan_comment_for_issue_ids |
|
50 | 50 | end |
|
51 | 51 | require 'pp' |
|
52 | 52 | |
|
53 | 53 | def scan_comment_for_issue_ids |
|
54 | 54 | return if comments.blank? |
|
55 | 55 | # keywords used to reference issues |
|
56 | 56 | ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip) |
|
57 | 57 | # keywords used to fix issues |
|
58 | 58 | fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip) |
|
59 | 59 | # status and optional done ratio applied |
|
60 | 60 | fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id) |
|
61 | 61 | done_ratio = Setting.commit_fix_done_ratio.blank? ? nil : Setting.commit_fix_done_ratio.to_i |
|
62 | 62 | |
|
63 | 63 | kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|") |
|
64 | 64 | return if kw_regexp.blank? |
|
65 | 65 | |
|
66 | 66 | referenced_issues = [] |
|
67 | 67 | |
|
68 | 68 | if ref_keywords.delete('*') |
|
69 | 69 | # find any issue ID in the comments |
|
70 | 70 | target_issue_ids = [] |
|
71 | 71 | comments.scan(%r{([\s\(,-^])#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] } |
|
72 | 72 | referenced_issues += repository.project.issues.find_all_by_id(target_issue_ids) |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match| |
|
76 | 76 | action = match[0] |
|
77 | 77 | target_issue_ids = match[1].scan(/\d+/) |
|
78 | 78 | target_issues = repository.project.issues.find_all_by_id(target_issue_ids) |
|
79 | 79 | if fix_status && fix_keywords.include?(action.downcase) |
|
80 | 80 | # update status of issues |
|
81 | 81 | logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug? |
|
82 | 82 | target_issues.each do |issue| |
|
83 | # the issue may have been updated by the closure of another one (eg. duplicate) | |
|
84 | issue.reload | |
|
83 | 85 | # don't change the status is the issue is closed |
|
84 | 86 | next if issue.status.is_closed? |
|
85 | 87 | user = committer_user || User.anonymous |
|
86 | 88 | journal = issue.init_journal(user, l(:text_status_changed_by_changeset, "r#{self.revision}")) |
|
87 | 89 | issue.status = fix_status |
|
88 | 90 | issue.done_ratio = done_ratio if done_ratio |
|
89 | 91 | issue.save |
|
90 | 92 | Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') |
|
91 | 93 | end |
|
92 | 94 | end |
|
93 | 95 | referenced_issues += target_issues |
|
94 | 96 | end |
|
95 | 97 | |
|
96 | 98 | self.issues = referenced_issues.uniq |
|
97 | 99 | end |
|
98 | 100 | |
|
99 | 101 | # Returns the Redmine User corresponding to the committer |
|
100 | 102 | def committer_user |
|
101 | 103 | if committer && committer.strip =~ /^([^<]+)(<(.*)>)?$/ |
|
102 | 104 | username, email = $1.strip, $3 |
|
103 | 105 | u = User.find_by_login(username) |
|
104 | 106 | u ||= User.find_by_mail(email) unless email.blank? |
|
105 | 107 | u |
|
106 | 108 | end |
|
107 | 109 | end |
|
108 | 110 | |
|
109 | 111 | # Returns the previous changeset |
|
110 | 112 | def previous |
|
111 | 113 | @previous ||= Changeset.find(:first, :conditions => ['revision < ? AND repository_id = ?', self.revision, self.repository_id], :order => 'revision DESC') |
|
112 | 114 | end |
|
113 | 115 | |
|
114 | 116 | # Returns the next changeset |
|
115 | 117 | def next |
|
116 | 118 | @next ||= Changeset.find(:first, :conditions => ['revision > ? AND repository_id = ?', self.revision, self.repository_id], :order => 'revision ASC') |
|
117 | 119 | end |
|
118 | 120 | end |
General Comments 0
You need to be logged in to leave comments.
Login now