2019-03-28 14:59:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Git
|
|
|
|
class BaseHooksService < ::BaseService
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-10-03 15:07:07 +00:00
|
|
|
include ChangeParams
|
2019-03-28 14:59:24 +00:00
|
|
|
|
|
|
|
# The N most recent commits to process in a single push payload.
|
|
|
|
PROCESS_COMMIT_LIMIT = 100
|
|
|
|
|
|
|
|
def execute
|
|
|
|
create_events
|
|
|
|
create_pipelines
|
|
|
|
execute_project_hooks
|
|
|
|
|
|
|
|
# Not a hook, but it needs access to the list of changed commits
|
|
|
|
enqueue_invalidate_cache
|
|
|
|
|
2019-08-12 22:31:58 +00:00
|
|
|
success
|
2019-03-28 14:59:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def hook_name
|
|
|
|
raise NotImplementedError, "Please implement #{self.class}##{__method__}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def commits
|
|
|
|
raise NotImplementedError, "Please implement #{self.class}##{__method__}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def limited_commits
|
2019-08-12 22:31:58 +00:00
|
|
|
@limited_commits ||= commits.last(PROCESS_COMMIT_LIMIT)
|
2019-03-28 14:59:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def commits_count
|
|
|
|
commits.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_message
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalidated_file_types
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2019-08-12 22:31:58 +00:00
|
|
|
# Push events in the activity feed only show information for the
|
|
|
|
# last commit.
|
2019-03-28 14:59:24 +00:00
|
|
|
def create_events
|
2019-10-17 12:07:33 +00:00
|
|
|
return unless params.fetch(:create_push_event, true)
|
|
|
|
|
2019-08-12 22:31:58 +00:00
|
|
|
EventCreateService.new.push(project, current_user, event_push_data)
|
2019-03-28 14:59:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_pipelines
|
2019-04-10 09:04:51 +00:00
|
|
|
return unless params.fetch(:create_pipelines, true)
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
Ci::CreatePipelineService
|
2019-08-17 07:42:23 +00:00
|
|
|
.new(project, current_user, pipeline_params)
|
2019-09-04 06:34:52 +00:00
|
|
|
.execute!(:push, pipeline_options)
|
|
|
|
rescue Ci::CreatePipelineService::CreateError => ex
|
|
|
|
log_pipeline_errors(ex)
|
2019-03-28 14:59:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute_project_hooks
|
2019-10-16 18:08:01 +00:00
|
|
|
return unless params.fetch(:execute_project_hooks, true)
|
|
|
|
|
2019-08-12 22:31:58 +00:00
|
|
|
# Creating push_data invokes one CommitDelta RPC per commit. Only
|
|
|
|
# build this data if we actually need it.
|
|
|
|
project.execute_hooks(push_data, hook_name) if project.has_active_hooks?(hook_name)
|
|
|
|
project.execute_services(push_data, hook_name) if project.has_active_services?(hook_name)
|
2019-03-28 14:59:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_invalidate_cache
|
2019-08-16 19:53:56 +00:00
|
|
|
file_types = invalidated_file_types
|
|
|
|
|
|
|
|
return unless file_types.present?
|
|
|
|
|
|
|
|
ProjectCacheWorker.perform_async(project.id, file_types, [], false)
|
2019-03-28 14:59:24 +00:00
|
|
|
end
|
|
|
|
|
2019-08-17 07:42:23 +00:00
|
|
|
def pipeline_params
|
2020-02-06 21:08:48 +00:00
|
|
|
strong_memoize(:pipeline_params) do
|
|
|
|
{
|
|
|
|
before: oldrev,
|
|
|
|
after: newrev,
|
|
|
|
ref: ref,
|
|
|
|
variables_attributes: generate_vars_from_push_options || [],
|
|
|
|
push_options: params[:push_options] || {},
|
|
|
|
checkout_sha: Gitlab::DataBuilder::Push.checkout_sha(
|
|
|
|
project.repository, newrev, ref)
|
|
|
|
}
|
|
|
|
end
|
2019-08-12 22:31:58 +00:00
|
|
|
end
|
|
|
|
|
2019-11-21 15:06:17 +00:00
|
|
|
def ci_variables_from_push_options
|
|
|
|
strong_memoize(:ci_variables_from_push_options) do
|
|
|
|
params[:push_options]&.deep_symbolize_keys&.dig(:ci, :variable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_vars_from_push_options
|
|
|
|
return [] unless ci_variables_from_push_options
|
|
|
|
|
|
|
|
ci_variables_from_push_options.map do |var_definition, _count|
|
|
|
|
key, value = var_definition.to_s.split("=", 2)
|
|
|
|
|
|
|
|
# Accept only valid format. We ignore the following formats
|
|
|
|
# 1. "=123". In this case, `key` will be an empty string
|
|
|
|
# 2. "FOO". In this case, `value` will be nil.
|
|
|
|
# However, the format "FOO=" will result in key beign `FOO` and value
|
|
|
|
# being an empty string. This is acceptable.
|
|
|
|
next if key.blank? || value.nil?
|
|
|
|
|
|
|
|
{ "key" => key, "variable_type" => "env_var", "secret_value" => value }
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2019-08-12 22:31:58 +00:00
|
|
|
def push_data_params(commits:, with_changed_files: true)
|
2019-08-17 07:42:23 +00:00
|
|
|
{
|
2019-10-03 15:07:07 +00:00
|
|
|
oldrev: oldrev,
|
|
|
|
newrev: newrev,
|
|
|
|
ref: ref,
|
2019-08-12 22:31:58 +00:00
|
|
|
project: project,
|
|
|
|
user: current_user,
|
|
|
|
commits: commits,
|
2018-12-31 03:31:35 +00:00
|
|
|
message: event_message,
|
2019-03-28 14:59:24 +00:00
|
|
|
commits_count: commits_count,
|
2019-08-12 22:31:58 +00:00
|
|
|
with_changed_files: with_changed_files
|
2019-08-17 07:42:23 +00:00
|
|
|
}
|
2019-08-12 22:31:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def event_push_data
|
|
|
|
# We only need the last commit for the event push, and we don't
|
|
|
|
# need the full deltas either.
|
|
|
|
@event_push_data ||= Gitlab::DataBuilder::Push.build(
|
2020-12-09 21:09:43 +00:00
|
|
|
**push_data_params(commits: commits.last, with_changed_files: false)
|
|
|
|
)
|
2019-08-12 22:31:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_data
|
2020-12-09 21:09:43 +00:00
|
|
|
@push_data ||= Gitlab::DataBuilder::Push.build(**push_data_params(commits: limited_commits))
|
2019-03-28 14:59:24 +00:00
|
|
|
|
|
|
|
# Dependent code may modify the push data, so return a duplicate each time
|
|
|
|
@push_data.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
# to be overridden in EE
|
|
|
|
def pipeline_options
|
|
|
|
{}
|
|
|
|
end
|
2019-05-17 21:43:58 +00:00
|
|
|
|
2019-09-04 06:34:52 +00:00
|
|
|
def log_pipeline_errors(exception)
|
|
|
|
data = {
|
|
|
|
class: self.class.name,
|
|
|
|
correlation_id: Labkit::Correlation::CorrelationId.current_id.to_s,
|
|
|
|
project_id: project.id,
|
|
|
|
project_path: project.full_path,
|
|
|
|
message: "Error creating pipeline",
|
|
|
|
errors: exception.to_s,
|
2020-02-06 21:08:48 +00:00
|
|
|
pipeline_params: sanitized_pipeline_params
|
2019-09-04 06:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn(data)
|
|
|
|
end
|
|
|
|
|
2020-02-06 21:08:48 +00:00
|
|
|
def sanitized_pipeline_params
|
|
|
|
pipeline_params.except(:push_options)
|
|
|
|
end
|
|
|
|
|
2019-09-04 06:34:52 +00:00
|
|
|
def logger
|
2019-12-22 09:07:51 +00:00
|
|
|
if Gitlab::Runtime.sidekiq?
|
2019-09-04 06:34:52 +00:00
|
|
|
Sidekiq.logger
|
|
|
|
else
|
|
|
|
# This service runs in Sidekiq, so this shouldn't ever be
|
|
|
|
# called, but this is included just in case.
|
|
|
|
Gitlab::ProjectServiceLogger
|
|
|
|
end
|
|
|
|
end
|
2019-03-28 14:59:24 +00:00
|
|
|
end
|
|
|
|
end
|