Commit 5a89d7a2 authored by Stan Hu's avatar Stan Hu

Merge branch '32328_use_documentation_url_to_configure_path_to_help_pages' into 'master'

Redirect to documentation pages URL when configuration option is set

See merge request gitlab-org/gitlab!43157
parents 9d71ac07 beffd2e1
...@@ -27,17 +27,10 @@ class HelpController < ApplicationController ...@@ -27,17 +27,10 @@ class HelpController < ApplicationController
respond_to do |format| respond_to do |format|
format.any(:markdown, :md, :html) do format.any(:markdown, :md, :html) do
# Note: We are purposefully NOT using `Rails.root.join` because of https://gitlab.com/gitlab-org/gitlab/-/issues/216028. if redirect_to_documentation_website?
path = File.join(Rails.root, 'doc', "#{@path}.md") redirect_to documentation_url
if File.exist?(path)
# Remove YAML frontmatter so that it doesn't look weird
@markdown = File.read(path).gsub(YAML_FRONT_MATTER_REGEXP, '')
render 'show.html.haml'
else else
# Force template to Haml render_documentation
render 'errors/not_found.html.haml', layout: 'errors', status: :not_found
end end
end end
...@@ -76,4 +69,48 @@ class HelpController < ApplicationController ...@@ -76,4 +69,48 @@ class HelpController < ApplicationController
params params
end end
def redirect_to_documentation_website?
return false unless Feature.enabled?(:help_page_documentation_redirect)
return false unless Gitlab::UrlSanitizer.valid_web?(documentation_url)
true
end
def documentation_url
return unless documentation_base_url
@documentation_url ||= [
documentation_base_url.chomp('/'),
version_segment,
'ee',
"#{@path}.html"
].compact.join('/')
end
def documentation_base_url
@documentation_base_url ||= Gitlab::CurrentSettings.current_application_settings.help_page_documentation_base_url.presence
end
def version_segment
return if Gitlab.pre_release?
version = Gitlab.version_info
[version.major, version.minor].join('.')
end
def render_documentation
# Note: We are purposefully NOT using `Rails.root.join` because of https://gitlab.com/gitlab-org/gitlab/-/issues/216028.
path = File.join(Rails.root, 'doc', "#{@path}.md")
if File.exist?(path)
# Remove YAML frontmatter so that it doesn't look weird
@markdown = File.read(path).gsub(YAML_FRONT_MATTER_REGEXP, '')
render 'show.html.haml'
else
# Force template to Haml
render 'errors/not_found.html.haml', layout: 'errors', status: :not_found
end
end
end end
---
title: Redirect to documentation pages URL when configuration option is set
merge_request: 43157
author:
type: added
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe HelpController do RSpec.describe HelpController do
include StubVersion
let(:user) { create(:user) } let(:user) { create(:user) }
before do before do
...@@ -108,10 +110,58 @@ RSpec.describe HelpController do ...@@ -108,10 +110,58 @@ RSpec.describe HelpController do
end end
it 'renders HTML' do it 'renders HTML' do
aggregate_failures do
expect(response).to render_template('show.html.haml') expect(response).to render_template('show.html.haml')
expect(response.media_type).to eq 'text/html' expect(response.media_type).to eq 'text/html'
end end
end end
end
context 'when a custom help_page_documentation_url is set' do
before do
stub_application_setting(help_page_documentation_base_url: documentation_base_url)
stub_version(gitlab_version, 'deadbeaf')
end
subject { get :show, params: { path: path }, format: 'html' }
let(:gitlab_version) { '13.4.0-ee' }
let(:documentation_base_url) { 'https://docs.gitlab.com' }
let(:path) { 'ssh/README' }
it 'redirects user to custom documentation url with a specified version' do
is_expected.to redirect_to("#{documentation_base_url}/13.4/ee/#{path}.html")
end
context 'when documentation url ends with a slash' do
let(:documentation_base_url) { 'https://docs.gitlab.com/' }
it 'redirects user to custom documentation url without slash duplicates' do
is_expected.to redirect_to("https://docs.gitlab.com/13.4/ee/#{path}.html")
end
end
context 'when it is a pre-release' do
let(:gitlab_version) { '13.4.0-pre' }
it 'redirects user to custom documentation url without a version' do
is_expected.to redirect_to("#{documentation_base_url}/ee/#{path}.html")
end
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(help_page_documentation_redirect: false)
end
it 'renders HTML' do
aggregate_failures do
is_expected.to render_template('show.html.haml')
expect(response.media_type).to eq 'text/html'
end
end
end
end
context 'when requested file is missing' do context 'when requested file is missing' do
it 'renders not found' do it 'renders not found' do
...@@ -129,11 +179,14 @@ RSpec.describe HelpController do ...@@ -129,11 +179,14 @@ RSpec.describe HelpController do
path: 'user/img/markdown_logo' path: 'user/img/markdown_logo'
}, },
format: :png format: :png
aggregate_failures do
expect(response).to be_successful expect(response).to be_successful
expect(response.media_type).to eq 'image/png' expect(response.media_type).to eq 'image/png'
expect(response.headers['Content-Disposition']).to match(/^inline;/) expect(response.headers['Content-Disposition']).to match(/^inline;/)
end end
end end
end
context 'when requested file is missing' do context 'when requested file is missing' do
it 'renders not found' do it 'renders not found' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment