From 414c4e3fd83a6a3cae623dcc8135d6ef09a30562 Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Thu, 26 Oct 2017 16:06:51 +0200 Subject: Add helper methods to redirect legacy paths --- changelogs/unreleased/bvl-free-paths.yml | 5 +++++ lib/gitlab/routing.rb | 9 +++++++++ spec/support/legacy_path_redirect_shared_examples.rb | 13 +++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 changelogs/unreleased/bvl-free-paths.yml create mode 100644 spec/support/legacy_path_redirect_shared_examples.rb diff --git a/changelogs/unreleased/bvl-free-paths.yml b/changelogs/unreleased/bvl-free-paths.yml new file mode 100644 index 00000000000..f15459cc788 --- /dev/null +++ b/changelogs/unreleased/bvl-free-paths.yml @@ -0,0 +1,5 @@ +--- +title: Free up some reserved group names +merge_request: 15052 +author: +type: other diff --git a/lib/gitlab/routing.rb b/lib/gitlab/routing.rb index e57890f1143..abfd413b7ea 100644 --- a/lib/gitlab/routing.rb +++ b/lib/gitlab/routing.rb @@ -40,5 +40,14 @@ module Gitlab def self.url_helpers @url_helpers ||= Gitlab::Application.routes.url_helpers end + + def self.redirect_legacy_paths(router, *paths) + paths.each do |path| + router.match "/#{path}(/*rest)", + via: [:get, :post, :patch, :delete], + to: router.redirect { |_params, request| request.fullpath.gsub(%r{/#{path}/*}, "/-/#{path}/") }, + as: "legacy_#{path}_redirect" + end + end end end diff --git a/spec/support/legacy_path_redirect_shared_examples.rb b/spec/support/legacy_path_redirect_shared_examples.rb new file mode 100644 index 00000000000..f300bdd48b1 --- /dev/null +++ b/spec/support/legacy_path_redirect_shared_examples.rb @@ -0,0 +1,13 @@ +shared_examples 'redirecting a legacy path' do |source, target| + include RSpec::Rails::RequestExampleGroup + + it "redirects #{source} to #{target} when the resource does not exist" do + expect(get(source)).to redirect_to(target) + end + + it "does not redirect #{source} to #{target} when the resource exists" do + resource + + expect(get(source)).not_to redirect_to(target) + end +end -- cgit v1.2.1