From 633538151b99c658bcbb2173e91eb5deba4408f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9my=20Coutable?= <remy@rymai.me>
Date: Thu, 1 Dec 2016 12:07:52 +0100
Subject: [PATCH] Fix URL rewritting in the Help section
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Rémy Coutable <remy@rymai.me>
---
 app/controllers/help_controller.rb            |  6 +++--
 .../25199-fix-broken-urls-in-help-page.yml    |  4 ++++
 spec/controllers/help_controller_spec.rb      | 24 ++++++++++++-------
 spec/features/help_pages_spec.rb              | 13 ++++++++--
 4 files changed, 34 insertions(+), 13 deletions(-)
 create mode 100644 changelogs/unreleased/25199-fix-broken-urls-in-help-page.yml

diff --git a/app/controllers/help_controller.rb b/app/controllers/help_controller.rb
index a10cdcce72b..37feff79999 100644
--- a/app/controllers/help_controller.rb
+++ b/app/controllers/help_controller.rb
@@ -7,8 +7,10 @@ class HelpController < ApplicationController
     @help_index = File.read(Rails.root.join('doc', 'README.md'))
 
     # Prefix Markdown links with `help/` unless they are external links
-    # See http://rubular.com/r/MioSrVLK3S
-    @help_index.gsub!(%r{(\]\()(?!.+://)([^\)\(]+\))}, '\1/help/\2')
+    # See http://rubular.com/r/X3baHTbPO2
+    @help_index.gsub!(%r{(?<delim>\]\()(?!.+://)(?!/)(?<link>[^\)\(]+\))}) do
+      "#{$~[:delim]}#{Gitlab.config.gitlab.relative_url_root}/help/#{$~[:link]}"
+    end
   end
 
   def show
diff --git a/changelogs/unreleased/25199-fix-broken-urls-in-help-page.yml b/changelogs/unreleased/25199-fix-broken-urls-in-help-page.yml
new file mode 100644
index 00000000000..58efd9113f2
--- /dev/null
+++ b/changelogs/unreleased/25199-fix-broken-urls-in-help-page.yml
@@ -0,0 +1,4 @@
+---
+title: Don't change relative URLs to absolute URLs in the Help page
+merge_request: 
+author: 
diff --git a/spec/controllers/help_controller_spec.rb b/spec/controllers/help_controller_spec.rb
index cffed987f6b..d3489324a9c 100644
--- a/spec/controllers/help_controller_spec.rb
+++ b/spec/controllers/help_controller_spec.rb
@@ -8,26 +8,32 @@ describe HelpController do
   end
 
   describe 'GET #index' do
-    context 'when url prefixed without /help/' do
-      it 'has correct url prefix' do
-        stub_readme("[API](api/README.md)")
+    context 'with absolute url' do
+      it 'keeps the URL absolute' do
+        stub_readme("[API](/api/README.md)")
+
         get :index
-        expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
+
+        expect(assigns[:help_index]).to eq '[API](/api/README.md)'
       end
     end
 
-    context 'when url prefixed with help' do
-      it 'will be an absolute path' do
-        stub_readme("[API](helpful_hints/README.md)")
+    context 'with relative url' do
+      it 'prefixes it with /help/' do
+        stub_readme("[API](api/README.md)")
+
         get :index
-        expect(assigns[:help_index]).to eq '[API](/help/helpful_hints/README.md)'
+
+        expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
       end
     end
 
     context 'when url is an external link' do
-      it 'will not be changed' do
+      it 'does not change it' do
         stub_readme("[external](https://some.external.link)")
+
         get :index
+
         expect(assigns[:help_index]).to eq '[external](https://some.external.link)'
       end
     end
diff --git a/spec/features/help_pages_spec.rb b/spec/features/help_pages_spec.rb
index 73d03837144..4319d6db0d2 100644
--- a/spec/features/help_pages_spec.rb
+++ b/spec/features/help_pages_spec.rb
@@ -12,9 +12,9 @@ describe 'Help Pages', feature: true do
   end
 
   describe 'Get the main help page' do
-    shared_examples_for 'help page' do
+    shared_examples_for 'help page' do |prefix: ''|
       it 'prefixes links correctly' do
-        expect(page).to have_selector('div.documentation-index > ul a[href="/help/api/README.md"]')
+        expect(page).to have_selector(%(div.documentation-index > ul a[href="#{prefix}/help/api/README.md"]))
       end
     end
 
@@ -33,5 +33,14 @@ describe 'Help Pages', feature: true do
 
       it_behaves_like 'help page'
     end
+
+    context 'with a relative installation' do
+      before do
+        stub_config_setting(relative_url_root: '/gitlab')
+        visit help_path
+      end
+
+      it_behaves_like 'help page', prefix: '/gitlab'
+    end
   end
 end
-- 
GitLab