post_receive.rb 1.4 KB
Newer Older
1
class PostReceive
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
2
  include Sidekiq::Worker
3

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4 5 6
  sidekiq_options queue: :post_receive

  def perform(repo_path, oldrev, newrev, ref, identifier)
7 8 9 10 11 12 13

    if repo_path.start_with?(Gitlab.config.gitolite.repos_path.to_s)
      repo_path.gsub!(Gitlab.config.gitolite.repos_path.to_s, "")
    else
      Gitlab::GitLogger.error("POST-RECEIVE: Check gitlab.yml config for correct gitolite.repos_path variable. \"#{Gitlab.config.gitolite.repos_path}\" does not match \"#{repo_path}\"")
    end

14 15
    repo_path.gsub!(/.git$/, "")
    repo_path.gsub!(/^\//, "")
16 17

    project = Project.find_with_namespace(repo_path)
18 19 20 21 22

    if project.nil?
      Gitlab::GitLogger.error("POST-RECEIVE: Triggered hook for non-existing project with full path \"#{repo_path} \"")
      return false
    end
23

24
    # Ignore push from non-gitlab users
25
    user = if identifier.eql? Gitlab.config.gitolite.admin_key
26 27 28 29 30
             email = project.repository.commit(newrev).author.email rescue nil
             User.find_by_email(email) if email
           elsif /^[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}$/.match(identifier)
             User.find_by_email(identifier)
           else
31
             User.find_by_username(identifier.strip)
32 33
           end

34 35 36 37
    unless user
      Gitlab::GitLogger.error("POST-RECEIVE: Triggered hook for non-existing user \"#{identifier} \"")
      return false
    end
38

39
    project.trigger_post_receive(oldrev, newrev, ref, user)
40 41
  end
end