From c5bff77ea4a6170a3dc1966254feac0ca1836eaa Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Mon, 15 Oct 2018 18:34:55 +0100 Subject: [PATCH] Remove a dependency on gitlab-gollum-lib Inlining this code allows us to remove a dependency on gitlab_grit in gitlab-ce. We can't stop maintaining gitlab_grit yet, since gitaly-ruby still depends on this gem, but it moves us a step closer. --- lib/gitlab/git/wiki.rb | 24 +++++++++------- spec/lib/gitlab/git/wiki_spec.rb | 47 +++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/lib/gitlab/git/wiki.rb b/lib/gitlab/git/wiki.rb index 563712c5d63..7fe56979d5c 100644 --- a/lib/gitlab/git/wiki.rb +++ b/lib/gitlab/git/wiki.rb @@ -11,16 +11,11 @@ module Gitlab { user_id: user_id, username: username, name: name, email: email, message: message } end end - PageBlob = Struct.new(:name) # GollumSlug inlines just enough knowledge from Gollum::Page to generate a # slug, which is used when previewing pages that haven't been persisted class GollumSlug class << self - def format_to_ext(format) - format == :markdown ? 'md' : format.to_s - end - def cname(name, char_white_sub = '-', char_other_sub = '-') if name.respond_to?(:gsub) name.gsub(/\s/, char_white_sub).gsub(/[<>+]/, char_other_sub) @@ -29,18 +24,27 @@ module Gitlab end end + def format_to_ext(format) + format == :markdown ? "md" : format.to_s + end + + def canonicalize_filename(filename) + ::File.basename(filename, ::File.extname(filename)).tr('-', ' ') + end + def generate(title, format) - name = cname(title) + '.' + format_to_ext(format) - blob = PageBlob.new(name) + ext = format_to_ext(format.to_sym) + name = cname(title) + '.' + ext + canonical_name = canonicalize_filename(name) path = - if blob.name.include?('/') - blob.name.sub(%r{/[^/]+$}, '/') + if name.include?('/') + name.sub(%r{/[^/]+$}, '/') else '' end - path + cname(name) + path + cname(canonical_name, '-', '-') end end end diff --git a/spec/lib/gitlab/git/wiki_spec.rb b/spec/lib/gitlab/git/wiki_spec.rb index c5666e4ec61..ded5d7576df 100644 --- a/spec/lib/gitlab/git/wiki_spec.rb +++ b/spec/lib/gitlab/git/wiki_spec.rb @@ -1,10 +1,13 @@ require 'spec_helper' describe Gitlab::Git::Wiki do + using RSpec::Parameterized::TableSyntax + let(:project) { create(:project) } let(:user) { project.owner } let(:project_wiki) { ProjectWiki.new(project, user) } - subject { project_wiki.wiki } + + subject(:wiki) { project_wiki.wiki } describe '#pages' do before do @@ -64,8 +67,44 @@ describe Gitlab::Git::Wiki do end end - def create_page(name, content) - subject.write_page(name, :markdown, content, commit_details(name)) + describe '#preview_slug' do + where(:title, :format, :expected_slug) do + 'The Best Thing' | :markdown | 'The-Best-Thing' + 'The Best Thing' | :md | 'The-Best-Thing' + 'The Best Thing' | :txt | 'The-Best-Thing' + 'A Subject/Title Here' | :txt | 'A-Subject/Title-Here' + 'A subject' | :txt | 'A-subject' + 'A 1/B 2/C 3' | :txt | 'A-1/B-2/C-3' + 'subject/title' | :txt | 'subject/title' + 'subject/title.md' | :txt | 'subject/title.md' + 'foo+baz' | :txt | 'foo-bar--baz' + 'foo%2Fbar' | :txt | 'foo%2Fbar' + '' | :markdown | '.md' + '' | :md | '.md' + '' | :txt | '.txt' + end + + with_them do + subject { wiki.preview_slug(title, format) } + + let(:gitaly_slug) { wiki.pages.first } + + it { is_expected.to eq(expected_slug) } + + it 'matches the slug generated by gitaly' do + skip('Gitaly cannot generate a slug for an empty title') unless title.present? + + create_page(title, 'content', format: format) + + gitaly_slug = wiki.pages.first.url_path + + is_expected.to eq(gitaly_slug) + end + end + end + + def create_page(name, content, format: :markdown) + wiki.write_page(name, format, content, commit_details(name)) end def commit_details(name) @@ -73,7 +112,7 @@ describe Gitlab::Git::Wiki do end def destroy_page(title, dir = '') - page = subject.page(title: title, dir: dir) + page = wiki.page(title: title, dir: dir) project_wiki.delete_page(page, "test commit") end end