2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-20 05:53:15 -05:00
|
|
|
class SystemHooksService
|
2014-01-15 07:26:05 -05:00
|
|
|
def execute_hooks_for(model, event)
|
2017-11-29 10:30:17 -05:00
|
|
|
data = build_event_data(model, event)
|
|
|
|
|
|
|
|
model.run_after_commit_or_now do
|
|
|
|
SystemHooksService.new.execute_hooks(data)
|
|
|
|
end
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
|
2016-04-15 07:08:22 -04:00
|
|
|
def execute_hooks(data, hooks_scope = :all)
|
2017-12-04 06:51:39 -05:00
|
|
|
SystemHook.hooks_for(hooks_scope).find_each do |hook|
|
2016-04-15 07:08:22 -04:00
|
|
|
hook.async_execute(data, 'system_hooks')
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
2018-02-08 10:51:02 -05:00
|
|
|
|
2018-02-19 12:55:54 -05:00
|
|
|
Gitlab::Plugin.execute_all_async(data)
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
|
2016-04-15 07:08:22 -04:00
|
|
|
private
|
2013-02-20 06:33:03 -05:00
|
|
|
|
2014-01-15 07:26:05 -05:00
|
|
|
def build_event_data(model, event)
|
2013-02-20 05:53:15 -05:00
|
|
|
data = {
|
|
|
|
event_name: build_event_name(model, event),
|
2018-03-05 16:37:37 -05:00
|
|
|
created_at: model.created_at&.xmlschema,
|
|
|
|
updated_at: model.updated_at&.xmlschema
|
2013-02-20 05:53:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case model
|
2014-08-18 10:46:46 -04:00
|
|
|
when Key
|
|
|
|
data.merge!(
|
|
|
|
key: model.key,
|
|
|
|
id: model.id
|
|
|
|
)
|
2017-07-20 05:34:09 -04:00
|
|
|
|
2014-08-18 10:46:46 -04:00
|
|
|
if model.user
|
2017-03-16 09:28:34 -04:00
|
|
|
data[:username] = model.user.username
|
2014-08-18 10:46:46 -04:00
|
|
|
end
|
2013-02-20 05:53:15 -05:00
|
|
|
when Project
|
2015-11-12 18:12:18 -05:00
|
|
|
data.merge!(project_data(model))
|
2015-05-05 11:13:11 -04:00
|
|
|
|
|
|
|
if event == :rename || event == :transfer
|
2017-02-21 18:50:22 -05:00
|
|
|
data[:old_path_with_namespace] = model.old_path_with_namespace
|
2015-05-05 11:13:11 -04:00
|
|
|
end
|
2013-02-20 05:53:15 -05:00
|
|
|
when User
|
2017-11-03 07:26:52 -04:00
|
|
|
data.merge!(user_data(model))
|
|
|
|
|
2018-01-15 00:10:48 -05:00
|
|
|
case event
|
|
|
|
when :rename
|
2017-11-03 07:26:52 -04:00
|
|
|
data[:old_username] = model.username_was
|
2018-01-15 00:10:48 -05:00
|
|
|
when :failed_login
|
|
|
|
data[:state] = model.state
|
2017-11-03 07:26:52 -04:00
|
|
|
end
|
2014-09-14 12:32:51 -04:00
|
|
|
when ProjectMember
|
2015-11-12 18:12:18 -05:00
|
|
|
data.merge!(project_member_data(model))
|
2014-08-21 06:53:32 -04:00
|
|
|
when Group
|
2017-11-03 07:26:52 -04:00
|
|
|
data.merge!(group_data(model))
|
2014-08-21 06:53:32 -04:00
|
|
|
|
2017-11-03 07:26:52 -04:00
|
|
|
if event == :rename
|
|
|
|
data.merge!(
|
|
|
|
old_path: model.path_was,
|
|
|
|
old_full_path: model.full_path_was
|
|
|
|
)
|
|
|
|
end
|
2014-08-21 06:53:32 -04:00
|
|
|
when GroupMember
|
2015-11-12 18:12:18 -05:00
|
|
|
data.merge!(group_member_data(model))
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
2017-07-20 05:34:09 -04:00
|
|
|
|
2017-03-16 09:28:34 -04:00
|
|
|
data
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
|
2014-01-15 07:26:05 -05:00
|
|
|
def build_event_name(model, event)
|
2013-02-20 05:53:15 -05:00
|
|
|
case model
|
2014-09-14 12:32:51 -04:00
|
|
|
when ProjectMember
|
2013-02-20 05:53:15 -05:00
|
|
|
return "user_add_to_team" if event == :create
|
|
|
|
return "user_remove_from_team" if event == :destroy
|
2014-08-21 06:53:32 -04:00
|
|
|
when GroupMember
|
|
|
|
return 'user_add_to_group' if event == :create
|
|
|
|
return 'user_remove_from_group' if event == :destroy
|
2013-02-20 05:53:15 -05:00
|
|
|
else
|
2016-10-03 10:09:57 -04:00
|
|
|
"#{model.class.name.downcase}_#{event}"
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
end
|
2015-11-12 18:12:18 -05:00
|
|
|
|
|
|
|
def project_data(model)
|
|
|
|
owner = model.owner
|
|
|
|
|
|
|
|
{
|
|
|
|
name: model.name,
|
|
|
|
path: model.path,
|
2017-07-20 05:34:09 -04:00
|
|
|
path_with_namespace: model.full_path,
|
2015-11-12 18:12:18 -05:00
|
|
|
project_id: model.id,
|
|
|
|
owner_name: owner.name,
|
2016-05-10 22:58:06 -04:00
|
|
|
owner_email: owner.respond_to?(:email) ? owner.email : "",
|
2017-10-27 10:09:11 -04:00
|
|
|
project_visibility: model.visibility.downcase
|
2015-11-12 18:12:18 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_member_data(model)
|
2016-03-29 10:57:24 -04:00
|
|
|
project = model.project || Project.unscoped.find(model.source_id)
|
|
|
|
|
2015-11-12 18:12:18 -05:00
|
|
|
{
|
2016-03-29 10:57:24 -04:00
|
|
|
project_name: project.name,
|
|
|
|
project_path: project.path,
|
2017-07-20 05:34:09 -04:00
|
|
|
project_path_with_namespace: project.full_path,
|
2016-03-29 10:57:24 -04:00
|
|
|
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-03-01 15:23:00 -05:00
|
|
|
project_visibility: Project.visibility_levels.key(project.visibility_level_value).downcase
|
2015-11-12 18:12:18 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-11-03 07:26:52 -04:00
|
|
|
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-12 18:12:18 -05:00
|
|
|
def group_member_data(model)
|
|
|
|
{
|
|
|
|
group_name: model.group.name,
|
|
|
|
group_path: model.group.path,
|
|
|
|
group_id: model.group.id,
|
2016-01-07 12:54:54 -05:00
|
|
|
user_username: model.user.username,
|
2015-11-12 18:12:18 -05:00
|
|
|
user_name: model.user.name,
|
|
|
|
user_email: model.user.email,
|
|
|
|
user_id: model.user.id,
|
2017-05-03 07:22:03 -04:00
|
|
|
group_access: model.human_access
|
2015-11-12 18:12:18 -05:00
|
|
|
}
|
|
|
|
end
|
2017-11-03 07:26:52 -04:00
|
|
|
|
|
|
|
def user_data(model)
|
|
|
|
{
|
|
|
|
name: model.name,
|
|
|
|
email: model.email,
|
|
|
|
user_id: model.id,
|
|
|
|
username: model.username
|
|
|
|
}
|
|
|
|
end
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|