From 6b90ccb9fd57401912e1978cbad28cc693a2e0a1 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 6 Oct 2016 15:14:24 +0300 Subject: [PATCH 1/4] Change user & group landing page routing from /u/:name & /groups/:name to /:name Signed-off-by: Dmitriy Zaporozhets --- CHANGELOG | 1 + app/assets/javascripts/user_tabs.js.es6 | 11 ++---- app/assets/javascripts/users_select.js | 4 +-- .../boards/components/_card.html.haml | 2 +- app/views/users/show.html.haml | 2 +- config/routes/group.rb | 8 +++++ config/routes/user.rb | 35 ++++++++++++------- lib/constraints/group_url_constrainer.rb | 7 ++++ lib/constraints/namespace_url_constrainer.rb | 13 +++++++ lib/constraints/user_url_constrainer.rb | 7 ++++ spec/features/users_spec.rb | 22 ++++++++++++ spec/helpers/projects_helper_spec.rb | 2 +- .../namespace_url_constrainer_spec.rb | 26 ++++++++++++++ spec/routing/routing_spec.rb | 4 ++- spec/services/system_note_service_spec.rb | 2 +- 15 files changed, 119 insertions(+), 27 deletions(-) create mode 100644 lib/constraints/group_url_constrainer.rb create mode 100644 lib/constraints/namespace_url_constrainer.rb create mode 100644 lib/constraints/user_url_constrainer.rb create mode 100644 spec/lib/constraints/namespace_url_constrainer_spec.rb diff --git a/CHANGELOG b/CHANGELOG index 30c8e801ea1..43713d9c9e2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -82,6 +82,7 @@ v 8.12.4 - Fix failed project deletion when feature visibility set to private. !6688 - Prevent claiming associated model IDs via import. - Set GitLab project exported file permissions to owner only + - Change user & group landing page routing from /u/:username to /:username v 8.12.3 - Update Gitlab Shell to support low IO priority for storage moves diff --git a/app/assets/javascripts/user_tabs.js.es6 b/app/assets/javascripts/user_tabs.js.es6 index 63bce0a6f6f..dfdfa1e7f75 100644 --- a/app/assets/javascripts/user_tabs.js.es6 +++ b/app/assets/javascripts/user_tabs.js.es6 @@ -89,7 +89,7 @@ content on the Users#show page. const action = $target.data('action'); const source = $target.attr('href'); this.setTab(source, action); - return this.setCurrentAction(action); + return this.setCurrentAction(source, action); } activateTab(action) { @@ -142,14 +142,9 @@ content on the Users#show page. .toggle(status); } - setCurrentAction(action) { - const regExp = new RegExp(`\/(${this.actions.join('|')})(\.html)?\/?$`); - let new_state = this._location.pathname; + setCurrentAction(source, action) { + let new_state = source new_state = new_state.replace(/\/+$/, ''); - new_state = new_state.replace(regExp, ''); - if (action !== this.defaultAction) { - new_state += `/${action}`; - } new_state += this._location.search + this._location.hash; history.replaceState({ turbolinks: true, diff --git a/app/assets/javascripts/users_select.js b/app/assets/javascripts/users_select.js index 05056a73aaf..bcabda3ceb2 100644 --- a/app/assets/javascripts/users_select.js +++ b/app/assets/javascripts/users_select.js @@ -71,8 +71,8 @@ return $collapsedSidebar.html(collapsedAssigneeTemplate(user)); }); }; - collapsedAssigneeTemplate = _.template('<% if( avatar ) { %> <% } else { %> <% } %>'); - assigneeTemplate = _.template('<% if (username) { %> <% if( avatar ) { %> <% } %> <%- name %> @<%- username %> <% } else { %> No assignee - assign yourself <% } %>'); + collapsedAssigneeTemplate = _.template('<% if( avatar ) { %> <% } else { %> <% } %>'); + assigneeTemplate = _.template('<% if (username) { %> <% if( avatar ) { %> <% } %> <%- name %> @<%- username %> <% } else { %> No assignee - assign yourself <% } %>'); return $dropdown.glDropdown({ showMenuAbove: showMenuAbove, data: function(term, callback) { diff --git a/app/views/projects/boards/components/_card.html.haml b/app/views/projects/boards/components/_card.html.haml index f15c87c8185..d8f16022407 100644 --- a/app/views/projects/boards/components/_card.html.haml +++ b/app/views/projects/boards/components/_card.html.haml @@ -26,7 +26,7 @@ ":title" => "label.description", data: { container: 'body' } } {{ label.title }} - %a.has-tooltip{ ":href" => "'/u/' + issue.assignee.username", + %a.has-tooltip{ ":href" => "'/' + issue.assignee.username", ":title" => "'Assigned to ' + issue.assignee.name", "v-if" => "issue.assignee", data: { container: 'body' } } diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml index 60fc0c0daf6..19759a33add 100644 --- a/app/views/users/show.html.haml +++ b/app/views/users/show.html.haml @@ -82,7 +82,7 @@ %ul.nav-links.center.user-profile-nav %li.js-activity-tab - = link_to user_calendar_activities_path, data: {target: 'div#activity', action: 'activity', toggle: 'tab'} do + = link_to user_path, data: {target: 'div#activity', action: 'activity', toggle: 'tab'} do Activity %li.js-groups-tab = link_to user_groups_path, data: {target: 'div#groups', action: 'groups', toggle: 'tab'} do diff --git a/config/routes/group.rb b/config/routes/group.rb index 5b3e25d5e3d..47a8a0a53d4 100644 --- a/config/routes/group.rb +++ b/config/routes/group.rb @@ -1,3 +1,11 @@ +require 'constraints/group_url_constrainer' + +constraints(GroupUrlConstrainer.new) do + scope(path: ':id', as: :group, controller: :groups) do + get '/', action: :show + end +end + resources :groups, constraints: { id: /[a-zA-Z.0-9_\-]+(? 'omniauth_callbacks#omniauth_error', as: :omniauth_error get '/users/almost_there' => 'confirmations#almost_there' end + +constraints(UserUrlConstrainer.new) do + scope(path: ':username', as: :user, controller: :users) do + get '/', action: :show + end +end + +scope(path: 'u/:username', + as: :user, + constraints: { username: /[a-zA-Z.0-9_\-]+(? Date: Thu, 6 Oct 2016 15:34:30 +0300 Subject: [PATCH 2/4] Fix users feature spec Signed-off-by: Dmitriy Zaporozhets --- spec/features/users_spec.rb | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/spec/features/users_spec.rb b/spec/features/users_spec.rb index f2c0aa7784a..6498b7317b4 100644 --- a/spec/features/users_spec.rb +++ b/spec/features/users_spec.rb @@ -46,14 +46,8 @@ feature 'Users', feature: true do scenario '/u/user1 redirects to user page' do visit '/u/user1' - expect_user_show_page - end - - - scenario '/users/user1 redirects to user page' do - visit '/users/user1' - - expect_user_show_page + expect(current_path).to eq user_path(user) + expect(page).to have_text(user.name) end end @@ -64,9 +58,4 @@ feature 'Users', feature: true do def number_of_errors_on_page(page) page.find('#error_explanation').find('ul').all('li').count end - - def expect_user_show_page - expect(current_path).to eq user_path(user) - expect(page).to have_text(user.name) - end end From 42c6555bab47eb042749be1f24d486797eb8d8ee Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 6 Oct 2016 16:59:22 +0300 Subject: [PATCH 3/4] Make user constrainer lookup same as controller and add more constrainer tests Signed-off-by: Dmitriy Zaporozhets --- lib/constraints/user_url_constrainer.rb | 2 +- spec/lib/constraints/group_url_constrainer_spec.rb | 10 ++++++++++ spec/lib/constraints/namespace_url_constrainer_spec.rb | 1 - spec/lib/constraints/user_url_constrainer_spec.rb | 10 ++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 spec/lib/constraints/group_url_constrainer_spec.rb create mode 100644 spec/lib/constraints/user_url_constrainer_spec.rb diff --git a/lib/constraints/user_url_constrainer.rb b/lib/constraints/user_url_constrainer.rb index ef38d47d5c6..504a0f5d93e 100644 --- a/lib/constraints/user_url_constrainer.rb +++ b/lib/constraints/user_url_constrainer.rb @@ -2,6 +2,6 @@ require 'constraints/namespace_url_constrainer' class UserUrlConstrainer < NamespaceUrlConstrainer def find_resource(id) - User.find_by_username(id) + User.find_by('lower(username) = ?', id.downcase) end end diff --git a/spec/lib/constraints/group_url_constrainer_spec.rb b/spec/lib/constraints/group_url_constrainer_spec.rb new file mode 100644 index 00000000000..f0b75a664f2 --- /dev/null +++ b/spec/lib/constraints/group_url_constrainer_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe GroupUrlConstrainer, lib: true do + let!(:username) { create(:group, path: 'gitlab-org') } + + describe '#find_resource' do + it { expect(!!subject.find_resource('gitlab-org')).to be_truthy } + it { expect(!!subject.find_resource('gitlab-com')).to be_falsey } + end +end diff --git a/spec/lib/constraints/namespace_url_constrainer_spec.rb b/spec/lib/constraints/namespace_url_constrainer_spec.rb index 8940fd6b94e..a5feaacb8ee 100644 --- a/spec/lib/constraints/namespace_url_constrainer_spec.rb +++ b/spec/lib/constraints/namespace_url_constrainer_spec.rb @@ -2,7 +2,6 @@ require 'spec_helper' describe NamespaceUrlConstrainer, lib: true do let!(:group) { create(:group, path: 'gitlab') } - subject { NamespaceUrlConstrainer.new } describe '#matches?' do context 'existing namespace' do diff --git a/spec/lib/constraints/user_url_constrainer_spec.rb b/spec/lib/constraints/user_url_constrainer_spec.rb new file mode 100644 index 00000000000..4b26692672f --- /dev/null +++ b/spec/lib/constraints/user_url_constrainer_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe UserUrlConstrainer, lib: true do + let!(:username) { create(:user, username: 'dz') } + + describe '#find_resource' do + it { expect(!!subject.find_resource('dz')).to be_truthy } + it { expect(!!subject.find_resource('john')).to be_falsey } + end +end From b356c7ddad1ada1d78136386536f6676ca8b1df4 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 6 Oct 2016 18:08:29 +0300 Subject: [PATCH 4/4] Update user routing spec after constrainer logic changed Signed-off-by: Dmitriy Zaporozhets --- spec/routing/routing_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/routing/routing_spec.rb b/spec/routing/routing_spec.rb index 3608aa70f65..0dd00af878d 100644 --- a/spec/routing/routing_spec.rb +++ b/spec/routing/routing_spec.rb @@ -9,7 +9,7 @@ require 'spec_helper' # user_calendar_activities GET /u/:username/calendar_activities(.:format) describe UsersController, "routing" do it "to #show" do - allow(User).to receive(:find_by_username).and_return(true) + allow(User).to receive(:find_by).and_return(true) expect(get("/User")).to route_to('users#show', username: 'User') end