From 11d56a1551e2b5d57656433f912a4dec1cee3008 Mon Sep 17 00:00:00 2001 From: Kia Mei Somabes Date: Fri, 20 Jul 2018 16:13:29 +0800 Subject: [PATCH 1/4] Redirect commits to root_ref if no ref is provided --- app/controllers/application_controller.rb | 4 ++++ .../unreleased/31576-redirect-commits-to-root-if-no-ref.yml | 5 +++++ config/routes/repository.rb | 1 + 3 files changed, 10 insertions(+) create mode 100644 changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 21cc6dfdd16..13eedba54a5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -63,6 +63,10 @@ class ApplicationController < ActionController::Base render_503 end + def redirect_commits_root + redirect_to :controller => 'commits', :action => 'show', :id => @repository.root_ref + end + def redirect_back_or_default(default: root_path, options: {}) redirect_to request.referer.present? ? :back : default, options end diff --git a/changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml b/changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml new file mode 100644 index 00000000000..d117e9ad3d7 --- /dev/null +++ b/changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml @@ -0,0 +1,5 @@ +--- +title: Redirect commits to root if no ref is provided (31576) +merge_request: +author: Kia Mei Somabes +type: added diff --git a/config/routes/repository.rb b/config/routes/repository.rb index e2bf8d6a7ff..87101a69bba 100644 --- a/config/routes/repository.rb +++ b/config/routes/repository.rb @@ -83,6 +83,7 @@ scope format: false do get '/raw/*id', to: 'raw#show', as: :raw get '/blame/*id', to: 'blame#show', as: :blame + get '/commits/', to: 'application#redirect_commits_root', as: :commits_root get '/commits/*id/signatures', to: 'commits#signatures', as: :signatures get '/commits/*id', to: 'commits#show', as: :commits From 3b4734ce714f2e112241453dd04f0273a9d362ec Mon Sep 17 00:00:00 2001 From: Kia Mei Somabes Date: Mon, 23 Jul 2018 06:36:49 +0800 Subject: [PATCH 2/4] Change hash syntax for redirect commits to root if no ref is provided --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 13eedba54a5..e802437df94 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -64,7 +64,7 @@ class ApplicationController < ActionController::Base end def redirect_commits_root - redirect_to :controller => 'commits', :action => 'show', :id => @repository.root_ref + redirect_to controller: 'commits', action: 'show', id: @repository.root_ref end def redirect_back_or_default(default: root_path, options: {}) From c03bc268be7769ceeb9d1738d6b887a7c866e2a0 Mon Sep 17 00:00:00 2001 From: Kia Mei Somabes Date: Tue, 24 Jul 2018 08:04:16 +0800 Subject: [PATCH 3/4] Transfer to commits_controller, add test, and update changelog --- app/controllers/application_controller.rb | 4 ---- app/controllers/projects/commits_controller.rb | 10 +++++++--- .../31576-redirect-commits-to-root-if-no-ref.yml | 2 +- config/routes/repository.rb | 2 +- .../projects/commits_controller_spec.rb | 14 ++++++++++++++ 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e802437df94..21cc6dfdd16 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -63,10 +63,6 @@ class ApplicationController < ActionController::Base render_503 end - def redirect_commits_root - redirect_to controller: 'commits', action: 'show', id: @repository.root_ref - end - def redirect_back_or_default(default: root_path, options: {}) redirect_to request.referer.present? ? :back : default, options end diff --git a/app/controllers/projects/commits_controller.rb b/app/controllers/projects/commits_controller.rb index 9e495061f4e..36faea8056e 100644 --- a/app/controllers/projects/commits_controller.rb +++ b/app/controllers/projects/commits_controller.rb @@ -4,13 +4,17 @@ class Projects::CommitsController < Projects::ApplicationController include ExtractsPath include RendersCommits - before_action :whitelist_query_limiting + before_action :whitelist_query_limiting, except: :commits_root before_action :require_non_empty_project - before_action :assign_ref_vars + before_action :assign_ref_vars, except: :commits_root before_action :authorize_download_code! - before_action :set_commits + before_action :set_commits, except: :commits_root before_action :set_request_format, only: :show + def commits_root + redirect_to project_commits_path(@project, @project.default_branch) + end + def show @merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.opened .find_by(source_project: @project, source_branch: @ref, target_branch: @repository.root_ref) diff --git a/changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml b/changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml index d117e9ad3d7..21d9d25d342 100644 --- a/changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml +++ b/changelogs/unreleased/31576-redirect-commits-to-root-if-no-ref.yml @@ -1,5 +1,5 @@ --- title: Redirect commits to root if no ref is provided (31576) -merge_request: +merge_request: 20738 author: Kia Mei Somabes type: added diff --git a/config/routes/repository.rb b/config/routes/repository.rb index 87101a69bba..d439cb9acbd 100644 --- a/config/routes/repository.rb +++ b/config/routes/repository.rb @@ -83,7 +83,7 @@ scope format: false do get '/raw/*id', to: 'raw#show', as: :raw get '/blame/*id', to: 'blame#show', as: :blame - get '/commits/', to: 'application#redirect_commits_root', as: :commits_root + get '/commits', to: 'commits#commits_root', as: :commits_root get '/commits/*id/signatures', to: 'commits#signatures', as: :signatures get '/commits/*id', to: 'commits#show', as: :commits diff --git a/spec/controllers/projects/commits_controller_spec.rb b/spec/controllers/projects/commits_controller_spec.rb index 55ed276f96b..2e0457378f0 100644 --- a/spec/controllers/projects/commits_controller_spec.rb +++ b/spec/controllers/projects/commits_controller_spec.rb @@ -9,6 +9,20 @@ describe Projects::CommitsController do project.add_master(user) end + describe "GET commits_root" do + context "no ref is provided" do + before do + get(:commits_root, + namespace_id: project.namespace, + project_id: project) + end + + it 'should redirect to the default branch of the project' do + expect(response).to redirect_to project_commits_path(project) + end + end + end + describe "GET show" do render_views From 1dbf32d84bd73037675cd179a7c8a74d980f3939 Mon Sep 17 00:00:00 2001 From: Kia Mei Somabes Date: Tue, 24 Jul 2018 15:45:24 +0800 Subject: [PATCH 4/4] Remove before hook for spec --- spec/controllers/projects/commits_controller_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/controllers/projects/commits_controller_spec.rb b/spec/controllers/projects/commits_controller_spec.rb index 2e0457378f0..f90df441512 100644 --- a/spec/controllers/projects/commits_controller_spec.rb +++ b/spec/controllers/projects/commits_controller_spec.rb @@ -11,13 +11,11 @@ describe Projects::CommitsController do describe "GET commits_root" do context "no ref is provided" do - before do + it 'should redirect to the default branch of the project' do get(:commits_root, namespace_id: project.namespace, project_id: project) - end - it 'should redirect to the default branch of the project' do expect(response).to redirect_to project_commits_path(project) end end