Commit 1bd28994ccde18228a1c295110a283c79dfe70ee

Authored by Dmitriy Zaporozhets
1 parent 26ec74c4

Use ProjectWiki instead of GollumWiki in code

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/models/gollum_wiki.rb
... ... @@ -1,120 +0,0 @@
1   -class GollumWiki
2   - include Gitlab::ShellAdapter
3   -
4   - MARKUPS = {
5   - "Markdown" => :markdown,
6   - "RDoc" => :rdoc
7   - }
8   -
9   - class CouldNotCreateWikiError < StandardError; end
10   -
11   - # Returns a string describing what went wrong after
12   - # an operation fails.
13   - attr_reader :error_message
14   -
15   - def initialize(project, user = nil)
16   - @project = project
17   - @user = user
18   - end
19   -
20   - def path
21   - @project.path + '.wiki'
22   - end
23   -
24   - def path_with_namespace
25   - @project.path_with_namespace + ".wiki"
26   - end
27   -
28   - def url_to_repo
29   - gitlab_shell.url_to_repo(path_with_namespace)
30   - end
31   -
32   - def ssh_url_to_repo
33   - url_to_repo
34   - end
35   -
36   - def http_url_to_repo
37   - [Gitlab.config.gitlab.url, "/", path_with_namespace, ".git"].join('')
38   - end
39   -
40   - # Returns the Gollum::Wiki object.
41   - def wiki
42   - @wiki ||= begin
43   - Gollum::Wiki.new(path_to_repo)
44   - rescue Gollum::NoSuchPathError
45   - create_repo!
46   - end
47   - end
48   -
49   - def empty?
50   - pages.empty?
51   - end
52   -
53   - # Returns an Array of Gitlab WikiPage instances or an
54   - # empty Array if this Wiki has no pages.
55   - def pages
56   - wiki.pages.map { |page| WikiPage.new(self, page, true) }
57   - end
58   -
59   - # Finds a page within the repository based on a tile
60   - # or slug.
61   - #
62   - # title - The human readable or parameterized title of
63   - # the page.
64   - #
65   - # Returns an initialized WikiPage instance or nil
66   - def find_page(title, version = nil)
67   - if page = wiki.page(title, version)
68   - WikiPage.new(self, page, true)
69   - else
70   - nil
71   - end
72   - end
73   -
74   - def create_page(title, content, format = :markdown, message = nil)
75   - commit = commit_details(:created, message, title)
76   -
77   - wiki.write_page(title, format, content, commit)
78   - rescue Gollum::DuplicatePageError => e
79   - @error_message = "Duplicate page: #{e.message}"
80   - return false
81   - end
82   -
83   - def update_page(page, content, format = :markdown, message = nil)
84   - commit = commit_details(:updated, message, page.title)
85   -
86   - wiki.update_page(page, page.name, format, content, commit)
87   - end
88   -
89   - def delete_page(page, message = nil)
90   - wiki.delete_page(page, commit_details(:deleted, message, page.title))
91   - end
92   -
93   - private
94   -
95   - def create_repo!
96   - if init_repo(path_with_namespace)
97   - Gollum::Wiki.new(path_to_repo)
98   - else
99   - raise CouldNotCreateWikiError
100   - end
101   - end
102   -
103   - def init_repo(path_with_namespace)
104   - gitlab_shell.add_repository(path_with_namespace)
105   - end
106   -
107   - def commit_details(action, message = nil, title = nil)
108   - commit_message = message || default_message(action, title)
109   -
110   - {email: @user.email, name: @user.name, message: commit_message}
111   - end
112   -
113   - def default_message(action, title)
114   - "#{@user.username} #{action} page: #{title}"
115   - end
116   -
117   - def path_to_repo
118   - @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git")
119   - end
120   -end
app/services/projects/create_service.rb
... ... @@ -74,8 +74,8 @@ module Projects
74 74 if @project.wiki_enabled?
75 75 begin
76 76 # force the creation of a wiki,
77   - GollumWiki.new(@project, @project.owner).wiki
78   - rescue GollumWiki::CouldNotCreateWikiError => ex
  77 + ProjectWiki.new(@project, @project.owner).wiki
  78 + rescue ProjectWiki::CouldNotCreateWikiError => ex
79 79 # Prevent project observer crash
80 80 # if failed to create wiki
81 81 nil
... ...
features/steps/project/wiki.rb
1   -class ProjectWiki < Spinach::FeatureSteps
  1 +class Spinach::Features::ProjectWiki < Spinach::FeatureSteps
2 2 include SharedAuthentication
3 3 include SharedProject
4 4 include SharedNote
... ... @@ -16,7 +16,7 @@ class ProjectWiki &lt; Spinach::FeatureSteps
16 16 end
17 17  
18 18 Given 'I create the Wiki Home page' do
19   - fill_in "Content", with: '[link test](test)'
  19 + fill_in "wiki_content", with: '[link test](test)'
20 20 click_on "Create page"
21 21 end
22 22  
... ... @@ -87,6 +87,6 @@ class ProjectWiki &lt; Spinach::FeatureSteps
87 87 end
88 88  
89 89 def wiki
90   - @gollum_wiki = GollumWiki.new(project, current_user)
  90 + @project_wiki = ProjectWiki.new(project, current_user)
91 91 end
92 92 end
... ...
lib/backup/repository.rb
... ... @@ -24,7 +24,7 @@ module Backup
24 24 puts "[FAILED]".red
25 25 end
26 26  
27   - wiki = GollumWiki.new(project)
  27 + wiki = ProjectWiki.new(project)
28 28  
29 29 if File.exists?(path_to_repo(wiki))
30 30 print " * #{wiki.path_with_namespace} ... "
... ... @@ -59,7 +59,7 @@ module Backup
59 59 puts "[FAILED]".red
60 60 end
61 61  
62   - wiki = GollumWiki.new(project)
  62 + wiki = ProjectWiki.new(project)
63 63  
64 64 if File.exists?(path_to_bundle(wiki))
65 65 print " * #{wiki.path_with_namespace} ... "
... ...
lib/redcarpet/render/gitlab_html.rb
... ... @@ -60,6 +60,8 @@ class Redcarpet::Render::GitlabHTML &lt; Redcarpet::Render::HTML
60 60 end
61 61  
62 62 def is_wiki?
63   - @template.instance_variable_get("@wiki")
  63 + if @template.instance_variable_get("@project_wiki")
  64 + @template.instance_variable_get("@page")
  65 + end
64 66 end
65 67 end
... ...
spec/models/gollum_wiki_spec.rb
... ... @@ -1,211 +0,0 @@
1   -require "spec_helper"
2   -
3   -describe GollumWiki do
4   -
5   - def remove_temp_repo(path)
6   - FileUtils.rm_rf path
7   - end
8   -
9   - def commit_details
10   - commit = {name: user.name, email: user.email, message: "test commit"}
11   - end
12   -
13   - def create_page(name, content)
14   - subject.wiki.write_page(name, :markdown, content, commit_details)
15   - end
16   -
17   - def destroy_page(page)
18   - subject.wiki.delete_page(page, commit_details)
19   - end
20   -
21   - let(:project) { create(:project) }
22   - let(:repository) { project.repository }
23   - let(:user) { project.owner }
24   - let(:gitlab_shell) { Gitlab::Shell.new }
25   -
26   - subject { GollumWiki.new(project, user) }
27   -
28   - before do
29   - create_temp_repo(subject.send(:path_to_repo))
30   - end
31   -
32   - describe "#path_with_namespace" do
33   - it "returns the project path with namespace with the .wiki extension" do
34   - subject.path_with_namespace.should == project.path_with_namespace + ".wiki"
35   - end
36   - end
37   -
38   - describe "#url_to_repo" do
39   - it "returns the correct ssh url to the repo" do
40   - subject.url_to_repo.should == gitlab_shell.url_to_repo(subject.path_with_namespace)
41   - end
42   - end
43   -
44   - describe "#ssh_url_to_repo" do
45   - it "equals #url_to_repo" do
46   - subject.ssh_url_to_repo.should == subject.url_to_repo
47   - end
48   - end
49   -
50   - describe "#http_url_to_repo" do
51   - it "provides the full http url to the repo" do
52   - gitlab_url = Gitlab.config.gitlab.url
53   - repo_http_url = "#{gitlab_url}/#{subject.path_with_namespace}.git"
54   - subject.http_url_to_repo.should == repo_http_url
55   - end
56   - end
57   -
58   - describe "#wiki" do
59   - it "contains a Gollum::Wiki instance" do
60   - subject.wiki.should be_a Gollum::Wiki
61   - end
62   -
63   - before do
64   - Gitlab::Shell.any_instance.stub(:add_repository) do
65   - create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
66   - end
67   - project.stub(:path_with_namespace).and_return("non-existant")
68   - end
69   -
70   - it "creates a new wiki repo if one does not yet exist" do
71   - wiki = GollumWiki.new(project, user)
72   - wiki.create_page("index", "test content").should_not == false
73   -
74   - FileUtils.rm_rf wiki.send(:path_to_repo)
75   - end
76   -
77   - it "raises CouldNotCreateWikiError if it can't create the wiki repository" do
78   - GollumWiki.any_instance.stub(:init_repo).and_return(false)
79   - expect { GollumWiki.new(project, user).wiki }.to raise_exception(GollumWiki::CouldNotCreateWikiError)
80   - end
81   - end
82   -
83   - describe "#empty?" do
84   - context "when the wiki repository is empty" do
85   - before do
86   - Gitlab::Shell.any_instance.stub(:add_repository) do
87   - create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
88   - end
89   - project.stub(:path_with_namespace).and_return("non-existant")
90   - end
91   -
92   - its(:empty?) { should be_true }
93   - end
94   -
95   - context "when the wiki has pages" do
96   - before do
97   - create_page("index", "This is an awesome new Gollum Wiki")
98   - end
99   -
100   - its(:empty?) { should be_false }
101   - end
102   - end
103   -
104   - describe "#pages" do
105   - before do
106   - create_page("index", "This is an awesome new Gollum Wiki")
107   - @pages = subject.pages
108   - end
109   -
110   - after do
111   - destroy_page(@pages.first.page)
112   - end
113   -
114   - it "returns an array of WikiPage instances" do
115   - @pages.first.should be_a WikiPage
116   - end
117   -
118   - it "returns the correct number of pages" do
119   - @pages.count.should == 1
120   - end
121   - end
122   -
123   - describe "#find_page" do
124   - before do
125   - create_page("index page", "This is an awesome Gollum Wiki")
126   - end
127   -
128   - after do
129   - destroy_page(subject.pages.first.page)
130   - end
131   -
132   - it "returns the latest version of the page if it exists" do
133   - page = subject.find_page("index page")
134   - page.title.should == "index page"
135   - end
136   -
137   - it "returns nil if the page does not exist" do
138   - subject.find_page("non-existant").should == nil
139   - end
140   -
141   - it "can find a page by slug" do
142   - page = subject.find_page("index-page")
143   - page.title.should == "index page"
144   - end
145   -
146   - it "returns a WikiPage instance" do
147   - page = subject.find_page("index page")
148   - page.should be_a WikiPage
149   - end
150   - end
151   -
152   - describe "#create_page" do
153   - after do
154   - destroy_page(subject.pages.first.page)
155   - end
156   -
157   - it "creates a new wiki page" do
158   - subject.create_page("test page", "this is content").should_not == false
159   - subject.pages.count.should == 1
160   - end
161   -
162   - it "returns false when a duplicate page exists" do
163   - subject.create_page("test page", "content")
164   - subject.create_page("test page", "content").should == false
165   - end
166   -
167   - it "stores an error message when a duplicate page exists" do
168   - 2.times { subject.create_page("test page", "content") }
169   - subject.error_message.should =~ /Duplicate page:/
170   - end
171   -
172   - it "sets the correct commit message" do
173   - subject.create_page("test page", "some content", :markdown, "commit message")
174   - subject.pages.first.page.version.message.should == "commit message"
175   - end
176   - end
177   -
178   - describe "#update_page" do
179   - before do
180   - create_page("update-page", "some content")
181   - @gollum_page = subject.wiki.paged("update-page")
182   - subject.update_page(@gollum_page, "some other content", :markdown, "updated page")
183   - @page = subject.pages.first.page
184   - end
185   -
186   - after do
187   - destroy_page(@page)
188   - end
189   -
190   - it "updates the content of the page" do
191   - @page.raw_data.should == "some other content"
192   - end
193   -
194   - it "sets the correct commit message" do
195   - @page.version.message.should == "updated page"
196   - end
197   - end
198   -
199   - describe "#delete_page" do
200   - before do
201   - create_page("index", "some content")
202   - @page = subject.wiki.paged("index")
203   - end
204   -
205   - it "deletes the page" do
206   - subject.delete_page(@page)
207   - subject.pages.count.should == 0
208   - end
209   - end
210   -
211   -end
spec/models/project_wiki_spec.rb 0 → 100644
... ... @@ -0,0 +1,211 @@
  1 +require "spec_helper"
  2 +
  3 +describe ProjectWiki do
  4 +
  5 + def remove_temp_repo(path)
  6 + FileUtils.rm_rf path
  7 + end
  8 +
  9 + def commit_details
  10 + commit = {name: user.name, email: user.email, message: "test commit"}
  11 + end
  12 +
  13 + def create_page(name, content)
  14 + subject.wiki.write_page(name, :markdown, content, commit_details)
  15 + end
  16 +
  17 + def destroy_page(page)
  18 + subject.wiki.delete_page(page, commit_details)
  19 + end
  20 +
  21 + let(:project) { create(:project) }
  22 + let(:repository) { project.repository }
  23 + let(:user) { project.owner }
  24 + let(:gitlab_shell) { Gitlab::Shell.new }
  25 +
  26 + subject { ProjectWiki.new(project, user) }
  27 +
  28 + before do
  29 + create_temp_repo(subject.send(:path_to_repo))
  30 + end
  31 +
  32 + describe "#path_with_namespace" do
  33 + it "returns the project path with namespace with the .wiki extension" do
  34 + subject.path_with_namespace.should == project.path_with_namespace + ".wiki"
  35 + end
  36 + end
  37 +
  38 + describe "#url_to_repo" do
  39 + it "returns the correct ssh url to the repo" do
  40 + subject.url_to_repo.should == gitlab_shell.url_to_repo(subject.path_with_namespace)
  41 + end
  42 + end
  43 +
  44 + describe "#ssh_url_to_repo" do
  45 + it "equals #url_to_repo" do
  46 + subject.ssh_url_to_repo.should == subject.url_to_repo
  47 + end
  48 + end
  49 +
  50 + describe "#http_url_to_repo" do
  51 + it "provides the full http url to the repo" do
  52 + gitlab_url = Gitlab.config.gitlab.url
  53 + repo_http_url = "#{gitlab_url}/#{subject.path_with_namespace}.git"
  54 + subject.http_url_to_repo.should == repo_http_url
  55 + end
  56 + end
  57 +
  58 + describe "#wiki" do
  59 + it "contains a Gollum::Wiki instance" do
  60 + subject.wiki.should be_a Gollum::Wiki
  61 + end
  62 +
  63 + before do
  64 + Gitlab::Shell.any_instance.stub(:add_repository) do
  65 + create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
  66 + end
  67 + project.stub(:path_with_namespace).and_return("non-existant")
  68 + end
  69 +
  70 + it "creates a new wiki repo if one does not yet exist" do
  71 + wiki = ProjectWiki.new(project, user)
  72 + wiki.create_page("index", "test content").should_not == false
  73 +
  74 + FileUtils.rm_rf wiki.send(:path_to_repo)
  75 + end
  76 +
  77 + it "raises CouldNotCreateWikiError if it can't create the wiki repository" do
  78 + ProjectWiki.any_instance.stub(:init_repo).and_return(false)
  79 + expect { ProjectWiki.new(project, user).wiki }.to raise_exception(ProjectWiki::CouldNotCreateWikiError)
  80 + end
  81 + end
  82 +
  83 + describe "#empty?" do
  84 + context "when the wiki repository is empty" do
  85 + before do
  86 + Gitlab::Shell.any_instance.stub(:add_repository) do
  87 + create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
  88 + end
  89 + project.stub(:path_with_namespace).and_return("non-existant")
  90 + end
  91 +
  92 + its(:empty?) { should be_true }
  93 + end
  94 +
  95 + context "when the wiki has pages" do
  96 + before do
  97 + create_page("index", "This is an awesome new Gollum Wiki")
  98 + end
  99 +
  100 + its(:empty?) { should be_false }
  101 + end
  102 + end
  103 +
  104 + describe "#pages" do
  105 + before do
  106 + create_page("index", "This is an awesome new Gollum Wiki")
  107 + @pages = subject.pages
  108 + end
  109 +
  110 + after do
  111 + destroy_page(@pages.first.page)
  112 + end
  113 +
  114 + it "returns an array of WikiPage instances" do
  115 + @pages.first.should be_a WikiPage
  116 + end
  117 +
  118 + it "returns the correct number of pages" do
  119 + @pages.count.should == 1
  120 + end
  121 + end
  122 +
  123 + describe "#find_page" do
  124 + before do
  125 + create_page("index page", "This is an awesome Gollum Wiki")
  126 + end
  127 +
  128 + after do
  129 + destroy_page(subject.pages.first.page)
  130 + end
  131 +
  132 + it "returns the latest version of the page if it exists" do
  133 + page = subject.find_page("index page")
  134 + page.title.should == "index page"
  135 + end
  136 +
  137 + it "returns nil if the page does not exist" do
  138 + subject.find_page("non-existant").should == nil
  139 + end
  140 +
  141 + it "can find a page by slug" do
  142 + page = subject.find_page("index-page")
  143 + page.title.should == "index page"
  144 + end
  145 +
  146 + it "returns a WikiPage instance" do
  147 + page = subject.find_page("index page")
  148 + page.should be_a WikiPage
  149 + end
  150 + end
  151 +
  152 + describe "#create_page" do
  153 + after do
  154 + destroy_page(subject.pages.first.page)
  155 + end
  156 +
  157 + it "creates a new wiki page" do
  158 + subject.create_page("test page", "this is content").should_not == false
  159 + subject.pages.count.should == 1
  160 + end
  161 +
  162 + it "returns false when a duplicate page exists" do
  163 + subject.create_page("test page", "content")
  164 + subject.create_page("test page", "content").should == false
  165 + end
  166 +
  167 + it "stores an error message when a duplicate page exists" do
  168 + 2.times { subject.create_page("test page", "content") }
  169 + subject.error_message.should =~ /Duplicate page:/
  170 + end
  171 +
  172 + it "sets the correct commit message" do
  173 + subject.create_page("test page", "some content", :markdown, "commit message")
  174 + subject.pages.first.page.version.message.should == "commit message"
  175 + end
  176 + end
  177 +
  178 + describe "#update_page" do
  179 + before do
  180 + create_page("update-page", "some content")
  181 + @gollum_page = subject.wiki.paged("update-page")
  182 + subject.update_page(@gollum_page, "some other content", :markdown, "updated page")
  183 + @page = subject.pages.first.page
  184 + end
  185 +
  186 + after do
  187 + destroy_page(@page)
  188 + end
  189 +
  190 + it "updates the content of the page" do
  191 + @page.raw_data.should == "some other content"
  192 + end
  193 +
  194 + it "sets the correct commit message" do
  195 + @page.version.message.should == "updated page"
  196 + end
  197 + end
  198 +
  199 + describe "#delete_page" do
  200 + before do
  201 + create_page("index", "some content")
  202 + @page = subject.wiki.paged("index")
  203 + end
  204 +
  205 + it "deletes the page" do
  206 + subject.delete_page(@page)
  207 + subject.pages.count.should == 0
  208 + end
  209 + end
  210 +
  211 +end
... ...
spec/models/wiki_page_spec.rb
... ... @@ -22,7 +22,7 @@ describe WikiPage do
22 22 let(:project) { create(:project) }
23 23 let(:repository) { project.repository }
24 24 let(:user) { project.owner }
25   - let(:wiki) { GollumWiki.new(project, user) }
  25 + let(:wiki) { ProjectWiki.new(project, user) }
26 26  
27 27 subject { WikiPage.new(wiki) }
28 28  
... ...
spec/services/projects/create_service_spec.rb
... ... @@ -42,7 +42,7 @@ describe Projects::CreateService do
42 42 context 'wiki_enabled true creates wiki repository directory' do
43 43 before do
44 44 @project = create_project(@user, @opts)
45   - @path = GollumWiki.new(@project, @user).send(:path_to_repo)
  45 + @path = ProjectWiki.new(@project, @user).send(:path_to_repo)
46 46 end
47 47  
48 48 it { File.exists?(@path).should be_true }
... ... @@ -52,7 +52,7 @@ describe Projects::CreateService do
52 52 before do
53 53 @opts.merge!(wiki_enabled: false)
54 54 @project = create_project(@user, @opts)
55   - @path = GollumWiki.new(@project, @user).send(:path_to_repo)
  55 + @path = ProjectWiki.new(@project, @user).send(:path_to_repo)
56 56 end
57 57  
58 58 it { File.exists?(@path).should be_false }
... ...
spec/support/test_env.rb
... ... @@ -52,7 +52,7 @@ module TestEnv
52 52 def setup_stubs()
53 53 # Use tmp dir for FS manipulations
54 54 repos_path = testing_path()
55   - GollumWiki.any_instance.stub(:init_repo) do |path|
  55 + ProjectWiki.any_instance.stub(:init_repo) do |path|
56 56 create_temp_repo(File.join(repos_path, "#{path}.git"))
57 57 end
58 58  
... ...