2013-02-20 05:53:15 -05:00
|
|
|
class SystemHooksService
|
2014-01-15 07:26:05 -05:00
|
|
|
def execute_hooks_for(model, event)
|
2013-02-20 05:53:15 -05:00
|
|
|
execute_hooks(build_event_data(model, event))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-01-15 07:26:05 -05:00
|
|
|
def execute_hooks(data)
|
2013-02-20 05:53:15 -05:00
|
|
|
SystemHook.all.each do |sh|
|
2015-01-23 19:10:43 -05:00
|
|
|
async_execute_hook(sh, data, 'system_hooks')
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-23 19:10:43 -05:00
|
|
|
def async_execute_hook(hook, data, hook_name)
|
|
|
|
Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data, hook_name)
|
2013-02-20 06:33:03 -05:00
|
|
|
end
|
|
|
|
|
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),
|
2014-08-13 13:10:18 -04:00
|
|
|
created_at: model.created_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
|
|
|
|
)
|
|
|
|
if model.user
|
|
|
|
data.merge!(
|
|
|
|
username: model.user.username
|
|
|
|
)
|
|
|
|
end
|
2013-02-20 05:53:15 -05:00
|
|
|
when Project
|
2013-09-26 07:49:22 -04:00
|
|
|
owner = model.owner
|
|
|
|
|
2013-02-20 05:53:15 -05:00
|
|
|
data.merge!({
|
|
|
|
name: model.name,
|
2013-05-22 07:06:31 -04:00
|
|
|
path: model.path,
|
|
|
|
path_with_namespace: model.path_with_namespace,
|
2013-02-20 05:53:15 -05:00
|
|
|
project_id: model.id,
|
2013-09-26 07:49:22 -04:00
|
|
|
owner_name: owner.name,
|
2014-09-03 16:28:04 -04:00
|
|
|
owner_email: owner.respond_to?(:email) ? owner.email : "",
|
2014-05-12 05:23:24 -04:00
|
|
|
project_visibility: Project.visibility_levels.key(model.visibility_level_field).downcase
|
2013-02-20 05:53:15 -05:00
|
|
|
})
|
|
|
|
when User
|
|
|
|
data.merge!({
|
|
|
|
name: model.name,
|
2013-10-08 03:54:40 -04:00
|
|
|
email: model.email,
|
|
|
|
user_id: model.id
|
2013-02-20 05:53:15 -05:00
|
|
|
})
|
2014-09-14 12:32:51 -04:00
|
|
|
when ProjectMember
|
2013-02-20 05:53:15 -05:00
|
|
|
data.merge!({
|
|
|
|
project_name: model.project.name,
|
|
|
|
project_path: model.project.path,
|
2015-09-12 16:53:18 -04:00
|
|
|
project_path_with_namespace: model.project.path_with_namespace,
|
2014-09-14 13:38:57 -04:00
|
|
|
project_id: model.project.id,
|
2013-02-20 05:53:15 -05:00
|
|
|
user_name: model.user.name,
|
|
|
|
user_email: model.user.email,
|
2014-09-14 12:32:51 -04:00
|
|
|
access_level: model.human_access,
|
2014-05-12 05:23:24 -04:00
|
|
|
project_visibility: Project.visibility_levels.key(model.project.visibility_level_field).downcase
|
2013-02-20 05:53:15 -05:00
|
|
|
})
|
2014-08-21 06:53:32 -04:00
|
|
|
when Group
|
|
|
|
owner = model.owner
|
|
|
|
|
|
|
|
data.merge!(
|
|
|
|
name: model.name,
|
|
|
|
path: model.path,
|
|
|
|
group_id: model.id,
|
|
|
|
owner_name: owner.respond_to?(:name) ? owner.name : nil,
|
|
|
|
owner_email: owner.respond_to?(:email) ? owner.email : nil,
|
|
|
|
)
|
|
|
|
when GroupMember
|
|
|
|
data.merge!(
|
|
|
|
group_name: model.group.name,
|
|
|
|
group_path: model.group.path,
|
|
|
|
group_id: model.group.id,
|
|
|
|
user_name: model.user.name,
|
|
|
|
user_email: model.user.email,
|
|
|
|
user_id: model.user.id,
|
|
|
|
group_access: model.human_access,
|
|
|
|
)
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
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
|
|
|
|
"#{model.class.name.downcase}_#{event.to_s}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|