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 new file mode 100644 index 00000000000..21d9d25d342 --- /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: 20738 +author: Kia Mei Somabes +type: added diff --git a/config/routes/repository.rb b/config/routes/repository.rb index e2bf8d6a7ff..d439cb9acbd 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: '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 d44048fdf55..a43bdd3ea80 100644 --- a/spec/controllers/projects/commits_controller_spec.rb +++ b/spec/controllers/projects/commits_controller_spec.rb @@ -9,6 +9,18 @@ describe Projects::CommitsController do project.add_maintainer(user) end + describe "GET commits_root" do + context "no ref is provided" do + it 'should redirect to the default branch of the project' do + get(:commits_root, + namespace_id: project.namespace, + project_id: project) + + expect(response).to redirect_to project_commits_path(project) + end + end + end + describe "GET show" do render_views