committer_with_hooks.rb 1.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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