2013-02-20 05:53:15 -05:00
|
|
|
class SystemHooksService
|
|
|
|
def self.execute_hooks_for(model, event)
|
|
|
|
execute_hooks(build_event_data(model, event))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.execute_hooks(data)
|
|
|
|
SystemHook.all.each do |sh|
|
2013-02-20 06:33:03 -05:00
|
|
|
async_execute_hook sh, data
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-20 06:33:03 -05:00
|
|
|
def self.async_execute_hook(hook, data)
|
2013-02-21 05:04:06 -05:00
|
|
|
Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data)
|
2013-02-20 06:33:03 -05:00
|
|
|
end
|
|
|
|
|
2013-02-20 05:53:15 -05:00
|
|
|
def self.build_event_data(model, event)
|
|
|
|
data = {
|
|
|
|
event_name: build_event_name(model, event),
|
|
|
|
created_at: model.created_at
|
|
|
|
}
|
|
|
|
|
|
|
|
case model
|
|
|
|
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,
|
|
|
|
owner_email: owner.respond_to?(:email) ? owner.email : nil
|
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
|
|
|
})
|
|
|
|
when UsersProject
|
|
|
|
data.merge!({
|
|
|
|
project_name: model.project.name,
|
|
|
|
project_path: model.project.path,
|
|
|
|
project_id: model.project_id,
|
|
|
|
user_name: model.user.name,
|
|
|
|
user_email: model.user.email,
|
2013-08-20 08:59:45 -04:00
|
|
|
project_access: model.human_access
|
2013-02-20 05:53:15 -05:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build_event_name(model, event)
|
|
|
|
case model
|
|
|
|
when UsersProject
|
|
|
|
return "user_add_to_team" if event == :create
|
|
|
|
return "user_remove_from_team" if event == :destroy
|
|
|
|
else
|
|
|
|
"#{model.class.name.downcase}_#{event.to_s}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|