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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
9611640e
Commit
9611640e
authored
Feb 25, 2013
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3052 from Undev/system-hook-observer-improves
System hooks code & logic refactoring
parents
72d48376
c77730dd
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
68 deletions
+103
-68
app/models/system_hook.rb
app/models/system_hook.rb
+0
-9
app/observers/system_hook_observer.rb
app/observers/system_hook_observer.rb
+3
-59
app/services/system_hooks_service.rb
app/services/system_hooks_service.rb
+59
-0
spec/services/system_hooks_service_spec.rb
spec/services/system_hooks_service_spec.rb
+41
-0
No files found.
app/models/system_hook.rb
View file @
9611640e
...
@@ -12,13 +12,4 @@
...
@@ -12,13 +12,4 @@
#
#
class
SystemHook
<
WebHook
class
SystemHook
<
WebHook
def
self
.
all_hooks_fire
(
data
)
SystemHook
.
all
.
each
do
|
sh
|
sh
.
async_execute
data
end
end
def
async_execute
(
data
)
Sidekiq
::
Client
.
enqueue
(
SystemHookWorker
,
id
,
data
)
end
end
end
app/observers/system_hook_observer.rb
View file @
9611640e
class
SystemHookObserver
<
ActiveRecord
::
Observer
class
SystemHookObserver
<
ActiveRecord
::
Observer
observe
:user
,
:project
,
:users_project
observe
:user
,
:project
,
:users_project
def
after_create
(
model
)
if
model
.
kind_of?
Project
SystemHook
.
all_hooks_fire
({
event_name:
"project_create"
,
name:
model
.
name
,
path:
model
.
path
,
project_id:
model
.
id
,
owner_name:
model
.
owner
.
name
,
owner_email:
model
.
owner
.
email
,
created_at:
model
.
created_at
})
elsif
model
.
kind_of?
User
SystemHook
.
all_hooks_fire
({
event_name:
"user_create"
,
name:
model
.
name
,
email:
model
.
email
,
created_at:
model
.
created_at
})
elsif
model
.
kind_of?
UsersProject
SystemHook
.
all_hooks_fire
({
event_name:
"user_add_to_team"
,
project_name:
model
.
project
.
name
,
project_path:
model
.
project
.
path
,
project_id:
model
.
project_id
,
user_name:
model
.
user
.
name
,
user_email:
model
.
user
.
email
,
project_access:
model
.
repo_access_human
,
created_at:
model
.
created_at
})
end
def
after_create
(
model
)
SystemHooksService
.
execute_hooks_for
(
model
,
:create
)
end
end
def
after_destroy
(
model
)
def
after_destroy
(
model
)
if
model
.
kind_of?
Project
SystemHooksService
.
execute_hooks_for
(
model
,
:destroy
)
SystemHook
.
all_hooks_fire
({
event_name:
"project_destroy"
,
name:
model
.
name
,
path:
model
.
path
,
project_id:
model
.
id
,
owner_name:
model
.
owner
.
name
,
owner_email:
model
.
owner
.
email
,
})
elsif
model
.
kind_of?
User
SystemHook
.
all_hooks_fire
({
event_name:
"user_destroy"
,
name:
model
.
name
,
email:
model
.
email
})
elsif
model
.
kind_of?
UsersProject
SystemHook
.
all_hooks_fire
({
event_name:
"user_remove_from_team"
,
project_name:
model
.
project
.
name
,
project_path:
model
.
project
.
path
,
project_id:
model
.
project_id
,
user_name:
model
.
user
.
name
,
user_email:
model
.
user
.
email
,
project_access:
model
.
repo_access_human
})
end
end
end
end
end
app/services/system_hooks_service.rb
0 → 100644
View file @
9611640e
class
SystemHooksService
def
self
.
execute_hooks_for
(
model
,
event
)
execute_hooks
(
build_event_data
(
model
,
event
))
end
private
def
self
.
execute_hooks
(
data
)
SystemHook
.
all
.
each
do
|
sh
|
async_execute_hook
sh
,
data
end
end
def
self
.
async_execute_hook
(
hook
,
data
)
Sidekiq
::
Client
.
enqueue
(
SystemHookWorker
,
hook
.
id
,
data
)
end
def
self
.
build_event_data
(
model
,
event
)
data
=
{
event_name:
build_event_name
(
model
,
event
),
created_at:
model
.
created_at
}
case
model
when
Project
data
.
merge!
({
name:
model
.
name
,
path:
model
.
path
,
project_id:
model
.
id
,
owner_name:
model
.
owner
.
name
,
owner_email:
model
.
owner
.
email
})
when
User
data
.
merge!
({
name:
model
.
name
,
email:
model
.
email
})
when
UsersProject
data
.
merge!
({
project_name:
model
.
project
.
name
,
project_path:
model
.
project
.
path
,
project_id:
model
.
project_id
,
user_name:
model
.
user
.
name
,
user_email:
model
.
user
.
email
,
project_access:
model
.
repo_access_human
})
end
end
def
self
.
build_event_name
(
model
,
event
)
case
model
when
UsersProject
return
"user_add_to_team"
if
event
==
:create
return
"user_remove_from_team"
if
event
==
:destroy
else
"
#{
model
.
class
.
name
.
downcase
}
_
#{
event
.
to_s
}
"
end
end
end
spec/services/system_hooks_service_spec.rb
0 → 100644
View file @
9611640e
require
'spec_helper'
describe
SystemHooksService
do
let
(
:user
)
{
create
:user
}
let
(
:project
)
{
create
:project
}
let
(
:users_project
)
{
create
:users_project
}
context
'it should build event data'
do
it
'should build event data for user'
do
SystemHooksService
.
build_event_data
(
user
,
:create
).
should
include
(
:event_name
,
:name
,
:created_at
,
:email
)
end
it
'should build event data for project'
do
SystemHooksService
.
build_event_data
(
project
,
:create
).
should
include
(
:event_name
,
:name
,
:created_at
,
:path
,
:project_id
,
:owner_name
,
:owner_email
)
end
it
'should build event data for users project'
do
SystemHooksService
.
build_event_data
(
users_project
,
:create
).
should
include
(
:event_name
,
:created_at
,
:project_name
,
:project_path
,
:project_id
,
:user_name
,
:user_email
,
:project_access
)
end
end
context
'it should build event names'
do
it
'should build event names for user'
do
SystemHooksService
.
build_event_name
(
user
,
:create
).
should
eq
"user_create"
SystemHooksService
.
build_event_name
(
user
,
:destroy
).
should
eq
"user_destroy"
end
it
'should build event names for project'
do
SystemHooksService
.
build_event_name
(
project
,
:create
).
should
eq
"project_create"
SystemHooksService
.
build_event_name
(
project
,
:destroy
).
should
eq
"project_destroy"
end
it
'should build event names for users project'
do
SystemHooksService
.
build_event_name
(
users_project
,
:create
).
should
eq
"user_add_to_team"
SystemHooksService
.
build_event_name
(
users_project
,
:destroy
).
should
eq
"user_remove_from_team"
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