Commit fae6eb63535f6136704365692f4c34ccfd6eba67

Authored by Dmitriy Zaporozhets
2 parents 71678f08 1bd28994

Merge branch 'wiki-refactoring' into 'master'

Wiki refactoring

Better names for variables and classes
app/controllers/projects/wikis_controller.rb
  1 +require 'project_wiki'
  2 +
1 class Projects::WikisController < Projects::ApplicationController 3 class Projects::WikisController < Projects::ApplicationController
2 before_filter :authorize_read_wiki! 4 before_filter :authorize_read_wiki!
3 before_filter :authorize_write_wiki!, only: [:edit, :create, :history] 5 before_filter :authorize_write_wiki!, only: [:edit, :create, :history]
4 before_filter :authorize_admin_wiki!, only: :destroy 6 before_filter :authorize_admin_wiki!, only: :destroy
5 - before_filter :load_gollum_wiki 7 + before_filter :load_project_wiki
6 8
7 def pages 9 def pages
8 - @wiki_pages = @gollum_wiki.pages 10 + @wiki_pages = @project_wiki.pages
9 end 11 end
10 12
11 def show 13 def show
12 - @wiki = @gollum_wiki.find_page(params[:id], params[:version_id]) 14 + @page = @project_wiki.find_page(params[:id], params[:version_id])
13 15
14 - if @wiki 16 + if @page
15 render 'show' 17 render 'show'
16 else 18 else
17 return render('empty') unless can?(current_user, :write_wiki, @project) 19 return render('empty') unless can?(current_user, :write_wiki, @project)
18 - @wiki = WikiPage.new(@gollum_wiki)  
19 - @wiki.title = params[:id] 20 + @page = WikiPage.new(@project_wiki)
  21 + @page.title = params[:id]
20 22
21 render 'edit' 23 render 'edit'
22 end 24 end
23 end 25 end
24 26
25 def edit 27 def edit
26 - @wiki = @gollum_wiki.find_page(params[:id]) 28 + @page = @project_wiki.find_page(params[:id])
27 end 29 end
28 30
29 def update 31 def update
30 - @wiki = @gollum_wiki.find_page(params[:id]) 32 + @page = @project_wiki.find_page(params[:id])
31 33
32 return render('empty') unless can?(current_user, :write_wiki, @project) 34 return render('empty') unless can?(current_user, :write_wiki, @project)
33 35
34 - if @wiki.update(content, format, message)  
35 - redirect_to [@project, @wiki], notice: 'Wiki was successfully updated.' 36 + if @page.update(content, format, message)
  37 + redirect_to [@project, @page], notice: 'Wiki was successfully updated.'
36 else 38 else
37 render 'edit' 39 render 'edit'
38 end 40 end
39 end 41 end
40 42
41 def create 43 def create
42 - @wiki = WikiPage.new(@gollum_wiki) 44 + @page = WikiPage.new(@project_wiki)
43 45
44 - if @wiki.create(wiki_params)  
45 - redirect_to project_wiki_path(@project, @wiki), notice: 'Wiki was successfully updated.' 46 + if @page.create(wiki_params)
  47 + redirect_to project_wiki_path(@project, @page), notice: 'Wiki was successfully updated.'
46 else 48 else
47 render action: "edit" 49 render action: "edit"
48 end 50 end
49 end 51 end
50 52
51 def history 53 def history
52 - @wiki = @gollum_wiki.find_page(params[:id]) 54 + @page = @project_wiki.find_page(params[:id])
53 55
54 - redirect_to(project_wiki_path(@project, :home), notice: "Page not found") unless @wiki 56 + unless @page
  57 + redirect_to(project_wiki_path(@project, :home), notice: "Page not found")
  58 + end
55 end 59 end
56 60
57 def destroy 61 def destroy
58 - @wiki = @gollum_wiki.find_page(params[:id])  
59 - @wiki.delete if @wiki 62 + @page = @project_wiki.find_page(params[:id])
  63 + @page.delete if @page
  64 +
60 redirect_to project_wiki_path(@project, :home), notice: "Page was successfully deleted" 65 redirect_to project_wiki_path(@project, :home), notice: "Page was successfully deleted"
61 end 66 end
62 67
@@ -65,12 +70,12 @@ class Projects::WikisController &lt; Projects::ApplicationController @@ -65,12 +70,12 @@ class Projects::WikisController &lt; Projects::ApplicationController
65 70
66 private 71 private
67 72
68 - def load_gollum_wiki  
69 - @gollum_wiki = GollumWiki.new(@project, current_user) 73 + def load_project_wiki
  74 + @project_wiki = ProjectWiki.new(@project, current_user)
70 75
71 # Call #wiki to make sure the Wiki Repo is initialized 76 # Call #wiki to make sure the Wiki Repo is initialized
72 - @gollum_wiki.wiki  
73 - rescue GollumWiki::CouldNotCreateWikiError => ex 77 + @project_wiki.wiki
  78 + rescue ProjectWiki::CouldNotCreateWikiError => ex
74 flash[:notice] = "Could not create Wiki Repository at this time. Please try again later." 79 flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
75 redirect_to @project 80 redirect_to @project
76 return false 81 return false
@@ -91,5 +96,4 @@ class Projects::WikisController &lt; Projects::ApplicationController @@ -91,5 +96,4 @@ class Projects::WikisController &lt; Projects::ApplicationController
91 def message 96 def message
92 params[:wiki][:message] 97 params[:wiki][:message]
93 end 98 end
94 -  
95 end 99 end
app/models/gollum_wiki.rb
@@ -1,120 +0,0 @@ @@ -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/models/project_wiki.rb 0 → 100644
@@ -0,0 +1,120 @@ @@ -0,0 +1,120 @@
  1 +class ProjectWiki
  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/models/wiki_page.rb
@@ -19,7 +19,7 @@ class WikiPage @@ -19,7 +19,7 @@ class WikiPage
19 validates :title, presence: true 19 validates :title, presence: true
20 validates :content, presence: true 20 validates :content, presence: true
21 21
22 - # The Gitlab GollumWiki instance. 22 + # The Gitlab ProjectWiki instance.
23 attr_reader :wiki 23 attr_reader :wiki
24 24
25 # The raw Gollum::Page instance. 25 # The raw Gollum::Page instance.
@@ -118,7 +118,7 @@ class WikiPage @@ -118,7 +118,7 @@ class WikiPage
118 # :content - The raw markup content. 118 # :content - The raw markup content.
119 # :format - Optional symbol representing the 119 # :format - Optional symbol representing the
120 # content format. Can be any type 120 # content format. Can be any type
121 - # listed in the GollumWiki::MARKUPS 121 + # listed in the ProjectWiki::MARKUPS
122 # Hash. 122 # Hash.
123 # :message - Optional commit message to set on 123 # :message - Optional commit message to set on
124 # the new page. 124 # the new page.
@@ -135,7 +135,7 @@ class WikiPage @@ -135,7 +135,7 @@ class WikiPage
135 # 135 #
136 # new_content - The raw markup content to replace the existing. 136 # new_content - The raw markup content to replace the existing.
137 # format - Optional symbol representing the content format. 137 # format - Optional symbol representing the content format.
138 - # See GollumWiki::MARKUPS Hash for available formats. 138 + # See ProjectWiki::MARKUPS Hash for available formats.
139 # message - Optional commit message to set on the new version. 139 # message - Optional commit message to set on the new version.
140 # 140 #
141 # Returns the String SHA1 of the newly created page 141 # Returns the String SHA1 of the newly created page
@@ -181,5 +181,4 @@ class WikiPage @@ -181,5 +181,4 @@ class WikiPage
181 end 181 end
182 @persisted 182 @persisted
183 end 183 end
184 -  
185 end 184 end
app/services/projects/create_service.rb
@@ -74,8 +74,8 @@ module Projects @@ -74,8 +74,8 @@ module Projects
74 if @project.wiki_enabled? 74 if @project.wiki_enabled?
75 begin 75 begin
76 # force the creation of a wiki, 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 # Prevent project observer crash 79 # Prevent project observer crash
80 # if failed to create wiki 80 # if failed to create wiki
81 nil 81 nil
app/views/projects/wikis/_form.html.haml
1 -= form_for [@project, @wiki], method: @wiki.persisted? ? :put : :post, html: { class: 'form-horizontal' } do |f|  
2 - -if @wiki.errors.any? 1 += form_for [@project, @page], method: @page.persisted? ? :put : :post, html: { class: 'form-horizontal' } do |f|
  2 + -if @page.errors.any?
3 #error_explanation 3 #error_explanation
4 - %h2= "#{pluralize(@wiki.errors.count, "error")} prohibited this wiki from being saved:" 4 + %h2= "#{pluralize(@page.errors.count, "error")} prohibited this wiki from being saved:"
5 %ul 5 %ul
6 - - @wiki.errors.full_messages.each do |msg| 6 + - @page.errors.full_messages.each do |msg|
7 %li= msg 7 %li= msg
8 8
9 - = f.hidden_field :title, value: @wiki.title 9 + = f.hidden_field :title, value: @page.title
10 .form-group 10 .form-group
11 = f.label :format, class: 'control-label' 11 = f.label :format, class: 'control-label'
12 .col-sm-10 12 .col-sm-10
13 - = f.select :format, options_for_select(GollumWiki::MARKUPS, {selected: @wiki.format}), {}, class: "form-control" 13 + = f.select :format, options_for_select(ProjectWiki::MARKUPS, {selected: @page.format}), {}, class: "form-control"
14 14
15 .row 15 .row
16 .col-sm-2 16 .col-sm-2
@@ -31,9 +31,9 @@ @@ -31,9 +31,9 @@
31 .col-sm-10= f.text_field :message, class: 'form-control', rows: 18 31 .col-sm-10= f.text_field :message, class: 'form-control', rows: 18
32 32
33 .form-actions 33 .form-actions
34 - - if @wiki && @wiki.persisted? 34 + - if @page && @page.persisted?
35 = f.submit 'Save changes', class: "btn-save btn" 35 = f.submit 'Save changes', class: "btn-save btn"
36 - = link_to "Cancel", project_wiki_path(@project, @wiki), class: "btn btn-cancel" 36 + = link_to "Cancel", project_wiki_path(@project, @page), class: "btn btn-cancel"
37 - else 37 - else
38 = f.submit 'Create page', class: "btn-create btn" 38 = f.submit 'Create page', class: "btn-create btn"
39 = link_to "Cancel", project_wiki_path(@project, :home), class: "btn btn-cancel" 39 = link_to "Cancel", project_wiki_path(@project, :home), class: "btn btn-cancel"
app/views/projects/wikis/_main_links.html.haml
1 %span.pull-right 1 %span.pull-right
2 - - if (@wiki && @wiki.persisted?)  
3 - = link_to history_project_wiki_path(@project, @wiki), class: "btn btn-grouped" do 2 + - if (@page && @page.persisted?)
  3 + = link_to history_project_wiki_path(@project, @page), class: "btn btn-grouped" do
4 Page History 4 Page History
5 - if can?(current_user, :write_wiki, @project) 5 - if can?(current_user, :write_wiki, @project)
6 - = link_to edit_project_wiki_path(@project, @wiki), class: "btn btn-grouped" do 6 + = link_to edit_project_wiki_path(@project, @page), class: "btn btn-grouped" do
7 %i.icon-edit 7 %i.icon-edit
8 Edit 8 Edit
app/views/projects/wikis/edit.html.haml
@@ -3,11 +3,11 @@ @@ -3,11 +3,11 @@
3 = render 'main_links' 3 = render 'main_links'
4 %h3.page-title 4 %h3.page-title
5 Editing - 5 Editing -
6 - %span.light #{@wiki.title.titleize} 6 + %span.light #{@page.title.titleize}
7 %hr 7 %hr
8 = render 'form' 8 = render 'form'
9 9
10 .pull-right 10 .pull-right
11 - - if @wiki.persisted? && can?(current_user, :admin_wiki, @project)  
12 - = link_to project_wiki_path(@project, @wiki), data: { confirm: "Are you sure you want to delete this page?"}, method: :delete, class: "btn btn-small btn-remove" do 11 + - if @page.persisted? && can?(current_user, :admin_wiki, @project)
  12 + = link_to project_wiki_path(@project, @page), data: { confirm: "Are you sure you want to delete this page?"}, method: :delete, class: "btn btn-small btn-remove" do
13 Delete this page 13 Delete this page
app/views/projects/wikis/git_access.html.haml
@@ -3,10 +3,10 @@ @@ -3,10 +3,10 @@
3 .col-sm-6 3 .col-sm-6
4 %h3.page-title 4 %h3.page-title
5 Git access for 5 Git access for
6 - %strong= @gollum_wiki.path_with_namespace 6 + %strong= @project_wiki.path_with_namespace
7 7
8 .col-sm-6 8 .col-sm-6
9 - = render "shared/clone_panel", project: @gollum_wiki 9 + = render "shared/clone_panel", project: @project_wiki
10 10
11 .git-empty 11 .git-empty
12 %fieldset 12 %fieldset
@@ -18,8 +18,8 @@ @@ -18,8 +18,8 @@
18 %legend Clone Your Wiki: 18 %legend Clone Your Wiki:
19 %pre.dark 19 %pre.dark
20 :preserve 20 :preserve
21 - git clone #{ content_tag(:span, default_url_to_repo(@gollum_wiki), class: 'clone')}  
22 - cd #{@gollum_wiki.path} 21 + git clone #{ content_tag(:span, default_url_to_repo(@project_wiki), class: 'clone')}
  22 + cd #{@project_wiki.path}
23 23
24 %legend Start Gollum And Edit Locally: 24 %legend Start Gollum And Edit Locally:
25 %pre.dark 25 %pre.dark
app/views/projects/wikis/history.html.haml
1 = render 'nav' 1 = render 'nav'
2 %h3.page-title 2 %h3.page-title
3 %span.light History for 3 %span.light History for
4 - = link_to @wiki.title.titleize, project_wiki_path(@project, @wiki) 4 + = link_to @page.title.titleize, project_wiki_path(@project, @page)
5 5
6 %table.table 6 %table.table
7 %thead 7 %thead
@@ -12,11 +12,11 @@ @@ -12,11 +12,11 @@
12 %th Last updated 12 %th Last updated
13 %th Format 13 %th Format
14 %tbody 14 %tbody
15 - - @wiki.versions.each do |version| 15 + - @page.versions.each do |version|
16 - commit = version 16 - commit = version
17 %tr 17 %tr
18 %td 18 %td
19 - = link_to project_wiki_path(@project, @wiki, version_id: commit.id) do 19 + = link_to project_wiki_path(@project, @page, version_id: commit.id) do
20 = commit.short_id 20 = commit.short_id
21 %td 21 %td
22 = commit_author_link(commit, avatar: true, size: 24) 22 = commit_author_link(commit, avatar: true, size: 24)
@@ -26,4 +26,4 @@ @@ -26,4 +26,4 @@
26 #{time_ago_with_tooltip(version.date)} 26 #{time_ago_with_tooltip(version.date)}
27 %td 27 %td
28 %strong 28 %strong
29 - = @wiki.page.wiki.page(@wiki.page.name, commit.id).try(:format) 29 + = @page.page.wiki.page(@page.page.name, commit.id).try(:format)
app/views/projects/wikis/show.html.haml
1 = render 'nav' 1 = render 'nav'
2 %h3.page-title 2 %h3.page-title
3 - = @wiki.title.titleize 3 + = @page.title.titleize
4 = render 'main_links' 4 = render 'main_links'
5 -- if @wiki.historical? 5 +- if @page.historical?
6 .warning_message 6 .warning_message
7 This is an old version of this page. 7 This is an old version of this page.
8 - You can view the #{link_to "most recent version", project_wiki_path(@project, @wiki)} or browse the #{link_to "history", history_project_wiki_path(@project, @wiki)}. 8 + You can view the #{link_to "most recent version", project_wiki_path(@project, @page)} or browse the #{link_to "history", history_project_wiki_path(@project, @page)}.
9 9
10 %hr 10 %hr
11 11
12 .wiki-holder 12 .wiki-holder
13 .wiki 13 .wiki
14 = preserve do 14 = preserve do
15 - = render_wiki_content(@wiki) 15 + = render_wiki_content(@page)
16 16
17 %hr 17 %hr
18 18
19 .wiki-last-edit-by 19 .wiki-last-edit-by
20 - Last edited by #{commit_author_link(@wiki.commit, avatar: true, size: 16)} #{time_ago_with_tooltip(@wiki.commit.created_at)} 20 + Last edited by #{commit_author_link(@page.commit, avatar: true, size: 16)} #{time_ago_with_tooltip(@page.commit.created_at)}
features/steps/project/wiki.rb
1 -class ProjectWiki < Spinach::FeatureSteps 1 +class Spinach::Features::ProjectWiki < Spinach::FeatureSteps
2 include SharedAuthentication 2 include SharedAuthentication
3 include SharedProject 3 include SharedProject
4 include SharedNote 4 include SharedNote
@@ -16,7 +16,7 @@ class ProjectWiki &lt; Spinach::FeatureSteps @@ -16,7 +16,7 @@ class ProjectWiki &lt; Spinach::FeatureSteps
16 end 16 end
17 17
18 Given 'I create the Wiki Home page' do 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 click_on "Create page" 20 click_on "Create page"
21 end 21 end
22 22
@@ -87,6 +87,6 @@ class ProjectWiki &lt; Spinach::FeatureSteps @@ -87,6 +87,6 @@ class ProjectWiki &lt; Spinach::FeatureSteps
87 end 87 end
88 88
89 def wiki 89 def wiki
90 - @gollum_wiki = GollumWiki.new(project, current_user) 90 + @project_wiki = ProjectWiki.new(project, current_user)
91 end 91 end
92 end 92 end
lib/backup/repository.rb
@@ -24,7 +24,7 @@ module Backup @@ -24,7 +24,7 @@ module Backup
24 puts "[FAILED]".red 24 puts "[FAILED]".red
25 end 25 end
26 26
27 - wiki = GollumWiki.new(project) 27 + wiki = ProjectWiki.new(project)
28 28
29 if File.exists?(path_to_repo(wiki)) 29 if File.exists?(path_to_repo(wiki))
30 print " * #{wiki.path_with_namespace} ... " 30 print " * #{wiki.path_with_namespace} ... "
@@ -59,7 +59,7 @@ module Backup @@ -59,7 +59,7 @@ module Backup
59 puts "[FAILED]".red 59 puts "[FAILED]".red
60 end 60 end
61 61
62 - wiki = GollumWiki.new(project) 62 + wiki = ProjectWiki.new(project)
63 63
64 if File.exists?(path_to_bundle(wiki)) 64 if File.exists?(path_to_bundle(wiki))
65 print " * #{wiki.path_with_namespace} ... " 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,6 +60,8 @@ class Redcarpet::Render::GitlabHTML &lt; Redcarpet::Render::HTML
60 end 60 end
61 61
62 def is_wiki? 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 end 66 end
65 end 67 end
spec/models/gollum_wiki_spec.rb
@@ -1,211 +0,0 @@ @@ -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 @@ @@ -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,7 +22,7 @@ describe WikiPage do
22 let(:project) { create(:project) } 22 let(:project) { create(:project) }
23 let(:repository) { project.repository } 23 let(:repository) { project.repository }
24 let(:user) { project.owner } 24 let(:user) { project.owner }
25 - let(:wiki) { GollumWiki.new(project, user) } 25 + let(:wiki) { ProjectWiki.new(project, user) }
26 26
27 subject { WikiPage.new(wiki) } 27 subject { WikiPage.new(wiki) }
28 28
spec/services/projects/create_service_spec.rb
@@ -42,7 +42,7 @@ describe Projects::CreateService do @@ -42,7 +42,7 @@ describe Projects::CreateService do
42 context 'wiki_enabled true creates wiki repository directory' do 42 context 'wiki_enabled true creates wiki repository directory' do
43 before do 43 before do
44 @project = create_project(@user, @opts) 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 end 46 end
47 47
48 it { File.exists?(@path).should be_true } 48 it { File.exists?(@path).should be_true }
@@ -52,7 +52,7 @@ describe Projects::CreateService do @@ -52,7 +52,7 @@ describe Projects::CreateService do
52 before do 52 before do
53 @opts.merge!(wiki_enabled: false) 53 @opts.merge!(wiki_enabled: false)
54 @project = create_project(@user, @opts) 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 end 56 end
57 57
58 it { File.exists?(@path).should be_false } 58 it { File.exists?(@path).should be_false }
spec/support/test_env.rb
@@ -52,7 +52,7 @@ module TestEnv @@ -52,7 +52,7 @@ module TestEnv
52 def setup_stubs() 52 def setup_stubs()
53 # Use tmp dir for FS manipulations 53 # Use tmp dir for FS manipulations
54 repos_path = testing_path() 54 repos_path = testing_path()
55 - GollumWiki.any_instance.stub(:init_repo) do |path| 55 + ProjectWiki.any_instance.stub(:init_repo) do |path|
56 create_temp_repo(File.join(repos_path, "#{path}.git")) 56 create_temp_repo(File.join(repos_path, "#{path}.git"))
57 end 57 end
58 58