BigW Consortium Gitlab

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

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 15
    repo_path.gsub!(/\.git\z/, "")
    repo_path.gsub!(/\A\//, "")
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 25 26
    changes = Base64.decode64(changes) unless changes.include?(" ")
    changes = utf8_encode_changes(changes)
    changes = changes.lines
27

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

31 32 33 34 35 36 37
      @user ||= identify(identifier, project, newrev)

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

38
      if Gitlab::Git.tag_ref?(ref)
39 40 41 42
        GitTagPushService.new.execute(project, @user, oldrev, newrev, ref)
      else
        GitPushService.new.execute(project, @user, oldrev, newrev, ref)
      end
43
    end
44
  end
45

46 47
  def utf8_encode_changes(changes)
    changes = changes.dup
48

49 50 51 52 53 54 55 56 57 58
    changes.force_encoding("UTF-8")
    return changes if changes.valid_encoding?

    # Convert non-UTF-8 branch/tag names to UTF-8 so they can be dumped as JSON.
    detection = CharlockHolmes::EncodingDetector.detect(changes)
    return changes unless detection && detection[:encoding]

    CharlockHolmes::Converter.convert(changes, detection[:encoding], 'UTF-8')
  end

59 60 61
  def log(message)
    Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
  end
62
end