diff options
author | Francisco Javier López <fjlopez@gitlab.com> | 2018-04-18 17:50:56 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-04-18 17:50:56 +0000 |
commit | e8a27a67fadfa8e62d1c72979281bcd74c39c489 (patch) | |
tree | 0bb3612890dcce48600c9f88352daaf1cb8b73a2 /lib | |
parent | 6a5de6dd3bc122be0473d80ba213de71be6b0fe8 (diff) | |
download | gitlab-ce-e8a27a67fadfa8e62d1c72979281bcd74c39c489.tar.gz |
Fix Custom hooks are not triggered by UI wiki edit
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/git/wiki.rb | 60 | ||||
-rw-r--r-- | lib/gitlab/gitaly_client/wiki_service.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/gl_id.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/wiki/committer_with_hooks.rb | 39 |
4 files changed, 84 insertions, 25 deletions
diff --git a/lib/gitlab/git/wiki.rb b/lib/gitlab/git/wiki.rb index 8d82820915d..821436911ab 100644 --- a/lib/gitlab/git/wiki.rb +++ b/lib/gitlab/git/wiki.rb @@ -2,10 +2,11 @@ module Gitlab module Git class Wiki DuplicatePageError = Class.new(StandardError) + OperationError = Class.new(StandardError) - CommitDetails = Struct.new(:name, :email, :message) do + CommitDetails = Struct.new(:user_id, :username, :name, :email, :message) do def to_h - { name: name, email: email, message: message } + { user_id: user_id, username: username, name: name, email: email, message: message } end end PageBlob = Struct.new(:name) @@ -140,6 +141,10 @@ module Gitlab end end + def gollum_wiki + @gollum_wiki ||= Gollum::Wiki.new(@repository.path) + end + private # options: @@ -158,10 +163,6 @@ module Gitlab offset: options[:offset]) end - def gollum_wiki - @gollum_wiki ||= Gollum::Wiki.new(@repository.path) - end - def gollum_page_by_path(page_path) page_name = Gollum::Page.canonicalize_filename(page_path) page_dir = File.split(page_path).first @@ -201,12 +202,12 @@ module Gitlab assert_type!(format, Symbol) assert_type!(commit_details, CommitDetails) - filename = File.basename(name) - dir = (tmp_dir = File.dirname(name)) == '.' ? '' : tmp_dir - - gollum_wiki.write_page(filename, format, content, commit_details.to_h, dir) + with_committer_with_hooks(commit_details) do |committer| + filename = File.basename(name) + dir = (tmp_dir = File.dirname(name)) == '.' ? '' : tmp_dir - nil + gollum_wiki.write_page(filename, format, content, { committer: committer }, dir) + end rescue Gollum::DuplicatePageError => e raise Gitlab::Git::Wiki::DuplicatePageError, e.message end @@ -214,24 +215,23 @@ module Gitlab def gollum_delete_page(page_path, commit_details) assert_type!(commit_details, CommitDetails) - gollum_wiki.delete_page(gollum_page_by_path(page_path), commit_details.to_h) - nil + with_committer_with_hooks(commit_details) do |committer| + gollum_wiki.delete_page(gollum_page_by_path(page_path), committer: committer) + end end def gollum_update_page(page_path, title, format, content, commit_details) assert_type!(format, Symbol) assert_type!(commit_details, CommitDetails) - page = gollum_page_by_path(page_path) - committer = Gollum::Committer.new(page.wiki, commit_details.to_h) - - # Instead of performing two renames if the title has changed, - # the update_page will only update the format and content and - # the rename_page will do anything related to moving/renaming - gollum_wiki.update_page(page, page.name, format, content, committer: committer) - gollum_wiki.rename_page(page, title, committer: committer) - committer.commit - nil + with_committer_with_hooks(commit_details) do |committer| + page = gollum_page_by_path(page_path) + # Instead of performing two renames if the title has changed, + # the update_page will only update the format and content and + # the rename_page will do anything related to moving/renaming + gollum_wiki.update_page(page, page.name, format, content, committer: committer) + gollum_wiki.rename_page(page, title, committer: committer) + end end def gollum_find_page(title:, version: nil, dir: nil) @@ -288,6 +288,20 @@ module Gitlab Gitlab::Git::WikiPage.new(wiki_page, version) end end + + def committer_with_hooks(commit_details) + Gitlab::Wiki::CommitterWithHooks.new(self, commit_details.to_h) + end + + def with_committer_with_hooks(commit_details, &block) + committer = committer_with_hooks(commit_details) + + yield committer + + committer.commit + + nil + end end end end diff --git a/lib/gitlab/gitaly_client/wiki_service.rb b/lib/gitlab/gitaly_client/wiki_service.rb index 7a698e4b3f3..2dfe055a496 100644 --- a/lib/gitlab/gitaly_client/wiki_service.rb +++ b/lib/gitlab/gitaly_client/wiki_service.rb @@ -200,6 +200,8 @@ module Gitlab def gitaly_commit_details(commit_details) Gitaly::WikiCommitDetails.new( + user_id: commit_details.user_id, + user_name: encode_binary(commit_details.username), name: encode_binary(commit_details.name), email: encode_binary(commit_details.email), message: encode_binary(commit_details.message) diff --git a/lib/gitlab/gl_id.rb b/lib/gitlab/gl_id.rb index 624fd00367e..a53d156b41f 100644 --- a/lib/gitlab/gl_id.rb +++ b/lib/gitlab/gl_id.rb @@ -2,10 +2,14 @@ module Gitlab module GlId def self.gl_id(user) if user.present? - "user-#{user.id}" + gl_id_from_id_value(user.id) else - "" + '' end end + + def self.gl_id_from_id_value(id) + "user-#{id}" + end end end diff --git a/lib/gitlab/wiki/committer_with_hooks.rb b/lib/gitlab/wiki/committer_with_hooks.rb new file mode 100644 index 00000000000..19f0b3814fd --- /dev/null +++ b/lib/gitlab/wiki/committer_with_hooks.rb @@ -0,0 +1,39 @@ +module Gitlab + module Wiki + class CommitterWithHooks < Gollum::Committer + attr_reader :gl_wiki + + def initialize(gl_wiki, options = {}) + @gl_wiki = gl_wiki + super(gl_wiki.gollum_wiki, options) + end + + def commit + result = Gitlab::Git::OperationService.new(git_user, gl_wiki.repository).with_branch( + @wiki.ref, + start_branch_name: @wiki.ref + ) do |start_commit| + super(false) + end + + result[:newrev] + rescue Gitlab::Git::HooksService::PreReceiveError => e + message = "Custom Hook failed: #{e.message}" + raise Gitlab::Git::Wiki::OperationError, message + end + + private + + def git_user + @git_user ||= Gitlab::Git::User.new(@options[:username], + @options[:name], + @options[:email], + gitlab_id) + end + + def gitlab_id + Gitlab::GlId.gl_id_from_id_value(@options[:user_id]) + end + end + end +end |