##// END OF EJS Templates
Fixes repository user mapping submission when a repository username is blank (#2339, Conflicting types for parameter containers)....
Jean-Philippe Lang -
r2135:7cea286c2344
parent child
Show More
@@ -1,324 +1,325
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 require 'SVG/Graph/Bar'
19 19 require 'SVG/Graph/BarHorizontal'
20 20 require 'digest/sha1'
21 21
22 22 class ChangesetNotFound < Exception; end
23 23 class InvalidRevisionParam < Exception; end
24 24
25 25 class RepositoriesController < ApplicationController
26 26 menu_item :repository
27 27 before_filter :find_repository, :except => :edit
28 28 before_filter :find_project, :only => :edit
29 29 before_filter :authorize
30 30 accept_key_auth :revisions
31 31
32 32 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
33 33
34 34 def edit
35 35 @repository = @project.repository
36 36 if !@repository
37 37 @repository = Repository.factory(params[:repository_scm])
38 38 @repository.project = @project if @repository
39 39 end
40 40 if request.post? && @repository
41 41 @repository.attributes = params[:repository]
42 42 @repository.save
43 43 end
44 44 render(:update) {|page| page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'}
45 45 end
46 46
47 47 def committers
48 48 @committers = @repository.committers
49 49 @users = @project.users
50 50 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
51 51 @users += User.find_all_by_id(additional_user_ids) unless additional_user_ids.empty?
52 52 @users.compact!
53 53 @users.sort!
54 if request.post?
55 @repository.committer_ids = params[:committers]
54 if request.post? && params[:committers].is_a?(Hash)
55 # Build a hash with repository usernames as keys and corresponding user ids as values
56 @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
56 57 flash[:notice] = l(:notice_successful_update)
57 58 redirect_to :action => 'committers', :id => @project
58 59 end
59 60 end
60 61
61 62 def destroy
62 63 @repository.destroy
63 64 redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
64 65 end
65 66
66 67 def show
67 68 # check if new revisions have been committed in the repository
68 69 @repository.fetch_changesets if Setting.autofetch_changesets?
69 70 # root entries
70 71 @entries = @repository.entries('', @rev)
71 72 # latest changesets
72 73 @changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC")
73 74 show_error_not_found unless @entries || @changesets.any?
74 75 end
75 76
76 77 def browse
77 78 @entries = @repository.entries(@path, @rev)
78 79 if request.xhr?
79 80 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
80 81 else
81 82 show_error_not_found and return unless @entries
82 83 @properties = @repository.properties(@path, @rev)
83 84 render :action => 'browse'
84 85 end
85 86 end
86 87
87 88 def changes
88 89 @entry = @repository.entry(@path, @rev)
89 90 show_error_not_found and return unless @entry
90 91 @changesets = @repository.changesets_for_path(@path)
91 92 @properties = @repository.properties(@path, @rev)
92 93 end
93 94
94 95 def revisions
95 96 @changeset_count = @repository.changesets.count
96 97 @changeset_pages = Paginator.new self, @changeset_count,
97 98 per_page_option,
98 99 params['page']
99 100 @changesets = @repository.changesets.find(:all,
100 101 :limit => @changeset_pages.items_per_page,
101 102 :offset => @changeset_pages.current.offset,
102 103 :include => :user)
103 104
104 105 respond_to do |format|
105 106 format.html { render :layout => false if request.xhr? }
106 107 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
107 108 end
108 109 end
109 110
110 111 def entry
111 112 @entry = @repository.entry(@path, @rev)
112 113 show_error_not_found and return unless @entry
113 114
114 115 # If the entry is a dir, show the browser
115 116 browse and return if @entry.is_dir?
116 117
117 118 @content = @repository.cat(@path, @rev)
118 119 show_error_not_found and return unless @content
119 120 if 'raw' == params[:format] || @content.is_binary_data?
120 121 # Force the download if it's a binary file
121 122 send_data @content, :filename => @path.split('/').last
122 123 else
123 124 # Prevent empty lines when displaying a file with Windows style eol
124 125 @content.gsub!("\r\n", "\n")
125 126 end
126 127 end
127 128
128 129 def annotate
129 130 @annotate = @repository.scm.annotate(@path, @rev)
130 131 render_error l(:error_scm_annotate) and return if @annotate.nil? || @annotate.empty?
131 132 end
132 133
133 134 def revision
134 135 @changeset = @repository.changesets.find_by_revision(@rev)
135 136 raise ChangesetNotFound unless @changeset
136 137
137 138 respond_to do |format|
138 139 format.html
139 140 format.js {render :layout => false}
140 141 end
141 142 rescue ChangesetNotFound
142 143 show_error_not_found
143 144 end
144 145
145 146 def diff
146 147 if params[:format] == 'diff'
147 148 @diff = @repository.diff(@path, @rev, @rev_to)
148 149 show_error_not_found and return unless @diff
149 150 filename = "changeset_r#{@rev}"
150 151 filename << "_r#{@rev_to}" if @rev_to
151 152 send_data @diff.join, :filename => "#{filename}.diff",
152 153 :type => 'text/x-patch',
153 154 :disposition => 'attachment'
154 155 else
155 156 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
156 157 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
157 158
158 159 # Save diff type as user preference
159 160 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
160 161 User.current.pref[:diff_type] = @diff_type
161 162 User.current.preference.save
162 163 end
163 164
164 165 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
165 166 unless read_fragment(@cache_key)
166 167 @diff = @repository.diff(@path, @rev, @rev_to)
167 168 show_error_not_found unless @diff
168 169 end
169 170 end
170 171 end
171 172
172 173 def stats
173 174 end
174 175
175 176 def graph
176 177 data = nil
177 178 case params[:graph]
178 179 when "commits_per_month"
179 180 data = graph_commits_per_month(@repository)
180 181 when "commits_per_author"
181 182 data = graph_commits_per_author(@repository)
182 183 end
183 184 if data
184 185 headers["Content-Type"] = "image/svg+xml"
185 186 send_data(data, :type => "image/svg+xml", :disposition => "inline")
186 187 else
187 188 render_404
188 189 end
189 190 end
190 191
191 192 private
192 193 def find_project
193 194 @project = Project.find(params[:id])
194 195 rescue ActiveRecord::RecordNotFound
195 196 render_404
196 197 end
197 198
198 199 REV_PARAM_RE = %r{^[a-f0-9]*$}
199 200
200 201 def find_repository
201 202 @project = Project.find(params[:id])
202 203 @repository = @project.repository
203 204 render_404 and return false unless @repository
204 205 @path = params[:path].join('/') unless params[:path].nil?
205 206 @path ||= ''
206 207 @rev = params[:rev]
207 208 @rev_to = params[:rev_to]
208 209 raise InvalidRevisionParam unless @rev.to_s.match(REV_PARAM_RE) && @rev.to_s.match(REV_PARAM_RE)
209 210 rescue ActiveRecord::RecordNotFound
210 211 render_404
211 212 rescue InvalidRevisionParam
212 213 show_error_not_found
213 214 end
214 215
215 216 def show_error_not_found
216 217 render_error l(:error_scm_not_found)
217 218 end
218 219
219 220 # Handler for Redmine::Scm::Adapters::CommandFailed exception
220 221 def show_error_command_failed(exception)
221 222 render_error l(:error_scm_command_failed, exception.message)
222 223 end
223 224
224 225 def graph_commits_per_month(repository)
225 226 @date_to = Date.today
226 227 @date_from = @date_to << 11
227 228 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
228 229 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
229 230 commits_by_month = [0] * 12
230 231 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
231 232
232 233 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
233 234 changes_by_month = [0] * 12
234 235 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
235 236
236 237 fields = []
237 238 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
238 239 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
239 240
240 241 graph = SVG::Graph::Bar.new(
241 242 :height => 300,
242 243 :width => 800,
243 244 :fields => fields.reverse,
244 245 :stack => :side,
245 246 :scale_integers => true,
246 247 :step_x_labels => 2,
247 248 :show_data_values => false,
248 249 :graph_title => l(:label_commits_per_month),
249 250 :show_graph_title => true
250 251 )
251 252
252 253 graph.add_data(
253 254 :data => commits_by_month[0..11].reverse,
254 255 :title => l(:label_revision_plural)
255 256 )
256 257
257 258 graph.add_data(
258 259 :data => changes_by_month[0..11].reverse,
259 260 :title => l(:label_change_plural)
260 261 )
261 262
262 263 graph.burn
263 264 end
264 265
265 266 def graph_commits_per_author(repository)
266 267 commits_by_author = repository.changesets.count(:all, :group => :committer)
267 268 commits_by_author.sort! {|x, y| x.last <=> y.last}
268 269
269 270 changes_by_author = repository.changes.count(:all, :group => :committer)
270 271 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
271 272
272 273 fields = commits_by_author.collect {|r| r.first}
273 274 commits_data = commits_by_author.collect {|r| r.last}
274 275 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
275 276
276 277 fields = fields + [""]*(10 - fields.length) if fields.length<10
277 278 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
278 279 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
279 280
280 281 # Remove email adress in usernames
281 282 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
282 283
283 284 graph = SVG::Graph::BarHorizontal.new(
284 285 :height => 400,
285 286 :width => 800,
286 287 :fields => fields,
287 288 :stack => :side,
288 289 :scale_integers => true,
289 290 :show_data_values => false,
290 291 :rotate_y_labels => false,
291 292 :graph_title => l(:label_commits_per_author),
292 293 :show_graph_title => true
293 294 )
294 295
295 296 graph.add_data(
296 297 :data => commits_data,
297 298 :title => l(:label_revision_plural)
298 299 )
299 300
300 301 graph.add_data(
301 302 :data => changes_data,
302 303 :title => l(:label_change_plural)
303 304 )
304 305
305 306 graph.burn
306 307 end
307 308
308 309 end
309 310
310 311 class Date
311 312 def months_ago(date = Date.today)
312 313 (date.year - self.year)*12 + (date.month - self.month)
313 314 end
314 315
315 316 def weeks_ago(date = Date.today)
316 317 (date.year - self.year)*52 + (date.cweek - self.cweek)
317 318 end
318 319 end
319 320
320 321 class String
321 322 def with_leading_slash
322 323 starts_with?('/') ? self : "/#{self}"
323 324 end
324 325 end
@@ -1,29 +1,34
1 1 <h2><%= l(:label_repository) %></h2>
2 2
3 3 <%= simple_format(l(:text_repository_usernames_mapping)) %>
4 4
5 5 <% if @committers.empty? %>
6 6 <p class="nodata"><%= l(:label_no_data) %></p>
7 7 <% else %>
8 8
9 9 <% form_tag({}) do %>
10 10 <table class="list">
11 11 <thead>
12 12 <tr>
13 13 <th><%= l(:field_login) %></th>
14 14 <th><%= l(:label_user) %></th>
15 15 </tr>
16 16 </thead>
17 17 <tbody>
18 <% i = 0 -%>
18 19 <% @committers.each do |committer, user_id| -%>
19 20 <tr class="<%= cycle 'odd', 'even' %>">
20 21 <td><%=h committer %></td>
21 <td><%= select_tag "committers[#{committer}]", content_tag('option', "-- #{l :actionview_instancetag_blank_option} --", :value => '') + options_from_collection_for_select(@users, 'id', 'name', user_id.to_i) %></td>
22 <td>
23 <%= hidden_field_tag "committers[#{i}][]", committer %>
24 <%= select_tag "committers[#{i}][]", content_tag('option', "-- #{l :actionview_instancetag_blank_option} --", :value => '') + options_from_collection_for_select(@users, 'id', 'name', user_id.to_i) %>
25 </td>
22 26 </tr>
27 <% i += 1 -%>
23 28 <% end -%>
24 29 </tbody>
25 30 </table>
26 31 <p><%= submit_tag(l(:button_update)) %></p>
27 32 <% end %>
28 33
29 34 <% end %> No newline at end of file
@@ -1,98 +1,98
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 require File.dirname(__FILE__) + '/../test_helper'
19 19 require 'repositories_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class RepositoriesController; def rescue_action(e) raise e end; end
23 23
24 24 class RepositoriesControllerTest < Test::Unit::TestCase
25 25 fixtures :projects, :users, :roles, :members, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :trackers
26 26
27 27 def setup
28 28 @controller = RepositoriesController.new
29 29 @request = ActionController::TestRequest.new
30 30 @response = ActionController::TestResponse.new
31 31 User.current = nil
32 32 end
33 33
34 34 def test_revisions
35 35 get :revisions, :id => 1
36 36 assert_response :success
37 37 assert_template 'revisions'
38 38 assert_not_nil assigns(:changesets)
39 39 end
40 40
41 41 def test_revision_with_before_nil_and_afer_normal
42 42 get :revision, {:id => 1, :rev => 1}
43 43 assert_response :success
44 44 assert_template 'revision'
45 45 assert_no_tag :tag => "div", :attributes => { :class => "contextual" },
46 46 :child => { :tag => "a", :attributes => { :href => '/repositories/revision/ecookbook/0'}
47 47 }
48 48 assert_tag :tag => "div", :attributes => { :class => "contextual" },
49 49 :child => { :tag => "a", :attributes => { :href => '/repositories/revision/ecookbook/2'}
50 50 }
51 51 end
52 52
53 53 def test_graph_commits_per_month
54 54 get :graph, :id => 1, :graph => 'commits_per_month'
55 55 assert_response :success
56 56 assert_equal 'image/svg+xml', @response.content_type
57 57 end
58 58
59 59 def test_graph_commits_per_author
60 60 get :graph, :id => 1, :graph => 'commits_per_author'
61 61 assert_response :success
62 62 assert_equal 'image/svg+xml', @response.content_type
63 63 end
64 64
65 65 def test_committers
66 66 @request.session[:user_id] = 2
67 67 # add a commit with an unknown user
68 68 Changeset.create!(:repository => Project.find(1).repository, :committer => 'foo', :committed_on => Time.now, :revision => 100, :comments => 'Committed by foo.')
69 69
70 70 get :committers, :id => 1
71 71 assert_response :success
72 72 assert_template 'committers'
73 73
74 74 assert_tag :td, :content => 'dlopper',
75 75 :sibling => { :tag => 'td',
76 :child => { :tag => 'select', :attributes => { :name => 'committers[dlopper]' },
76 :child => { :tag => 'select', :attributes => { :name => %r{^committers\[\d+\]\[\]$} },
77 77 :child => { :tag => 'option', :content => 'Dave Lopper',
78 78 :attributes => { :value => '3', :selected => 'selected' }}}}
79 79 assert_tag :td, :content => 'foo',
80 80 :sibling => { :tag => 'td',
81 :child => { :tag => 'select', :attributes => { :name => 'committers[foo]' }}}
81 :child => { :tag => 'select', :attributes => { :name => %r{^committers\[\d+\]\[\]$} }}}
82 82 assert_no_tag :td, :content => 'foo',
83 83 :sibling => { :tag => 'td',
84 84 :descendant => { :tag => 'option', :attributes => { :selected => 'selected' }}}
85 85 end
86 86
87 87 def test_map_committers
88 88 @request.session[:user_id] = 2
89 89 # add a commit with an unknown user
90 90 c = Changeset.create!(:repository => Project.find(1).repository, :committer => 'foo', :committed_on => Time.now, :revision => 100, :comments => 'Committed by foo.')
91 91
92 92 assert_no_difference "Changeset.count(:conditions => 'user_id = 3')" do
93 post :committers, :id => 1, :committers => { 'foo' => '2', 'dlopper' => '3'}
93 post :committers, :id => 1, :committers => { '0' => ['foo', '2'], '1' => ['dlopper', '3']}
94 94 assert_redirected_to '/repositories/committers/ecookbook'
95 95 assert_equal User.find(2), c.reload.user
96 96 end
97 97 end
98 98 end
General Comments 0
You need to be logged in to leave comments. Login now