debian-mirror-gitlab/app/workers/post_receive.rb

63 lines
1.9 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class PostReceive
include Sidekiq::Worker
include Gitlab::Identifier
sidekiq_options queue: :post_receive
2015-04-26 12:48:37 +05:30
def perform(repo_path, identifier, changes)
2014-09-02 18:07:02 +05:30
if repo_path.start_with?(Gitlab.config.gitlab_shell.repos_path.to_s)
repo_path.gsub!(Gitlab.config.gitlab_shell.repos_path.to_s, "")
else
log("Check gitlab.yml config for correct gitlab_shell.repos_path variable. \"#{Gitlab.config.gitlab_shell.repos_path}\" does not match \"#{repo_path}\"")
end
2015-04-26 12:48:37 +05:30
repo_path.gsub!(/\.git\z/, "")
repo_path.gsub!(/\A\//, "")
2014-09-02 18:07:02 +05:30
project = Project.find_with_namespace(repo_path)
if project.nil?
log("Triggered hook for non-existing project with full path \"#{repo_path} \"")
return false
end
2015-04-26 12:48:37 +05:30
changes = Base64.decode64(changes) unless changes.include?(" ")
changes = utf8_encode_changes(changes)
changes = changes.lines
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
changes.each do |change|
oldrev, newrev, ref = change.strip.split(' ')
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
@user ||= identify(identifier, project, newrev)
unless @user
log("Triggered hook for non-existing user \"#{identifier} \"")
return false
end
if Gitlab::Git.tag_ref?(ref)
GitTagPushService.new.execute(project, @user, oldrev, newrev, ref)
else
2016-04-02 18:10:28 +05:30
GitPushService.new(project, @user, oldrev: oldrev, newrev: newrev, ref: ref).execute
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
2015-04-26 12:48:37 +05:30
def utf8_encode_changes(changes)
changes = changes.dup
2015-09-11 14:41:01 +05:30
2015-04-26 12:48:37 +05:30
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]
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
CharlockHolmes::Converter.convert(changes, detection[:encoding], 'UTF-8')
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def log(message)
Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
2014-09-02 18:07:02 +05:30
end
end