2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-19 13:09:10 -05:00
|
|
|
class PostReceive # rubocop:disable Scalability/IdempotentWorker
|
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
|
2020-03-02 13:07:42 -05:00
|
|
|
urgency :high
|
2019-10-30 11:14:17 -04:00
|
|
|
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 = {})
|
2020-03-04 19:07:49 -05:00
|
|
|
container, project, repo_type = Gitlab::GlRepository.parse(gl_repository)
|
2017-05-02 17:15:12 -04:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
if project.nil? && (!repo_type.snippet? || container.is_a?(ProjectSnippet))
|
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']
|
2020-03-04 19:07:49 -05:00
|
|
|
post_received = Gitlab::GitPostReceive.new(container, identifier, changes, push_options)
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2019-03-18 12:51:11 -04:00
|
|
|
if repo_type.wiki?
|
2020-03-04 19:07:49 -05:00
|
|
|
process_wiki_changes(post_received, container)
|
2019-04-22 12:07:19 -04:00
|
|
|
elsif repo_type.project?
|
2020-03-04 19:07:49 -05:00
|
|
|
process_project_changes(post_received, container)
|
|
|
|
elsif repo_type.snippet?
|
|
|
|
process_snippet_changes(post_received, container)
|
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
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
def process_project_changes(post_received, project)
|
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
|
|
|
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
|
2020-03-04 19:07:49 -05:00
|
|
|
expire_caches(post_received, project.repository)
|
|
|
|
enqueue_project_cache_update(post_received, project)
|
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)
|
2020-03-04 19:07:49 -05:00
|
|
|
update_remote_mirrors(post_received, project)
|
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
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
def process_wiki_changes(post_received, project)
|
|
|
|
project.touch(:last_activity_at, :last_repository_updated_at)
|
|
|
|
project.wiki.repository.expire_statistics_caches
|
|
|
|
ProjectCacheWorker.perform_async(project.id, [], [:wiki_size])
|
|
|
|
|
|
|
|
user = identify_user(post_received)
|
|
|
|
return false unless user
|
|
|
|
|
|
|
|
# We only need to expire certain caches once per push
|
|
|
|
expire_caches(post_received, project.wiki.repository)
|
|
|
|
|
|
|
|
::Git::WikiPushService.new(project, user, changes: post_received.changes).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_snippet_changes(post_received, snippet)
|
|
|
|
user = identify_user(post_received)
|
|
|
|
|
|
|
|
return false unless user
|
|
|
|
|
|
|
|
expire_caches(post_received, snippet.repository)
|
2020-04-09 11:09:29 -04:00
|
|
|
snippet.repository.expire_statistics_caches
|
2020-03-04 19:07:49 -05:00
|
|
|
end
|
|
|
|
|
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.
|
2020-03-04 19:07:49 -05:00
|
|
|
def enqueue_project_cache_update(post_received, project)
|
2019-08-16 15:53:56 -04:00
|
|
|
stats_to_invalidate = [:repository_size]
|
|
|
|
stats_to_invalidate << :commit_count if post_received.includes_default_branch?
|
|
|
|
|
|
|
|
ProjectCacheWorker.perform_async(
|
2020-03-04 19:07:49 -05:00
|
|
|
project.id,
|
2019-08-16 15:53:56 -04:00
|
|
|
[],
|
|
|
|
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
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
def update_remote_mirrors(post_received, project)
|
2019-10-03 08:06:00 -04:00
|
|
|
return unless post_received.includes_branches? || post_received.includes_tags?
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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')
|