Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
d477ba3f
Commit
d477ba3f
authored
Apr 19, 2021
by
Manoj M J
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Project event data for webhooks
Refactor Project event data for webhooks
parent
44fb7c26
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
143 additions
and
21 deletions
+143
-21
app/services/system_hooks_service.rb
app/services/system_hooks_service.rb
+3
-21
lib/gitlab/hook_data/project_builder.rb
lib/gitlab/hook_data/project_builder.rb
+57
-0
spec/lib/gitlab/hook_data/project_builder_spec.rb
spec/lib/gitlab/hook_data/project_builder_spec.rb
+83
-0
No files found.
app/services/system_hooks_service.rb
View file @
d477ba3f
# frozen_string_literal: true
class
SystemHooksService
BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES
=
[
GroupMember
,
Group
,
ProjectMember
,
User
].
freeze
BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES
=
[
GroupMember
,
Group
,
ProjectMember
,
User
,
Project
].
freeze
def
execute_hooks_for
(
model
,
event
)
data
=
build_event_data
(
model
,
event
)
...
...
@@ -41,12 +41,6 @@ class SystemHooksService
if
model
.
user
data
[
:username
]
=
model
.
user
.
username
end
when
Project
data
.
merge!
(
project_data
(
model
))
if
event
==
:rename
||
event
==
:transfer
data
[
:old_path_with_namespace
]
=
model
.
old_path_with_namespace
end
end
data
...
...
@@ -56,20 +50,6 @@ class SystemHooksService
"
#{
model
.
class
.
name
.
downcase
}
_
#{
event
}
"
end
def
project_data
(
model
)
owner
=
model
.
owner
{
name:
model
.
name
,
path:
model
.
path
,
path_with_namespace:
model
.
full_path
,
project_id:
model
.
id
,
owner_name:
owner
.
name
,
owner_email:
owner
.
respond_to?
(
:email
)
?
owner
.
email
:
""
,
project_visibility:
model
.
visibility
.
downcase
}
end
def
builder_driven_event_data_available?
(
model
)
model
.
class
.
in?
(
BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES
)
end
...
...
@@ -84,6 +64,8 @@ class SystemHooksService
Gitlab
::
HookData
::
ProjectMemberBuilder
when
User
Gitlab
::
HookData
::
UserBuilder
when
Project
Gitlab
::
HookData
::
ProjectBuilder
end
builder_class
.
new
(
model
).
build
(
event
)
...
...
lib/gitlab/hook_data/project_builder.rb
0 → 100644
View file @
d477ba3f
# 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",
# 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
owner
=
project
.
owner
{
name:
project
.
name
,
path:
project
.
path
,
path_with_namespace:
project
.
full_path
,
project_id:
project
.
id
,
owner_name:
owner
.
name
,
owner_email:
owner
.
respond_to?
(
:email
)
?
owner
.
email
:
""
,
project_visibility:
project
.
visibility
.
downcase
}
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
spec/lib/gitlab/hook_data/project_builder_spec.rb
0 → 100644
View file @
d477ba3f
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Gitlab
::
HookData
::
ProjectBuilder
do
let_it_be
(
:user
)
{
create
(
:user
,
name:
'John'
,
email:
'john@example.com'
)
}
let_it_be
(
:namespace
)
{
create
(
:namespace
,
owner:
user
)
}
let_it_be
(
:project
)
{
create
(
:project
,
:internal
,
name:
'my_project'
,
namespace:
namespace
)
}
describe
'#build'
do
let
(
:data
)
{
described_class
.
new
(
project
).
build
(
event
)
}
let
(
:event_name
)
{
data
[
:event_name
]
}
let
(
:attributes
)
do
[
:event_name
,
:created_at
,
:updated_at
,
:name
,
:path
,
:path_with_namespace
,
:project_id
,
:owner_name
,
:owner_email
,
:project_visibility
]
end
context
'data'
do
shared_examples_for
'includes the required attributes'
do
it
'includes the required attributes'
do
expect
(
data
).
to
include
(
*
attributes
)
expect
(
data
[
:created_at
]).
to
eq
(
project
.
created_at
.
xmlschema
)
expect
(
data
[
:updated_at
]).
to
eq
(
project
.
updated_at
.
xmlschema
)
expect
(
data
[
:name
]).
to
eq
(
'my_project'
)
expect
(
data
[
:path
]).
to
eq
(
project
.
path
)
expect
(
data
[
:path_with_namespace
]).
to
eq
(
project
.
full_path
)
expect
(
data
[
:project_id
]).
to
eq
(
project
.
id
)
expect
(
data
[
:owner_name
]).
to
eq
(
'John'
)
expect
(
data
[
:owner_email
]).
to
eq
(
'john@example.com'
)
expect
(
data
[
:project_visibility
]).
to
eq
(
'internal'
)
end
end
shared_examples_for
'does not include `old_path_with_namespace` attribute'
do
it
'does not include `old_path_with_namespace` attribute'
do
expect
(
data
).
not_to
include
(
:old_path_with_namespace
)
end
end
shared_examples_for
'includes `old_path_with_namespace` attribute'
do
it
'includes `old_path_with_namespace` attribute'
do
allow
(
project
).
to
receive
(
:old_path_with_namespace
).
and_return
(
'old-path-with-namespace'
)
expect
(
data
[
:old_path_with_namespace
]).
to
eq
(
'old-path-with-namespace'
)
end
end
context
'on create'
do
let
(
:event
)
{
:create
}
it
{
expect
(
event_name
).
to
eq
(
'project_create'
)
}
it_behaves_like
'includes the required attributes'
it_behaves_like
'does not include `old_path_with_namespace` attribute'
end
context
'on destroy'
do
let
(
:event
)
{
:destroy
}
it
{
expect
(
event_name
).
to
eq
(
'project_destroy'
)
}
it_behaves_like
'includes the required attributes'
it_behaves_like
'does not include `old_path_with_namespace` attribute'
end
context
'on rename'
do
let
(
:event
)
{
:rename
}
it
{
expect
(
event_name
).
to
eq
(
'project_rename'
)
}
it_behaves_like
'includes the required attributes'
it_behaves_like
'includes `old_path_with_namespace` attribute'
end
context
'on transfer'
do
let
(
:event
)
{
:transfer
}
it
{
expect
(
event_name
).
to
eq
(
'project_transfer'
)
}
it_behaves_like
'includes the required attributes'
it_behaves_like
'includes `old_path_with_namespace` attribute'
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment