From 634a03bf608eaf86ecf14b4f44fa30e62905ac6b Mon Sep 17 00:00:00 2001 From: Luke Duncalfe Date: Wed, 24 Jul 2019 13:53:06 +1200 Subject: [PATCH] Redirect project.wiki.git to project wiki home https://gitlab.com/gitlab-org/gitlab-ce/issues/19186 --- .../19186-redirect-wiki-git-route-to-wiki.yml | 5 ++++ config/routes/git_http.rb | 12 ++++++++++ lib/gitlab/path_regex.rb | 4 ++++ spec/routing/git_http_routing_spec.rb | 24 +++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 changelogs/unreleased/19186-redirect-wiki-git-route-to-wiki.yml create mode 100644 spec/routing/git_http_routing_spec.rb diff --git a/changelogs/unreleased/19186-redirect-wiki-git-route-to-wiki.yml b/changelogs/unreleased/19186-redirect-wiki-git-route-to-wiki.yml new file mode 100644 index 00000000000..705621d06f7 --- /dev/null +++ b/changelogs/unreleased/19186-redirect-wiki-git-route-to-wiki.yml @@ -0,0 +1,5 @@ +--- +title: Redirect from a project wiki git route to the project wiki home +merge_request: 31085 +author: +type: added diff --git a/config/routes/git_http.rb b/config/routes/git_http.rb index a959d40881b..aac6d418a92 100644 --- a/config/routes/git_http.rb +++ b/config/routes/git_http.rb @@ -34,6 +34,18 @@ scope(path: '*namespace_id/:project_id', end end + # Redirect /group/project.wiki.git to the project wiki + scope(format: true, constraints: { project_id: Gitlab::PathRegex.project_wiki_git_route_regex, format: :git }) do + wiki_redirect = redirect do |params, request| + project_id = params[:project_id].delete_suffix('.wiki') + path = [params[:namespace_id], project_id, 'wikis'].join('/') + path << "?#{request.query_string}" unless request.query_string.blank? + path + end + + get '/', to: wiki_redirect + end + # Redirect /group/project/info/refs to /group/project.git/info/refs scope(constraints: { project_id: Gitlab::PathRegex.project_route_regex }) do # Allow /info/refs, /info/refs?service=git-upload-pack, and diff --git a/lib/gitlab/path_regex.rb b/lib/gitlab/path_regex.rb index a13b3f9e069..f96466b2b00 100644 --- a/lib/gitlab/path_regex.rb +++ b/lib/gitlab/path_regex.rb @@ -175,6 +175,10 @@ module Gitlab @project_git_route_regex ||= /#{project_route_regex}\.git/.freeze end + def project_wiki_git_route_regex + @project_wiki_git_route_regex ||= /#{PATH_REGEX_STR}\.wiki/.freeze + end + def full_namespace_path_regex @full_namespace_path_regex ||= %r{\A#{full_namespace_route_regex}/\z} end diff --git a/spec/routing/git_http_routing_spec.rb b/spec/routing/git_http_routing_spec.rb new file mode 100644 index 00000000000..af14e5f81cb --- /dev/null +++ b/spec/routing/git_http_routing_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'git_http routing' do + include RSpec::Rails::RequestExampleGroup + + describe 'wiki.git routing', 'routing' do + let(:wiki_path) { '/gitlab/gitlabhq/wikis' } + + it 'redirects namespace/project.wiki.git to the project wiki' do + expect(get('/gitlab/gitlabhq.wiki.git')).to redirect_to(wiki_path) + end + + it 'preserves query parameters' do + expect(get('/gitlab/gitlabhq.wiki.git?foo=bar&baz=qux')).to redirect_to("#{wiki_path}?foo=bar&baz=qux") + end + + it 'only redirects when the format is .git' do + expect(get('/gitlab/gitlabhq.wiki')).not_to redirect_to(wiki_path) + expect(get('/gitlab/gitlabhq.wiki.json')).not_to redirect_to(wiki_path) + end + end +end