2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class SystemHooksService
|
|
|
|
def execute_hooks_for(model, event)
|
2018-03-17 18:26:18 +05:30
|
|
|
data = build_event_data(model, event)
|
|
|
|
|
|
|
|
model.run_after_commit_or_now do
|
|
|
|
SystemHooksService.new.execute_hooks(data)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def execute_hooks(data, hooks_scope = :all)
|
2018-03-17 18:26:18 +05:30
|
|
|
SystemHook.hooks_for(hooks_scope).find_each do |hook|
|
2016-06-02 11:05:42 +05:30
|
|
|
hook.async_execute(data, 'system_hooks')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
Gitlab::FileHook.execute_all_async(data)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
private
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
def build_event_data(model, event)
|
|
|
|
data = {
|
|
|
|
event_name: build_event_name(model, event),
|
2018-03-27 19:54:05 +05:30
|
|
|
created_at: model.created_at&.xmlschema,
|
|
|
|
updated_at: model.updated_at&.xmlschema
|
2014-09-02 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
case model
|
2015-04-26 12:48:37 +05:30
|
|
|
when Key
|
|
|
|
data.merge!(
|
|
|
|
key: model.key,
|
|
|
|
id: model.id
|
|
|
|
)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
if model.user
|
2017-08-17 22:00:37 +05:30
|
|
|
data[:username] = model.user.username
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
when Project
|
2015-11-26 14:37:03 +05:30
|
|
|
data.merge!(project_data(model))
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
if event == :rename || event == :transfer
|
2017-08-17 22:00:37 +05:30
|
|
|
data[:old_path_with_namespace] = model.old_path_with_namespace
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
when User
|
2018-03-17 18:26:18 +05:30
|
|
|
data.merge!(user_data(model))
|
|
|
|
|
|
|
|
case event
|
|
|
|
when :rename
|
2019-07-31 22:56:46 +05:30
|
|
|
data[:old_username] = model.username_before_last_save
|
2018-03-17 18:26:18 +05:30
|
|
|
when :failed_login
|
|
|
|
data[:state] = model.state
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
when ProjectMember
|
2015-11-26 14:37:03 +05:30
|
|
|
data.merge!(project_member_data(model))
|
2015-04-26 12:48:37 +05:30
|
|
|
when Group
|
2018-03-17 18:26:18 +05:30
|
|
|
data.merge!(group_data(model))
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if event == :rename
|
|
|
|
data.merge!(
|
2019-07-31 22:56:46 +05:30
|
|
|
old_path: model.path_before_last_save,
|
|
|
|
old_full_path: model.full_path_before_last_save
|
2018-03-17 18:26:18 +05:30
|
|
|
)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
when GroupMember
|
2015-11-26 14:37:03 +05:30
|
|
|
data.merge!(group_member_data(model))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
data
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def build_event_name(model, event)
|
|
|
|
case model
|
2015-04-26 12:48:37 +05:30
|
|
|
when ProjectMember
|
2014-09-02 18:07:02 +05:30
|
|
|
return "user_add_to_team" if event == :create
|
|
|
|
return "user_remove_from_team" if event == :destroy
|
2019-12-04 20:38:33 +05:30
|
|
|
return "user_update_for_team" if event == :update
|
2015-04-26 12:48:37 +05:30
|
|
|
when GroupMember
|
|
|
|
return 'user_add_to_group' if event == :create
|
|
|
|
return 'user_remove_from_group' if event == :destroy
|
2019-12-04 20:38:33 +05:30
|
|
|
return 'user_update_for_group' if event == :update
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2016-11-03 12:29:30 +05:30
|
|
|
"#{model.class.name.downcase}_#{event}"
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
|
|
|
def project_data(model)
|
|
|
|
owner = model.owner
|
|
|
|
|
|
|
|
{
|
|
|
|
name: model.name,
|
|
|
|
path: model.path,
|
2017-09-10 17:25:29 +05:30
|
|
|
path_with_namespace: model.full_path,
|
2015-11-26 14:37:03 +05:30
|
|
|
project_id: model.id,
|
|
|
|
owner_name: owner.name,
|
2016-06-02 11:05:42 +05:30
|
|
|
owner_email: owner.respond_to?(:email) ? owner.email : "",
|
2018-03-17 18:26:18 +05:30
|
|
|
project_visibility: model.visibility.downcase
|
2015-11-26 14:37:03 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_member_data(model)
|
2016-06-02 11:05:42 +05:30
|
|
|
project = model.project || Project.unscoped.find(model.source_id)
|
|
|
|
|
2015-11-26 14:37:03 +05:30
|
|
|
{
|
2016-06-02 11:05:42 +05:30
|
|
|
project_name: project.name,
|
|
|
|
project_path: project.path,
|
2017-09-10 17:25:29 +05:30
|
|
|
project_path_with_namespace: project.full_path,
|
2016-06-02 11:05:42 +05:30
|
|
|
project_id: project.id,
|
|
|
|
user_username: model.user.username,
|
|
|
|
user_name: model.user.name,
|
|
|
|
user_email: model.user.email,
|
|
|
|
user_id: model.user.id,
|
|
|
|
access_level: model.human_access,
|
2017-08-17 22:00:37 +05:30
|
|
|
project_visibility: Project.visibility_levels.key(project.visibility_level_value).downcase
|
2015-11-26 14:37:03 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def group_data(model)
|
|
|
|
owner = model.owner
|
|
|
|
|
|
|
|
{
|
|
|
|
name: model.name,
|
|
|
|
path: model.path,
|
|
|
|
full_path: model.full_path,
|
|
|
|
group_id: model.id,
|
|
|
|
owner_name: owner.try(:name),
|
|
|
|
owner_email: owner.try(:email)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-11-26 14:37:03 +05:30
|
|
|
def group_member_data(model)
|
|
|
|
{
|
|
|
|
group_name: model.group.name,
|
|
|
|
group_path: model.group.path,
|
|
|
|
group_id: model.group.id,
|
2016-01-14 18:37:52 +05:30
|
|
|
user_username: model.user.username,
|
2015-11-26 14:37:03 +05:30
|
|
|
user_name: model.user.name,
|
|
|
|
user_email: model.user.email,
|
|
|
|
user_id: model.user.id,
|
2017-09-10 17:25:29 +05:30
|
|
|
group_access: model.human_access
|
2015-11-26 14:37:03 +05:30
|
|
|
}
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def user_data(model)
|
|
|
|
{
|
|
|
|
name: model.name,
|
|
|
|
email: model.email,
|
|
|
|
user_id: model.id,
|
|
|
|
username: model.username
|
|
|
|
}
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
SystemHooksService.prepend_if_ee('EE::SystemHooksService')
|