2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-12-12 19:03:26 -05:00
|
|
|
class PostReceive
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2013-01-09 00:14:05 -05:00
|
|
|
|
2019-10-18 07:11:44 -04:00
|
|
|
feature_category :source_code_management
|
2019-10-30 11:14:17 -04:00
|
|
|
latency_sensitive_worker!
|
|
|
|
worker_resource_boundary :cpu
|
2020-01-24 13:09:00 -05:00
|
|
|
weight 5
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-04-05 00:19:30 -04:00
|
|
|
def perform(gl_repository, identifier, changes, push_options = {})
|
2019-03-18 12:51:11 -04:00
|
|
|
project, repo_type = Gitlab::GlRepository.parse(gl_repository)
|
2017-05-02 17:15:12 -04:00
|
|
|
|
|
|
|
if project.nil?
|
2017-06-19 18:37:15 -04:00
|
|
|
log("Triggered hook for non-existing project with gl_repository \"#{gl_repository}\"")
|
2017-05-02 17:15:12 -04:00
|
|
|
return false
|
|
|
|
end
|
2013-01-29 04:32:05 -05:00
|
|
|
|
2016-07-28 09:12:49 -04:00
|
|
|
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']
|
2017-11-28 22:50:29 -05:00
|
|
|
post_received = Gitlab::GitPostReceive.new(project, identifier, changes, push_options)
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2019-03-18 12:51:11 -04:00
|
|
|
if repo_type.wiki?
|
2017-06-05 19:37:52 -04:00
|
|
|
process_wiki_changes(post_received)
|
2019-04-22 12:07:19 -04:00
|
|
|
elsif repo_type.project?
|
2017-05-02 17:15:12 -04:00
|
|
|
process_project_changes(post_received)
|
2019-04-22 12:07:19 -04:00
|
|
|
else
|
|
|
|
# Other repos don't have hooks for now
|
2016-03-16 23:24:12 -04:00
|
|
|
end
|
|
|
|
end
|
2013-01-28 10:22:45 -05:00
|
|
|
|
2017-06-05 19:37:52 -04:00
|
|
|
private
|
|
|
|
|
2019-06-12 00:35:19 -04:00
|
|
|
def identify_user(post_received)
|
|
|
|
post_received.identify.tap do |user|
|
|
|
|
log("Triggered hook for non-existing user \"#{post_received.identifier}\"") unless user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-05 19:37:52 -04:00
|
|
|
def process_project_changes(post_received)
|
2019-06-12 00:35:19 -04:00
|
|
|
user = identify_user(post_received)
|
2019-10-03 11:07:07 -04:00
|
|
|
|
2019-06-12 00:35:19 -04:00
|
|
|
return false unless user
|
2014-09-02 03:58:54 -04:00
|
|
|
|
2019-10-03 11:07:07 -04:00
|
|
|
project = post_received.project
|
|
|
|
push_options = post_received.push_options
|
|
|
|
changes = post_received.changes
|
|
|
|
|
2019-08-16 15:53:56 -04:00
|
|
|
# We only need to expire certain caches once per push
|
2019-10-03 05:07:16 -04:00
|
|
|
expire_caches(post_received, post_received.project.repository)
|
|
|
|
enqueue_repository_cache_update(post_received)
|
2019-08-09 06:09:06 -04:00
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
process_ref_changes(project, user, push_options: push_options, changes: changes)
|
2019-10-03 08:06:00 -04:00
|
|
|
update_remote_mirrors(post_received)
|
2019-10-03 11:07:07 -04:00
|
|
|
after_project_changes_hooks(project, user, changes.refs, changes.repository_data)
|
2011-12-12 19:03:26 -05:00
|
|
|
end
|
2013-04-29 02:43:18 -04:00
|
|
|
|
2019-10-03 05:07:16 -04:00
|
|
|
# Expire the repository status, branch, and tag cache once per push.
|
|
|
|
def expire_caches(post_received, repository)
|
|
|
|
repository.expire_status_cache if repository.empty?
|
|
|
|
repository.expire_branches_cache if post_received.includes_branches?
|
|
|
|
repository.expire_caches_for_tags if post_received.includes_tags?
|
2019-08-16 15:53:56 -04:00
|
|
|
end
|
|
|
|
|
2019-10-03 05:07:16 -04:00
|
|
|
# Schedule an update for the repository size and commit count if necessary.
|
2019-08-16 15:53:56 -04:00
|
|
|
def enqueue_repository_cache_update(post_received)
|
|
|
|
stats_to_invalidate = [:repository_size]
|
|
|
|
stats_to_invalidate << :commit_count if post_received.includes_default_branch?
|
|
|
|
|
|
|
|
ProjectCacheWorker.perform_async(
|
|
|
|
post_received.project.id,
|
|
|
|
[],
|
|
|
|
stats_to_invalidate,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
def process_ref_changes(project, user, params = {})
|
|
|
|
return unless params[:changes].any?
|
|
|
|
|
|
|
|
Git::ProcessRefChangesService.new(project, user, params).execute
|
2019-10-03 11:07:07 -04:00
|
|
|
end
|
|
|
|
|
2019-10-03 08:06:00 -04:00
|
|
|
def update_remote_mirrors(post_received)
|
|
|
|
return unless post_received.includes_branches? || post_received.includes_tags?
|
|
|
|
|
|
|
|
project = post_received.project
|
|
|
|
return unless project.has_remote_mirror?
|
|
|
|
|
|
|
|
project.mark_stuck_remote_mirrors_as_failed!
|
|
|
|
project.update_remote_mirrors
|
|
|
|
end
|
|
|
|
|
2019-10-03 11:07:07 -04:00
|
|
|
def after_project_changes_hooks(project, user, refs, changes)
|
|
|
|
repository_update_hook_data = Gitlab::DataBuilder::Repository.update(project, user, changes, refs)
|
|
|
|
SystemHooksService.new.execute_hooks(repository_update_hook_data, :repository_update_hooks)
|
2019-08-13 18:33:16 -04:00
|
|
|
Gitlab::UsageDataCounters::SourceCodeCounter.count(:pushes)
|
2017-06-05 19:37:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_wiki_changes(post_received)
|
2018-03-07 15:16:14 -05:00
|
|
|
post_received.project.touch(:last_activity_at, :last_repository_updated_at)
|
2019-02-13 17:38:11 -05:00
|
|
|
post_received.project.wiki.repository.expire_statistics_caches
|
|
|
|
ProjectCacheWorker.perform_async(post_received.project.id, [], [:wiki_size])
|
2019-06-12 00:35:19 -04:00
|
|
|
|
|
|
|
user = identify_user(post_received)
|
|
|
|
return false unless user
|
|
|
|
|
2019-10-03 05:07:16 -04:00
|
|
|
# We only need to expire certain caches once per push
|
|
|
|
expire_caches(post_received, post_received.project.wiki.repository)
|
|
|
|
|
2019-10-03 11:07:07 -04:00
|
|
|
::Git::WikiPushService.new(post_received.project, user, changes: post_received.changes).execute
|
2017-06-05 19:37:52 -04:00
|
|
|
end
|
2016-04-15 08:26:27 -04:00
|
|
|
|
2013-04-29 02:43:18 -04:00
|
|
|
def log(message)
|
|
|
|
Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
|
|
|
|
end
|
2011-12-12 19:03:26 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
PostReceive.prepend_if_ee('EE::PostReceive')
|