2018-06-27 07:23:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-19 18:09:10 +00:00
|
|
|
class PostReceive # rubocop:disable Scalability/IdempotentWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2021-02-06 00:09:54 +00:00
|
|
|
include Gitlab::Experiment::Dsl
|
2013-01-09 05:14:05 +00:00
|
|
|
|
2019-10-18 11:11:44 +00:00
|
|
|
feature_category :source_code_management
|
2020-03-02 18:07:42 +00:00
|
|
|
urgency :high
|
2019-10-30 15:14:17 +00:00
|
|
|
worker_resource_boundary :cpu
|
2020-01-24 18:09:00 +00:00
|
|
|
weight 5
|
2020-06-12 12:08:56 +00:00
|
|
|
loggable_arguments 0, 1, 2, 3
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2019-04-05 04:19:30 +00:00
|
|
|
def perform(gl_repository, identifier, changes, push_options = {})
|
2020-03-05 00:07:49 +00:00
|
|
|
container, project, repo_type = Gitlab::GlRepository.parse(gl_repository)
|
2017-05-02 21:15:12 +00:00
|
|
|
|
2020-09-09 21:08:33 +00:00
|
|
|
if container.nil? || (container.is_a?(ProjectSnippet) && project.nil?)
|
|
|
|
log("Triggered hook for non-existing gl_repository \"#{gl_repository}\"")
|
2017-05-02 21:15:12 +00:00
|
|
|
return false
|
|
|
|
end
|
2013-01-29 09:32:05 +00:00
|
|
|
|
2016-07-28 13:12:49 +00:00
|
|
|
changes = Base64.decode64(changes) unless changes.include?(' ')
|
|
|
|
# Use Sidekiq.logger so arguments can be correlated with execution
|
|
|
|
# time and thread ID's.
|
2020-11-13 18:09:11 +00:00
|
|
|
Sidekiq.logger.info "changes: #{changes.inspect}" if SidekiqLogArguments.enabled?
|
2020-03-05 00:07:49 +00:00
|
|
|
post_received = Gitlab::GitPostReceive.new(container, identifier, changes, push_options)
|
2011-12-14 16:38:52 +00:00
|
|
|
|
2019-03-18 16:51:11 +00:00
|
|
|
if repo_type.wiki?
|
2020-10-05 15:08:56 +00:00
|
|
|
process_wiki_changes(post_received, container)
|
2019-04-22 16:07:19 +00:00
|
|
|
elsif repo_type.project?
|
2020-03-05 00:07:49 +00:00
|
|
|
process_project_changes(post_received, container)
|
|
|
|
elsif repo_type.snippet?
|
|
|
|
process_snippet_changes(post_received, container)
|
2019-04-22 16:07:19 +00:00
|
|
|
else
|
|
|
|
# Other repos don't have hooks for now
|
2016-03-17 03:24:12 +00:00
|
|
|
end
|
|
|
|
end
|
2013-01-28 15:22:45 +00:00
|
|
|
|
2017-06-05 23:37:52 +00:00
|
|
|
private
|
|
|
|
|
2019-06-12 04:35:19 +00: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-05 00:07:49 +00:00
|
|
|
def process_project_changes(post_received, project)
|
2019-06-12 04:35:19 +00:00
|
|
|
user = identify_user(post_received)
|
2019-10-03 15:07:07 +00:00
|
|
|
|
2019-06-12 04:35:19 +00:00
|
|
|
return false unless user
|
2014-09-02 07:58:54 +00:00
|
|
|
|
2019-10-03 15:07:07 +00:00
|
|
|
push_options = post_received.push_options
|
|
|
|
changes = post_received.changes
|
|
|
|
|
2019-08-16 19:53:56 +00:00
|
|
|
# We only need to expire certain caches once per push
|
2020-03-05 00:07:49 +00:00
|
|
|
expire_caches(post_received, project.repository)
|
|
|
|
enqueue_project_cache_update(post_received, project)
|
2019-08-09 10:09:06 +00:00
|
|
|
|
2019-10-11 18:06:15 +00:00
|
|
|
process_ref_changes(project, user, push_options: push_options, changes: changes)
|
2020-03-05 00:07:49 +00:00
|
|
|
update_remote_mirrors(post_received, project)
|
2019-10-03 15:07:07 +00:00
|
|
|
after_project_changes_hooks(project, user, changes.refs, changes.repository_data)
|
2011-12-13 00:03:26 +00:00
|
|
|
end
|
2013-04-29 06:43:18 +00:00
|
|
|
|
2020-09-09 21:08:33 +00:00
|
|
|
def process_wiki_changes(post_received, wiki)
|
2020-03-05 00:07:49 +00:00
|
|
|
user = identify_user(post_received)
|
|
|
|
return false unless user
|
|
|
|
|
|
|
|
# We only need to expire certain caches once per push
|
2020-09-09 21:08:33 +00:00
|
|
|
expire_caches(post_received, wiki.repository)
|
|
|
|
wiki.repository.expire_statistics_caches
|
2020-03-05 00:07:49 +00:00
|
|
|
|
2020-09-09 21:08:33 +00:00
|
|
|
::Git::WikiPushService.new(wiki, user, changes: post_received.changes).execute
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_snippet_changes(post_received, snippet)
|
|
|
|
user = identify_user(post_received)
|
|
|
|
|
|
|
|
return false unless user
|
|
|
|
|
2020-09-11 21:08:44 +00:00
|
|
|
replicate_snippet_changes(snippet)
|
|
|
|
|
2020-03-05 00:07:49 +00:00
|
|
|
expire_caches(post_received, snippet.repository)
|
2020-06-30 15:08:48 +00:00
|
|
|
Snippets::UpdateStatisticsService.new(snippet).execute
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
|
2020-09-11 21:08:44 +00:00
|
|
|
def replicate_snippet_changes(snippet)
|
|
|
|
# Used by Gitlab Geo
|
|
|
|
end
|
|
|
|
|
2019-10-03 09:07:16 +00: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 19:53:56 +00:00
|
|
|
end
|
|
|
|
|
2019-10-03 09:07:16 +00:00
|
|
|
# Schedule an update for the repository size and commit count if necessary.
|
2020-03-05 00:07:49 +00:00
|
|
|
def enqueue_project_cache_update(post_received, project)
|
2019-08-16 19:53:56 +00:00
|
|
|
stats_to_invalidate = [:repository_size]
|
|
|
|
stats_to_invalidate << :commit_count if post_received.includes_default_branch?
|
|
|
|
|
|
|
|
ProjectCacheWorker.perform_async(
|
2020-03-05 00:07:49 +00:00
|
|
|
project.id,
|
2019-08-16 19:53:56 +00:00
|
|
|
[],
|
|
|
|
stats_to_invalidate,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-11 18:06:15 +00:00
|
|
|
def process_ref_changes(project, user, params = {})
|
|
|
|
return unless params[:changes].any?
|
|
|
|
|
|
|
|
Git::ProcessRefChangesService.new(project, user, params).execute
|
2019-10-03 15:07:07 +00:00
|
|
|
end
|
|
|
|
|
2020-03-05 00:07:49 +00:00
|
|
|
def update_remote_mirrors(post_received, project)
|
2019-10-03 12:06:00 +00: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 15:07:07 +00:00
|
|
|
def after_project_changes_hooks(project, user, refs, changes)
|
2021-02-06 00:09:54 +00:00
|
|
|
experiment(:new_project_readme, actor: user).track_initial_writes(project)
|
2021-04-22 12:09:49 +00:00
|
|
|
experiment(:empty_repo_upload, project: project).track_initial_write
|
2019-10-03 15:07:07 +00:00
|
|
|
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 22:33:16 +00:00
|
|
|
Gitlab::UsageDataCounters::SourceCodeCounter.count(:pushes)
|
2017-06-05 23:37:52 +00:00
|
|
|
end
|
|
|
|
|
2013-04-29 06:43:18 +00:00
|
|
|
def log(message)
|
|
|
|
Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
|
|
|
|
end
|
2011-12-13 00:03:26 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
PostReceive.prepend_if_ee('EE::PostReceive')
|