Commit c5bff77e authored by Nick Thomas's avatar Nick Thomas

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.
parent 0621ee38
...@@ -11,16 +11,11 @@ module Gitlab ...@@ -11,16 +11,11 @@ module Gitlab
{ user_id: user_id, username: username, name: name, email: email, message: message } { user_id: user_id, username: username, name: name, email: email, message: message }
end end
end end
PageBlob = Struct.new(:name)
# GollumSlug inlines just enough knowledge from Gollum::Page to generate a # GollumSlug inlines just enough knowledge from Gollum::Page to generate a
# slug, which is used when previewing pages that haven't been persisted # slug, which is used when previewing pages that haven't been persisted
class GollumSlug class GollumSlug
class << self class << self
def format_to_ext(format)
format == :markdown ? 'md' : format.to_s
end
def cname(name, char_white_sub = '-', char_other_sub = '-') def cname(name, char_white_sub = '-', char_other_sub = '-')
if name.respond_to?(:gsub) if name.respond_to?(:gsub)
name.gsub(/\s/, char_white_sub).gsub(/[<>+]/, char_other_sub) name.gsub(/\s/, char_white_sub).gsub(/[<>+]/, char_other_sub)
...@@ -29,18 +24,27 @@ module Gitlab ...@@ -29,18 +24,27 @@ module Gitlab
end end
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) def generate(title, format)
name = cname(title) + '.' + format_to_ext(format) ext = format_to_ext(format.to_sym)
blob = PageBlob.new(name) name = cname(title) + '.' + ext
canonical_name = canonicalize_filename(name)
path = path =
if blob.name.include?('/') if name.include?('/')
blob.name.sub(%r{/[^/]+$}, '/') name.sub(%r{/[^/]+$}, '/')
else else
'' ''
end end
path + cname(name) path + cname(canonical_name, '-', '-')
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Git::Wiki do describe Gitlab::Git::Wiki do
using RSpec::Parameterized::TableSyntax
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { project.owner } let(:user) { project.owner }
let(:project_wiki) { ProjectWiki.new(project, user) } let(:project_wiki) { ProjectWiki.new(project, user) }
subject { project_wiki.wiki }
subject(:wiki) { project_wiki.wiki }
describe '#pages' do describe '#pages' do
before do before do
...@@ -64,8 +67,44 @@ describe Gitlab::Git::Wiki do ...@@ -64,8 +67,44 @@ describe Gitlab::Git::Wiki do
end end
end end
def create_page(name, content) describe '#preview_slug' do
subject.write_page(name, :markdown, content, commit_details(name)) 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<bar>+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 end
def commit_details(name) def commit_details(name)
...@@ -73,7 +112,7 @@ describe Gitlab::Git::Wiki do ...@@ -73,7 +112,7 @@ describe Gitlab::Git::Wiki do
end end
def destroy_page(title, dir = '') 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") project_wiki.delete_page(page, "test commit")
end end
end end
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