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

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

7
  def perform(repo_path, identifier, changes)
8 9
    if repo_path.start_with?(Gitlab.config.gitlab_shell.repos_path.to_s)
      repo_path.gsub!(Gitlab.config.gitlab_shell.repos_path.to_s, "")
10
    else
11
      log("Check gitlab.yml config for correct gitlab_shell.repos_path variable. \"#{Gitlab.config.gitlab_shell.repos_path}\" does not match \"#{repo_path}\"")
12 13
    end

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

    project = Project.find_with_namespace(repo_path)
18 19

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

24
    changes = changes.lines if changes.kind_of?(String)
25

26 27
    changes.each do |change|
      oldrev, newrev, ref = change.strip.split(' ')
28

29 30 31 32 33 34 35 36 37 38 39 40
      @user ||= identify(identifier, project, newrev)

      unless @user
        log("Triggered hook for non-existing user \"#{identifier} \"")
        return false
      end

      if tag?(ref)
        GitTagPushService.new.execute(project, @user, oldrev, newrev, ref)
      else
        GitPushService.new.execute(project, @user, oldrev, newrev, ref)
      end
41
    end
42
  end
43 44 45 46

  def log(message)
    Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
  end
47 48 49 50 51 52

  private

  def tag?(ref)
    !!(/refs\/tags\/(.*)/.match(ref))
  end
53
end