post_receive.rb 1.87 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
  sidekiq_options queue: :post_receive

6
  def perform(repo_path, identifier, changes)
7 8
    if path = Gitlab.config.repositories.storages.find { |p| repo_path.start_with?(p[1].to_s) }
      repo_path.gsub!(path[1].to_s, "")
9
    else
10
      log("Check gitlab.yml config for correct repositories.storages values. No repository storage path matches \"#{repo_path}\"")
11 12
    end

13 14 15 16
    changes = Base64.decode64(changes) unless changes.include?(' ')
    # Use Sidekiq.logger so arguments can be correlated with execution
    # time and thread ID's.
    Sidekiq.logger.info "changes: #{changes.inspect}" if ENV['SIDEKIQ_LOG_ARGUMENTS']
17
    post_received = Gitlab::GitPostReceive.new(repo_path, identifier, changes)
18

19
    if post_received.project.nil?
20
      log("Triggered hook for non-existing project with full path \"#{repo_path} \"")
21 22
      return false
    end
23

24 25 26 27 28 29 30 31 32
    if post_received.wiki?
      # Nothing defined here yet.
    elsif post_received.regular_project?
      process_project_changes(post_received)
    else
      log("Triggered hook for unidentifiable repository type with full path \"#{repo_path} \"")
      false
    end
  end
33

34 35
  def process_project_changes(post_received)
    post_received.changes.each do |change|
36
      oldrev, newrev, ref = change.strip.split(' ')
37

38
      @user ||= post_received.identify(newrev)
39 40

      unless @user
41
        log("Triggered hook for non-existing user \"#{post_received.identifier} \"")
42 43 44
        return false
      end

45
      if Gitlab::Git.tag_ref?(ref)
46
        GitTagPushService.new(post_received.project, @user, oldrev: oldrev, newrev: newrev, ref: ref).execute
47
      elsif Gitlab::Git.branch_ref?(ref)
48
        GitPushService.new(post_received.project, @user, oldrev: oldrev, newrev: newrev, ref: ref).execute
49
      end
50
    end
51
  end
52

53
  private
54

55 56 57
  def log(message)
    Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
  end
58
end