2014-09-02 18:07:02 +05:30
|
|
|
class SystemHooksService
|
|
|
|
def execute_hooks_for(model, event)
|
|
|
|
execute_hooks(build_event_data(model, event))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_hooks(data)
|
|
|
|
SystemHook.all.each do |sh|
|
|
|
|
async_execute_hook sh, data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def async_execute_hook(hook, data)
|
|
|
|
Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_event_data(model, event)
|
|
|
|
data = {
|
|
|
|
event_name: build_event_name(model, event),
|
2015-04-26 12:48:37 +05:30
|
|
|
created_at: model.created_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
|
|
|
|
)
|
|
|
|
if model.user
|
|
|
|
data.merge!(
|
|
|
|
username: model.user.username
|
|
|
|
)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
when Project
|
|
|
|
owner = model.owner
|
|
|
|
|
|
|
|
data.merge!({
|
|
|
|
name: model.name,
|
|
|
|
path: model.path,
|
|
|
|
path_with_namespace: model.path_with_namespace,
|
|
|
|
project_id: model.id,
|
|
|
|
owner_name: owner.name,
|
2015-04-26 12:48:37 +05:30
|
|
|
owner_email: owner.respond_to?(:email) ? owner.email : "",
|
2014-09-02 18:07:02 +05:30
|
|
|
project_visibility: Project.visibility_levels.key(model.visibility_level_field).downcase
|
|
|
|
})
|
|
|
|
when User
|
|
|
|
data.merge!({
|
|
|
|
name: model.name,
|
|
|
|
email: model.email,
|
|
|
|
user_id: model.id
|
|
|
|
})
|
2015-04-26 12:48:37 +05:30
|
|
|
when ProjectMember
|
2014-09-02 18:07:02 +05:30
|
|
|
data.merge!({
|
|
|
|
project_name: model.project.name,
|
|
|
|
project_path: model.project.path,
|
2015-04-26 12:48:37 +05:30
|
|
|
project_id: model.project.id,
|
2014-09-02 18:07:02 +05:30
|
|
|
user_name: model.user.name,
|
|
|
|
user_email: model.user.email,
|
2015-04-26 12:48:37 +05:30
|
|
|
access_level: model.human_access,
|
2014-09-02 18:07:02 +05:30
|
|
|
project_visibility: Project.visibility_levels.key(model.project.visibility_level_field).downcase
|
|
|
|
})
|
2015-04-26 12:48:37 +05:30
|
|
|
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,
|
|
|
|
)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
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
|
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
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
|
|
|
"#{model.class.name.downcase}_#{event.to_s}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|