diff --git a/app/models/gollum_wiki.rb b/app/models/gollum_wiki.rb deleted file mode 100644 index 62c8b51..0000000 --- a/app/models/gollum_wiki.rb +++ /dev/null @@ -1,120 +0,0 @@ -class GollumWiki - include Gitlab::ShellAdapter - - MARKUPS = { - "Markdown" => :markdown, - "RDoc" => :rdoc - } - - class CouldNotCreateWikiError < StandardError; end - - # Returns a string describing what went wrong after - # an operation fails. - attr_reader :error_message - - def initialize(project, user = nil) - @project = project - @user = user - end - - def path - @project.path + '.wiki' - end - - def path_with_namespace - @project.path_with_namespace + ".wiki" - end - - def url_to_repo - gitlab_shell.url_to_repo(path_with_namespace) - end - - def ssh_url_to_repo - url_to_repo - end - - def http_url_to_repo - [Gitlab.config.gitlab.url, "/", path_with_namespace, ".git"].join('') - end - - # Returns the Gollum::Wiki object. - def wiki - @wiki ||= begin - Gollum::Wiki.new(path_to_repo) - rescue Gollum::NoSuchPathError - create_repo! - end - end - - def empty? - pages.empty? - end - - # Returns an Array of Gitlab WikiPage instances or an - # empty Array if this Wiki has no pages. - def pages - wiki.pages.map { |page| WikiPage.new(self, page, true) } - end - - # Finds a page within the repository based on a tile - # or slug. - # - # title - The human readable or parameterized title of - # the page. - # - # Returns an initialized WikiPage instance or nil - def find_page(title, version = nil) - if page = wiki.page(title, version) - WikiPage.new(self, page, true) - else - nil - end - end - - def create_page(title, content, format = :markdown, message = nil) - commit = commit_details(:created, message, title) - - wiki.write_page(title, format, content, commit) - rescue Gollum::DuplicatePageError => e - @error_message = "Duplicate page: #{e.message}" - return false - end - - def update_page(page, content, format = :markdown, message = nil) - commit = commit_details(:updated, message, page.title) - - wiki.update_page(page, page.name, format, content, commit) - end - - def delete_page(page, message = nil) - wiki.delete_page(page, commit_details(:deleted, message, page.title)) - end - - private - - def create_repo! - if init_repo(path_with_namespace) - Gollum::Wiki.new(path_to_repo) - else - raise CouldNotCreateWikiError - end - end - - def init_repo(path_with_namespace) - gitlab_shell.add_repository(path_with_namespace) - end - - def commit_details(action, message = nil, title = nil) - commit_message = message || default_message(action, title) - - {email: @user.email, name: @user.name, message: commit_message} - end - - def default_message(action, title) - "#{@user.username} #{action} page: #{title}" - end - - def path_to_repo - @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git") - end -end diff --git a/app/services/projects/create_service.rb b/app/services/projects/create_service.rb index 4d3d518..11b6525 100644 --- a/app/services/projects/create_service.rb +++ b/app/services/projects/create_service.rb @@ -74,8 +74,8 @@ module Projects if @project.wiki_enabled? begin # force the creation of a wiki, - GollumWiki.new(@project, @project.owner).wiki - rescue GollumWiki::CouldNotCreateWikiError => ex + ProjectWiki.new(@project, @project.owner).wiki + rescue ProjectWiki::CouldNotCreateWikiError => ex # Prevent project observer crash # if failed to create wiki nil diff --git a/features/steps/project/wiki.rb b/features/steps/project/wiki.rb index 6146599..65e7d09 100644 --- a/features/steps/project/wiki.rb +++ b/features/steps/project/wiki.rb @@ -1,4 +1,4 @@ -class ProjectWiki < Spinach::FeatureSteps +class Spinach::Features::ProjectWiki < Spinach::FeatureSteps include SharedAuthentication include SharedProject include SharedNote @@ -16,7 +16,7 @@ class ProjectWiki < Spinach::FeatureSteps end Given 'I create the Wiki Home page' do - fill_in "Content", with: '[link test](test)' + fill_in "wiki_content", with: '[link test](test)' click_on "Create page" end @@ -87,6 +87,6 @@ class ProjectWiki < Spinach::FeatureSteps end def wiki - @gollum_wiki = GollumWiki.new(project, current_user) + @project_wiki = ProjectWiki.new(project, current_user) end end diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb index 552f7ea..214d982 100644 --- a/lib/backup/repository.rb +++ b/lib/backup/repository.rb @@ -24,7 +24,7 @@ module Backup puts "[FAILED]".red end - wiki = GollumWiki.new(project) + wiki = ProjectWiki.new(project) if File.exists?(path_to_repo(wiki)) print " * #{wiki.path_with_namespace} ... " @@ -59,7 +59,7 @@ module Backup puts "[FAILED]".red end - wiki = GollumWiki.new(project) + wiki = ProjectWiki.new(project) if File.exists?(path_to_bundle(wiki)) print " * #{wiki.path_with_namespace} ... " diff --git a/lib/redcarpet/render/gitlab_html.rb b/lib/redcarpet/render/gitlab_html.rb index 86d8b69..7d9428f 100644 --- a/lib/redcarpet/render/gitlab_html.rb +++ b/lib/redcarpet/render/gitlab_html.rb @@ -60,6 +60,8 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML end def is_wiki? - @template.instance_variable_get("@wiki") + if @template.instance_variable_get("@project_wiki") + @template.instance_variable_get("@page") + end end end diff --git a/spec/models/gollum_wiki_spec.rb b/spec/models/gollum_wiki_spec.rb deleted file mode 100644 index 9b9d15b..0000000 --- a/spec/models/gollum_wiki_spec.rb +++ /dev/null @@ -1,211 +0,0 @@ -require "spec_helper" - -describe GollumWiki do - - def remove_temp_repo(path) - FileUtils.rm_rf path - end - - def commit_details - commit = {name: user.name, email: user.email, message: "test commit"} - end - - def create_page(name, content) - subject.wiki.write_page(name, :markdown, content, commit_details) - end - - def destroy_page(page) - subject.wiki.delete_page(page, commit_details) - end - - let(:project) { create(:project) } - let(:repository) { project.repository } - let(:user) { project.owner } - let(:gitlab_shell) { Gitlab::Shell.new } - - subject { GollumWiki.new(project, user) } - - before do - create_temp_repo(subject.send(:path_to_repo)) - end - - describe "#path_with_namespace" do - it "returns the project path with namespace with the .wiki extension" do - subject.path_with_namespace.should == project.path_with_namespace + ".wiki" - end - end - - describe "#url_to_repo" do - it "returns the correct ssh url to the repo" do - subject.url_to_repo.should == gitlab_shell.url_to_repo(subject.path_with_namespace) - end - end - - describe "#ssh_url_to_repo" do - it "equals #url_to_repo" do - subject.ssh_url_to_repo.should == subject.url_to_repo - end - end - - describe "#http_url_to_repo" do - it "provides the full http url to the repo" do - gitlab_url = Gitlab.config.gitlab.url - repo_http_url = "#{gitlab_url}/#{subject.path_with_namespace}.git" - subject.http_url_to_repo.should == repo_http_url - end - end - - describe "#wiki" do - it "contains a Gollum::Wiki instance" do - subject.wiki.should be_a Gollum::Wiki - end - - before do - Gitlab::Shell.any_instance.stub(:add_repository) do - create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git") - end - project.stub(:path_with_namespace).and_return("non-existant") - end - - it "creates a new wiki repo if one does not yet exist" do - wiki = GollumWiki.new(project, user) - wiki.create_page("index", "test content").should_not == false - - FileUtils.rm_rf wiki.send(:path_to_repo) - end - - it "raises CouldNotCreateWikiError if it can't create the wiki repository" do - GollumWiki.any_instance.stub(:init_repo).and_return(false) - expect { GollumWiki.new(project, user).wiki }.to raise_exception(GollumWiki::CouldNotCreateWikiError) - end - end - - describe "#empty?" do - context "when the wiki repository is empty" do - before do - Gitlab::Shell.any_instance.stub(:add_repository) do - create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git") - end - project.stub(:path_with_namespace).and_return("non-existant") - end - - its(:empty?) { should be_true } - end - - context "when the wiki has pages" do - before do - create_page("index", "This is an awesome new Gollum Wiki") - end - - its(:empty?) { should be_false } - end - end - - describe "#pages" do - before do - create_page("index", "This is an awesome new Gollum Wiki") - @pages = subject.pages - end - - after do - destroy_page(@pages.first.page) - end - - it "returns an array of WikiPage instances" do - @pages.first.should be_a WikiPage - end - - it "returns the correct number of pages" do - @pages.count.should == 1 - end - end - - describe "#find_page" do - before do - create_page("index page", "This is an awesome Gollum Wiki") - end - - after do - destroy_page(subject.pages.first.page) - end - - it "returns the latest version of the page if it exists" do - page = subject.find_page("index page") - page.title.should == "index page" - end - - it "returns nil if the page does not exist" do - subject.find_page("non-existant").should == nil - end - - it "can find a page by slug" do - page = subject.find_page("index-page") - page.title.should == "index page" - end - - it "returns a WikiPage instance" do - page = subject.find_page("index page") - page.should be_a WikiPage - end - end - - describe "#create_page" do - after do - destroy_page(subject.pages.first.page) - end - - it "creates a new wiki page" do - subject.create_page("test page", "this is content").should_not == false - subject.pages.count.should == 1 - end - - it "returns false when a duplicate page exists" do - subject.create_page("test page", "content") - subject.create_page("test page", "content").should == false - end - - it "stores an error message when a duplicate page exists" do - 2.times { subject.create_page("test page", "content") } - subject.error_message.should =~ /Duplicate page:/ - end - - it "sets the correct commit message" do - subject.create_page("test page", "some content", :markdown, "commit message") - subject.pages.first.page.version.message.should == "commit message" - end - end - - describe "#update_page" do - before do - create_page("update-page", "some content") - @gollum_page = subject.wiki.paged("update-page") - subject.update_page(@gollum_page, "some other content", :markdown, "updated page") - @page = subject.pages.first.page - end - - after do - destroy_page(@page) - end - - it "updates the content of the page" do - @page.raw_data.should == "some other content" - end - - it "sets the correct commit message" do - @page.version.message.should == "updated page" - end - end - - describe "#delete_page" do - before do - create_page("index", "some content") - @page = subject.wiki.paged("index") - end - - it "deletes the page" do - subject.delete_page(@page) - subject.pages.count.should == 0 - end - end - -end diff --git a/spec/models/project_wiki_spec.rb b/spec/models/project_wiki_spec.rb new file mode 100644 index 0000000..32a8247 --- /dev/null +++ b/spec/models/project_wiki_spec.rb @@ -0,0 +1,211 @@ +require "spec_helper" + +describe ProjectWiki do + + def remove_temp_repo(path) + FileUtils.rm_rf path + end + + def commit_details + commit = {name: user.name, email: user.email, message: "test commit"} + end + + def create_page(name, content) + subject.wiki.write_page(name, :markdown, content, commit_details) + end + + def destroy_page(page) + subject.wiki.delete_page(page, commit_details) + end + + let(:project) { create(:project) } + let(:repository) { project.repository } + let(:user) { project.owner } + let(:gitlab_shell) { Gitlab::Shell.new } + + subject { ProjectWiki.new(project, user) } + + before do + create_temp_repo(subject.send(:path_to_repo)) + end + + describe "#path_with_namespace" do + it "returns the project path with namespace with the .wiki extension" do + subject.path_with_namespace.should == project.path_with_namespace + ".wiki" + end + end + + describe "#url_to_repo" do + it "returns the correct ssh url to the repo" do + subject.url_to_repo.should == gitlab_shell.url_to_repo(subject.path_with_namespace) + end + end + + describe "#ssh_url_to_repo" do + it "equals #url_to_repo" do + subject.ssh_url_to_repo.should == subject.url_to_repo + end + end + + describe "#http_url_to_repo" do + it "provides the full http url to the repo" do + gitlab_url = Gitlab.config.gitlab.url + repo_http_url = "#{gitlab_url}/#{subject.path_with_namespace}.git" + subject.http_url_to_repo.should == repo_http_url + end + end + + describe "#wiki" do + it "contains a Gollum::Wiki instance" do + subject.wiki.should be_a Gollum::Wiki + end + + before do + Gitlab::Shell.any_instance.stub(:add_repository) do + create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git") + end + project.stub(:path_with_namespace).and_return("non-existant") + end + + it "creates a new wiki repo if one does not yet exist" do + wiki = ProjectWiki.new(project, user) + wiki.create_page("index", "test content").should_not == false + + FileUtils.rm_rf wiki.send(:path_to_repo) + end + + it "raises CouldNotCreateWikiError if it can't create the wiki repository" do + ProjectWiki.any_instance.stub(:init_repo).and_return(false) + expect { ProjectWiki.new(project, user).wiki }.to raise_exception(ProjectWiki::CouldNotCreateWikiError) + end + end + + describe "#empty?" do + context "when the wiki repository is empty" do + before do + Gitlab::Shell.any_instance.stub(:add_repository) do + create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git") + end + project.stub(:path_with_namespace).and_return("non-existant") + end + + its(:empty?) { should be_true } + end + + context "when the wiki has pages" do + before do + create_page("index", "This is an awesome new Gollum Wiki") + end + + its(:empty?) { should be_false } + end + end + + describe "#pages" do + before do + create_page("index", "This is an awesome new Gollum Wiki") + @pages = subject.pages + end + + after do + destroy_page(@pages.first.page) + end + + it "returns an array of WikiPage instances" do + @pages.first.should be_a WikiPage + end + + it "returns the correct number of pages" do + @pages.count.should == 1 + end + end + + describe "#find_page" do + before do + create_page("index page", "This is an awesome Gollum Wiki") + end + + after do + destroy_page(subject.pages.first.page) + end + + it "returns the latest version of the page if it exists" do + page = subject.find_page("index page") + page.title.should == "index page" + end + + it "returns nil if the page does not exist" do + subject.find_page("non-existant").should == nil + end + + it "can find a page by slug" do + page = subject.find_page("index-page") + page.title.should == "index page" + end + + it "returns a WikiPage instance" do + page = subject.find_page("index page") + page.should be_a WikiPage + end + end + + describe "#create_page" do + after do + destroy_page(subject.pages.first.page) + end + + it "creates a new wiki page" do + subject.create_page("test page", "this is content").should_not == false + subject.pages.count.should == 1 + end + + it "returns false when a duplicate page exists" do + subject.create_page("test page", "content") + subject.create_page("test page", "content").should == false + end + + it "stores an error message when a duplicate page exists" do + 2.times { subject.create_page("test page", "content") } + subject.error_message.should =~ /Duplicate page:/ + end + + it "sets the correct commit message" do + subject.create_page("test page", "some content", :markdown, "commit message") + subject.pages.first.page.version.message.should == "commit message" + end + end + + describe "#update_page" do + before do + create_page("update-page", "some content") + @gollum_page = subject.wiki.paged("update-page") + subject.update_page(@gollum_page, "some other content", :markdown, "updated page") + @page = subject.pages.first.page + end + + after do + destroy_page(@page) + end + + it "updates the content of the page" do + @page.raw_data.should == "some other content" + end + + it "sets the correct commit message" do + @page.version.message.should == "updated page" + end + end + + describe "#delete_page" do + before do + create_page("index", "some content") + @page = subject.wiki.paged("index") + end + + it "deletes the page" do + subject.delete_page(@page) + subject.pages.count.should == 0 + end + end + +end diff --git a/spec/models/wiki_page_spec.rb b/spec/models/wiki_page_spec.rb index 1c70edf..2af164b 100644 --- a/spec/models/wiki_page_spec.rb +++ b/spec/models/wiki_page_spec.rb @@ -22,7 +22,7 @@ describe WikiPage do let(:project) { create(:project) } let(:repository) { project.repository } let(:user) { project.owner } - let(:wiki) { GollumWiki.new(project, user) } + let(:wiki) { ProjectWiki.new(project, user) } subject { WikiPage.new(wiki) } diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb index f2a784d..38aae45 100644 --- a/spec/services/projects/create_service_spec.rb +++ b/spec/services/projects/create_service_spec.rb @@ -42,7 +42,7 @@ describe Projects::CreateService do context 'wiki_enabled true creates wiki repository directory' do before do @project = create_project(@user, @opts) - @path = GollumWiki.new(@project, @user).send(:path_to_repo) + @path = ProjectWiki.new(@project, @user).send(:path_to_repo) end it { File.exists?(@path).should be_true } @@ -52,7 +52,7 @@ describe Projects::CreateService do before do @opts.merge!(wiki_enabled: false) @project = create_project(@user, @opts) - @path = GollumWiki.new(@project, @user).send(:path_to_repo) + @path = ProjectWiki.new(@project, @user).send(:path_to_repo) end it { File.exists?(@path).should be_false } diff --git a/spec/support/test_env.rb b/spec/support/test_env.rb index d237f7a..b1bb65a 100644 --- a/spec/support/test_env.rb +++ b/spec/support/test_env.rb @@ -52,7 +52,7 @@ module TestEnv def setup_stubs() # Use tmp dir for FS manipulations repos_path = testing_path() - GollumWiki.any_instance.stub(:init_repo) do |path| + ProjectWiki.any_instance.stub(:init_repo) do |path| create_temp_repo(File.join(repos_path, "#{path}.git")) end -- libgit2 0.21.2