diff options
author | Boyan Tabakov <boyan.tabakov@futurice.com> | 2014-08-21 14:53:32 +0400 |
---|---|---|
committer | Boyan Tabakov <boyan.tabakov@futurice.com> | 2015-01-27 10:25:28 +0300 |
commit | 93bc2d5202e5802bd31419d05232b62355516a53 (patch) | |
tree | a1b2f38faa6470b8df2f0047115a22fbe8361f57 /app | |
parent | a4dad7085850aa62134ed23b90f3a045c3569663 (diff) |
Added support for firing system hooks on group create/destroy and adding/removing users to group.
Added tests and updated docs. Also adding 'user_id' field in the hooks for adding/removing user from team.
Diffstat (limited to 'app')
-rw-r--r-- | app/models/group.rb | 15 | ||||
-rw-r--r-- | app/models/members/group_member.rb | 14 | ||||
-rw-r--r-- | app/services/system_hooks_service.rb | 23 |
3 files changed, 50 insertions, 2 deletions
diff --git a/app/models/group.rb b/app/models/group.rb index 733afa2fc07..e098dfb3cdf 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -25,6 +25,9 @@ class Group < Namespace mount_uploader :avatar, AttachmentUploader + after_create :post_create_hook + after_destroy :post_destroy_hook + def human_name name end @@ -74,6 +77,18 @@ class Group < Namespace projects.public_only.any? end + def post_create_hook + system_hook_service.execute_hooks_for(self, :create) + end + + def post_destroy_hook + system_hook_service.execute_hooks_for(self, :destroy) + end + + def system_hook_service + SystemHooksService.new + end + class << self def search(query) where("LOWER(namespaces.name) LIKE :query", query: "%#{query.downcase}%") diff --git a/app/models/members/group_member.rb b/app/models/members/group_member.rb index b7f296b13fb..28d0b4483b4 100644 --- a/app/models/members/group_member.rb +++ b/app/models/members/group_member.rb @@ -27,8 +27,9 @@ class GroupMember < Member scope :with_group, ->(group) { where(source_id: group.id) } scope :with_user, ->(user) { where(user_id: user.id) } - after_create :notify_create + after_create :post_create_hook after_update :notify_update + after_destroy :post_destroy_hook def self.access_level_roles Gitlab::Access.options_with_owner @@ -42,8 +43,9 @@ class GroupMember < Member access_level end - def notify_create + def post_create_hook notification_service.new_group_member(self) + system_hook_service.execute_hooks_for(self, :create) end def notify_update @@ -52,6 +54,14 @@ class GroupMember < Member end end + def post_destroy_hook + system_hook_service.execute_hooks_for(self, :destroy) + end + + def system_hook_service + SystemHooksService.new + end + def notification_service NotificationService.new end diff --git a/app/services/system_hooks_service.rb b/app/services/system_hooks_service.rb index 44e494525b3..46f6e91e808 100644 --- a/app/services/system_hooks_service.rb +++ b/app/services/system_hooks_service.rb @@ -60,6 +60,26 @@ class SystemHooksService access_level: model.human_access, project_visibility: Project.visibility_levels.key(model.project.visibility_level_field).downcase }) + 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, + ) end end @@ -68,6 +88,9 @@ class SystemHooksService when ProjectMember return "user_add_to_team" if event == :create return "user_remove_from_team" if event == :destroy + when GroupMember + return 'user_add_to_group' if event == :create + return 'user_remove_from_group' if event == :destroy else "#{model.class.name.downcase}_#{event.to_s}" end |