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
|
# frozen_string_literal: true
module Gitlab
module HookData
class ProjectBuilder < BaseBuilder
alias_method :project, :object
# Sample data
# {
# event_name: "project_rename",
# created_at: "2021-04-19T07:05:36Z",
# updated_at: "2021-04-19T07:05:36Z",
# name: "my_project",
# path: "my_project",
# path_with_namespace: "namespace2/my_project",
# project_id: 1,
# owner_name: "John",
# owner_email: "user1@example.org",
# owners: [name: "John", email: "user1@example.org"],
# project_visibility: "internal",
# old_path_with_namespace: "old-path-with-namespace"
# }
def build(event)
[
event_data(event),
timestamps_data,
project_data,
event_specific_project_data(event)
].reduce(:merge)
end
private
def project_data
# When this is removed, also remove the `deprecated_owner` method
# See https://gitlab.com/gitlab-org/gitlab/-/issues/350603
owner = project.deprecated_owner
{
name: project.name,
path: project.path,
path_with_namespace: project.full_path,
project_id: project.id,
owner_name: owner.try(:name),
owner_email: user_email(owner),
owners: owners_data,
project_visibility: project.visibility.downcase
}
end
def owners_data
# Extracted code from ProjectTeam#owners, but works without creating cross joins queries
# Can be consolidate again once https://gitlab.com/gitlab-org/gitlab/-/issues/432606 is addressed
if project.group
project.group.all_owner_members.select(:id, :user_id)
.preload_user.find_each.map { |member| owner_data(member.user) }
else
data = []
project.project_authorizations.owners.preload_user.each_batch(column: :user_id) do |relation|
data.concat(relation.map { |member| owner_data(member.user) })
end
data |= Array.wrap(owner_data(project.owner)) if project.owner
data
end
end
def owner_data(user)
{ name: user.name, email: user_email(user) }
end
def user_email(user)
user.respond_to?(:webhook_email) ? user.webhook_email : ""
end
def event_specific_project_data(event)
return {} unless event == :rename || event == :transfer
{
old_path_with_namespace: project.old_path_with_namespace
}
end
end
end
end
|