summaryrefslogtreecommitdiff
path: root/app/services/system_hooks_service.rb
blob: 9a5fe4af9dde7e93b9c298018867b9a52309c15f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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, 'system_hooks')
    end
  end

  def async_execute_hook(hook, data, hook_name)
    Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data, hook_name)
  end

  def build_event_data(model, event)
    data = {
      event_name: build_event_name(model, event),
      created_at: model.created_at.xmlschema
    }

    case model
    when Key
      data.merge!(
        key: model.key,
        id: model.id
      )
      if model.user
        data.merge!(
          username: model.user.username
        )
      end
    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,
        owner_email: owner.respond_to?(:email) ?  owner.email : "",
        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
      })
    when ProjectMember
      data.merge!({
        project_name: model.project.name,
        project_path: model.project.path,
        project_path_with_namespace: model.project.path_with_namespace,
        project_id: model.project.id,
        user_name: model.user.name,
        user_email: model.user.email,
        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

  def build_event_name(model, event)
    case model
    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
  end
end