Class: Changeset

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/changeset.rb

Class Method Summary

Instance Method Summary

Methods inherited from ActiveRecord::Base

quoted_table_name

Class Method Details

+ (Object) normalize_comments(str)

Strips and reencodes a commit log before insertion into the database



152
153
154
# File 'app/models/changeset.rb', line 152

def self.normalize_comments(str)
  to_utf8(str.to_s.strip)
end

Instance Method Details

- (Object) after_create



76
77
78
# File 'app/models/changeset.rb', line 76

def after_create
  scan_comment_for_issue_ids
end

- (Object) author



68
69
70
# File 'app/models/changeset.rb', line 68

def author
  user || committer.to_s.split('<').first
end

- (Object) before_create



72
73
74
# File 'app/models/changeset.rb', line 72

def before_create
  self.user = repository.find_committer_user(committer)
end

- (Object) comments=(comment)



51
52
53
# File 'app/models/changeset.rb', line 51

def comments=(comment)
  write_attribute(:comments, Changeset.normalize_comments(comment))
end

- (Object) committed_on=(date)



55
56
57
58
# File 'app/models/changeset.rb', line 55

def committed_on=(date)
  self.commit_date = date
  super
end

- (Object) committer=(arg)



60
61
62
# File 'app/models/changeset.rb', line 60

def committer=(arg)
  write_attribute(:committer, self.class.to_utf8(arg.to_s))
end

- (Object) create_change(change)

Creates a new Change from it’s common parameters



157
158
159
160
161
162
163
# File 'app/models/changeset.rb', line 157

def create_change(change)
  Change.create(:changeset => self,
                :action => change[:action],
                :path => change[:path],
                :from_path => change[:from_path],
                :from_revision => change[:from_revision])
end

- (Object) long_comments



137
138
139
# File 'app/models/changeset.rb', line 137

def long_comments
  @long_comments || split_comments.last
end

- (Object) next

Returns the next changeset



147
148
149
# File 'app/models/changeset.rb', line 147

def next
  @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
end

- (Object) previous

Returns the previous changeset



142
143
144
# File 'app/models/changeset.rb', line 142

def previous
  @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
end

- (Object) project



64
65
66
# File 'app/models/changeset.rb', line 64

def project
  repository.project
end

- (Object) revision=(r)



47
48
49
# File 'app/models/changeset.rb', line 47

def revision=(r)
  write_attribute :revision, (r.nil? ? nil : r.to_s)
end

- (Object) scan_comment_for_issue_ids



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/changeset.rb', line 81

def scan_comment_for_issue_ids
  return if comments.blank?
  # keywords used to reference issues
  ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
  # keywords used to fix issues
  fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
  
  kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
  return if kw_regexp.blank?
  
  referenced_issues = []
  
  if ref_keywords.delete('*')
    # find any issue ID in the comments
    target_issue_ids = []
    comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] }
    referenced_issues += find_referenced_issues_by_id(target_issue_ids)
  end
  
  comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match|
    action = match[0]
    target_issue_ids = match[1].scan(/\d+/)
    target_issues = find_referenced_issues_by_id(target_issue_ids)
    if fix_keywords.include?(action.downcase) && fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id)
      # update status of issues
      logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug?
      target_issues.each do |issue|
        # the issue may have been updated by the closure of another one (eg. duplicate)
        issue.reload
        # don't change the status is the issue is closed
        next if issue.status.is_closed?
        csettext = "r#{self.revision}"
        if self.scmid && (! (csettext =~ /^r[0-9]+$/))
          csettext = "commit:\"#{self.scmid}\""
        end
        journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, csettext))
        issue.status = fix_status
        unless Setting.commit_fix_done_ratio.blank?
          issue.done_ratio = Setting.commit_fix_done_ratio.to_i
        end
        Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
                                { :changeset => self, :issue => issue })
        issue.save
      end
    end
    referenced_issues += target_issues
  end
  
  referenced_issues.uniq!
  self.issues = referenced_issues unless referenced_issues.empty?
end

- (Object) short_comments



133
134
135
# File 'app/models/changeset.rb', line 133

def short_comments
  @short_comments || split_comments.first
end