Commit c8ba5c2d58b882fd7cd5342a42158bb5f810fd60

Authored by Dmitriy Zaporozhets
1 parent 49e73f8a

Fix routing issues when navigating over tree, commits etc

app/helpers/commits_helper.rb
@@ -70,4 +70,12 @@ module CommitsHelper @@ -70,4 +70,12 @@ module CommitsHelper
70 escape_javascript(render 'commits/commit', commit: commit) 70 escape_javascript(render 'commits/commit', commit: commit)
71 end 71 end
72 end 72 end
  73 +
  74 + def diff_line_content(line)
  75 + if line.blank?
  76 + "  "
  77 + else
  78 + line
  79 + end
  80 + end
73 end 81 end
app/models/project.rb
@@ -102,7 +102,7 @@ class Project < ActiveRecord::Base @@ -102,7 +102,7 @@ class Project < ActiveRecord::Base
102 if id.include?("/") 102 if id.include?("/")
103 id = id.split("/") 103 id = id.split("/")
104 namespace_id = Namespace.find_by_path(id.first).id 104 namespace_id = Namespace.find_by_path(id.first).id
105 - where(namespace_id: namespace_id).find_by_path(id.last) 105 + where(namespace_id: namespace_id).find_by_path(id.second)
106 else 106 else
107 where(path: id, namespace_id: nil).last 107 where(path: id, namespace_id: nil).last
108 end 108 end
app/views/commits/_text_file.html.haml
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 - if @comments_allowed 15 - if @comments_allowed
16 = render "notes/per_line_note_link", line_code: line_code 16 = render "notes/per_line_note_link", line_code: line_code
17 %td.new_line= link_to raw(type == "old" ? " " : line_new) , "##{line_code}", id: line_code 17 %td.new_line= link_to raw(type == "old" ? " " : line_new) , "##{line_code}", id: line_code
18 - %td.line_content{class: "noteable_line #{type} #{line_code}", "line_code" => line_code}= raw "#{line}  " 18 + %td.line_content{class: "noteable_line #{type} #{line_code}", "line_code" => line_code}= raw diff_line_content(line)
19 19
20 - if @comments_allowed 20 - if @comments_allowed
21 - comments = @line_notes.select { |n| n.line_code == line_code }.sort_by(&:created_at) 21 - comments = @line_notes.select { |n| n.line_code == line_code }.sort_by(&:created_at)
config/routes.rb
@@ -112,7 +112,7 @@ Gitlab::Application.routes.draw do @@ -112,7 +112,7 @@ Gitlab::Application.routes.draw do
112 # 112 #
113 # Project Area 113 # Project Area
114 # 114 #
115 - resources :projects, constraints: { id: /[a-zA-Z.\/0-9_\-]+/ }, except: [:new, :create, :index], path: "/" do 115 + resources :projects, constraints: { id: /[a-zA-Z.0-9_\-\/]+/ }, except: [:new, :create, :index], path: "/" do
116 member do 116 member do
117 get "wall" 117 get "wall"
118 get "graph" 118 get "graph"
@@ -190,12 +190,12 @@ Gitlab::Application.routes.draw do @@ -190,12 +190,12 @@ Gitlab::Application.routes.draw do
190 end 190 end
191 end 191 end
192 192
  193 + resources :tree, only: [:show, :edit, :update], constraints: {id: /.+/}
193 resources :commit, only: [:show], constraints: {id: /[[:alnum:]]{6,40}/} 194 resources :commit, only: [:show], constraints: {id: /[[:alnum:]]{6,40}/}
194 resources :commits, only: [:show], constraints: {id: /.+/} 195 resources :commits, only: [:show], constraints: {id: /.+/}
195 resources :compare, only: [:index, :create] 196 resources :compare, only: [:index, :create]
196 resources :blame, only: [:show], constraints: {id: /.+/} 197 resources :blame, only: [:show], constraints: {id: /.+/}
197 resources :blob, only: [:show], constraints: {id: /.+/} 198 resources :blob, only: [:show], constraints: {id: /.+/}
198 - resources :tree, only: [:show, :edit, :update], constraints: {id: /.+/}  
199 match "/compare/:from...:to" => "compare#show", as: "compare", 199 match "/compare/:from...:to" => "compare#show", as: "compare",
200 :via => [:get, :post], constraints: {from: /.+/, to: /.+/} 200 :via => [:get, :post], constraints: {from: /.+/, to: /.+/}
201 201
lib/extracts_path.rb
@@ -33,6 +33,9 @@ module ExtractsPath @@ -33,6 +33,9 @@ module ExtractsPath
33 # extract_ref("v2.0.0/README.md") 33 # extract_ref("v2.0.0/README.md")
34 # # => ['v2.0.0', 'README.md'] 34 # # => ['v2.0.0', 'README.md']
35 # 35 #
  36 + # extract_ref('/gitlab/vagrant/tree/master/app/models/project.rb')
  37 + # # => ['master', 'app/models/project.rb']
  38 + #
36 # extract_ref('issues/1234/app/models/project.rb') 39 # extract_ref('issues/1234/app/models/project.rb')
37 # # => ['issues/1234', 'app/models/project.rb'] 40 # # => ['issues/1234', 'app/models/project.rb']
38 # 41 #
@@ -47,6 +50,13 @@ module ExtractsPath @@ -47,6 +50,13 @@ module ExtractsPath
47 50
48 return pair unless @project 51 return pair unless @project
49 52
  53 + # Remove project, actions and all other staff from path
  54 + input.gsub!("/#{@project.path_with_namespace}", "")
  55 + input.gsub!(/^\/(tree|commits|blame|blob)\//, "") # remove actions
  56 + input.gsub!(/\?.*$/, "") # remove stamps suffix
  57 + input.gsub!(/.atom$/, "") # remove rss feed
  58 + input.gsub!(/\/edit$/, "") # remove edit route part
  59 +
50 if input.match(/^([[:alnum:]]{40})(.+)/) 60 if input.match(/^([[:alnum:]]{40})(.+)/)
51 # If the ref appears to be a SHA, we're done, just split the string 61 # If the ref appears to be a SHA, we're done, just split the string
52 pair = $~.captures 62 pair = $~.captures
@@ -98,7 +108,7 @@ module ExtractsPath @@ -98,7 +108,7 @@ module ExtractsPath
98 request.format = :atom 108 request.format = :atom
99 end 109 end
100 110
101 - @ref, @path = extract_ref(params[:id]) 111 + @ref, @path = extract_ref(request.fullpath)
102 112
103 @id = File.join(@ref, @path) 113 @id = File.join(@ref, @path)
104 114
spec/lib/extracts_path_spec.rb
@@ -8,6 +8,7 @@ describe ExtractsPath do @@ -8,6 +8,7 @@ describe ExtractsPath do
8 before do 8 before do
9 @project = project 9 @project = project
10 project.stub(:ref_names).and_return(['master', 'foo/bar/baz', 'v1.0.0', 'v2.0.0']) 10 project.stub(:ref_names).and_return(['master', 'foo/bar/baz', 'v1.0.0', 'v2.0.0'])
  11 + project.stub(path_with_namespace: 'gitlab/gitlab-ci')
11 end 12 end
12 13
13 describe '#extract_ref' do 14 describe '#extract_ref' do
@@ -53,5 +54,24 @@ describe ExtractsPath do @@ -53,5 +54,24 @@ describe ExtractsPath do
53 extract_ref('stable/CHANGELOG').should == ['stable', 'CHANGELOG'] 54 extract_ref('stable/CHANGELOG').should == ['stable', 'CHANGELOG']
54 end 55 end
55 end 56 end
  57 +
  58 + context "with a fullpath" do
  59 + it "extracts a valid branch" do
  60 + extract_ref('/gitlab/gitlab-ci/tree/foo/bar/baz/CHANGELOG').should == ['foo/bar/baz', 'CHANGELOG']
  61 + end
  62 +
  63 + it "extracts a valid tag" do
  64 + extract_ref('/gitlab/gitlab-ci/tree/v2.0.0/CHANGELOG').should == ['v2.0.0', 'CHANGELOG']
  65 + end
  66 +
  67 + it "extracts a valid commit SHA" do
  68 + extract_ref('/gitlab/gitlab-ci/tree/f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG').should ==
  69 + ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG']
  70 + end
  71 +
  72 + it "extracts a timestamp" do
  73 + extract_ref('/gitlab/gitlab-ci/tree/v2.0.0/CHANGELOG?_=12354435').should == ['v2.0.0', 'CHANGELOG']
  74 + end
  75 + end
56 end 76 end
57 end 77 end
spec/tasks/gitlab/backup_rake_spec.rb
@@ -3,6 +3,7 @@ require 'rake' @@ -3,6 +3,7 @@ require 'rake'
3 3
4 describe 'gitlab:app namespace rake task' do 4 describe 'gitlab:app namespace rake task' do
5 before :all do 5 before :all do
  6 + Rake.application.rake_require "tasks/gitlab/task_helpers"
6 Rake.application.rake_require "tasks/gitlab/backup" 7 Rake.application.rake_require "tasks/gitlab/backup"
7 # empty task as env is already loaded 8 # empty task as env is already loaded
8 Rake::Task.define_task :environment 9 Rake::Task.define_task :environment