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
f6619809
Commit
f6619809
authored
May 03, 2017
by
Gabriel Mazetto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added repository_update hook
parent
8b17fb61
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
117 additions
and
4 deletions
+117
-4
app/models/hooks/system_hook.rb
app/models/hooks/system_hook.rb
+2
-0
app/models/hooks/web_hook.rb
app/models/hooks/web_hook.rb
+1
-0
app/workers/post_receive.rb
app/workers/post_receive.rb
+22
-3
db/migrate/20170503023315_add_repository_update_events_to_web_hooks.rb
...170503023315_add_repository_update_events_to_web_hooks.rb
+15
-0
db/schema.rb
db/schema.rb
+1
-0
lib/gitlab/data_builder/repository.rb
lib/gitlab/data_builder/repository.rb
+35
-0
lib/gitlab/git_post_receive.rb
lib/gitlab/git_post_receive.rb
+10
-0
spec/lib/gitlab/import_export/safe_model_attributes.yml
spec/lib/gitlab/import_export/safe_model_attributes.yml
+1
-0
spec/models/hooks/system_hook_spec.rb
spec/models/hooks/system_hook_spec.rb
+8
-0
spec/workers/post_receive_spec.rb
spec/workers/post_receive_spec.rb
+22
-1
No files found.
app/models/hooks/system_hook.rb
View file @
f6619809
class
SystemHook
<
WebHook
scope
:repository_update_hooks
,
->
{
where
(
repository_update_events:
true
)
}
def
async_execute
(
data
,
hook_name
)
Sidekiq
::
Client
.
enqueue
(
SystemHookWorker
,
id
,
data
,
hook_name
)
end
...
...
app/models/hooks/web_hook.rb
View file @
f6619809
...
...
@@ -10,6 +10,7 @@ class WebHook < ActiveRecord::Base
default_value_for
:tag_push_events
,
false
default_value_for
:build_events
,
false
default_value_for
:pipeline_events
,
false
default_value_for
:repository_update_events
,
false
default_value_for
:enable_ssl_verification
,
true
scope
:push_hooks
,
->
{
where
(
push_events:
true
)
}
...
...
app/workers/post_receive.rb
View file @
f6619809
...
...
@@ -20,13 +20,32 @@ class PostReceive
# Nothing defined here yet.
else
process_project_changes
(
post_received
)
process_repository_update
(
post_received
)
end
end
def
process_project_changes
(
post_received
)
post_received
.
changes
.
each
do
|
change
|
oldrev
,
newrev
,
ref
=
change
.
strip
.
split
(
' '
)
def
process_repository_update
(
post_received
)
changes
=
[]
refs
=
Set
.
new
post_received
.
changes_refs
do
|
oldrev
,
newrev
,
ref
|
@user
||=
post_received
.
identify
(
newrev
)
unless
@user
log
(
"Triggered hook for non-existing user
\"
#{
post_received
.
identifier
}
\"
"
)
return
false
end
changes
<<
Gitlab
::
DataBuilder
::
Repository
.
single_change
(
oldrev
,
newrev
,
ref
)
refs
<<
ref
end
hook_data
=
Gitlab
::
DataBuilder
::
Repository
.
update
(
post_received
.
project
,
@user
,
changes
,
refs
.
to_a
)
SystemHooksService
.
new
.
execute_hooks
(
hook_data
,
:repository_update_hooks
)
end
def
process_project_changes
(
post_received
)
post_received
.
changes_refs
do
|
oldrev
,
newrev
,
ref
|
@user
||=
post_received
.
identify
(
newrev
)
unless
@user
...
...
db/migrate/20170503023315_add_repository_update_events_to_web_hooks.rb
0 → 100644
View file @
f6619809
class
AddRepositoryUpdateEventsToWebHooks
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
disable_ddl_transaction!
def
up
add_column_with_default
:web_hooks
,
:repository_update_events
,
:boolean
,
default:
false
,
allow_null:
false
end
def
down
remove_column
:web_hooks
,
:repository_update_events
end
end
db/schema.rb
View file @
f6619809
...
...
@@ -1404,6 +1404,7 @@ ActiveRecord::Schema.define(version: 20170508190732) do
t
.
string
"token"
t
.
boolean
"pipeline_events"
,
default:
false
,
null:
false
t
.
boolean
"confidential_issues_events"
,
default:
false
,
null:
false
t
.
boolean
"repository_update_events"
,
default:
false
,
null:
false
end
add_index
"web_hooks"
,
[
"project_id"
],
name:
"index_web_hooks_on_project_id"
,
using: :btree
...
...
lib/gitlab/data_builder/repository.rb
0 → 100644
View file @
f6619809
module
Gitlab
module
DataBuilder
module
Repository
extend
self
# Produce a hash of post-receive data
def
update
(
project
,
user
,
changes
,
refs
)
{
event_name:
'repository_update'
,
user_id:
user
.
id
,
user_name:
user
.
name
,
user_email:
user
.
email
,
user_avatar:
user
.
avatar_url
,
project_id:
project
.
id
,
project:
project
.
hook_attrs
,
changes:
changes
,
refs:
refs
}
end
# Produce a hash of partial data for a single change
def
single_change
(
oldrev
,
newrev
,
ref
)
{
before:
oldrev
,
after:
newrev
,
ref:
ref
}
end
end
end
end
lib/gitlab/git_post_receive.rb
View file @
f6619809
...
...
@@ -13,6 +13,16 @@ module Gitlab
super
(
identifier
,
project
,
revision
)
end
def
changes_refs
return
enum_for
(
:changes_refs
)
unless
block_given?
changes
.
each
do
|
change
|
oldrev
,
newrev
,
ref
=
change
.
strip
.
split
(
' '
)
yield
oldrev
,
newrev
,
ref
end
end
private
def
deserialize_changes
(
changes
)
...
...
spec/lib/gitlab/import_export/safe_model_attributes.yml
View file @
f6619809
...
...
@@ -317,6 +317,7 @@ ProjectHook:
-
token
-
group_id
-
confidential_issues_events
-
repository_update_events
ProtectedBranch
:
-
id
-
project_id
...
...
spec/models/hooks/system_hook_spec.rb
View file @
f6619809
...
...
@@ -105,4 +105,12 @@ describe SystemHook, models: true do
).
once
end
end
describe
'.repository_update_hooks'
do
it
'returns hooks for repository update events only'
do
hook
=
create
(
:system_hook
,
repository_update_events:
true
)
create
(
:system_hook
,
repository_update_events:
false
)
expect
(
SystemHook
.
repository_update_hooks
).
to
eq
([
hook
])
end
end
end
spec/workers/post_receive_spec.rb
View file @
f6619809
...
...
@@ -9,7 +9,7 @@ describe PostReceive do
let
(
:key
)
{
create
(
:key
,
user:
project
.
owner
)
}
let
(
:key_id
)
{
key
.
shell_id
}
context
"as a
resque
worker"
do
context
"as a
sidekiq
worker"
do
it
"reponds to #perform"
do
expect
(
described_class
.
new
).
to
respond_to
(
:perform
)
end
...
...
@@ -93,6 +93,27 @@ describe PostReceive do
end
end
describe
'#process_repository_update'
do
let
(
:changes
)
{
'123456 789012 refs/heads/tést'
}
let
(
:fake_hook_data
)
do
{
event_name:
'repository_update'
}
end
before
do
allow_any_instance_of
(
Gitlab
::
GitPostReceive
).
to
receive
(
:identify
).
and_return
(
project
.
owner
)
allow_any_instance_of
(
Gitlab
::
DataBuilder
::
Repository
).
to
receive
(
:update
).
and_return
(
fake_hook_data
)
# silence hooks so we can isolate
allow_any_instance_of
(
Key
).
to
receive
(
:post_create_hook
).
and_return
(
true
)
allow
(
subject
).
to
receive
(
:process_project_changes
).
and_return
(
true
)
end
it
'calls SystemHooksService'
do
expect_any_instance_of
(
SystemHooksService
).
to
receive
(
:execute_hooks
).
with
(
fake_hook_data
,
:repository_update_hooks
).
and_return
(
true
)
subject
.
perform
(
pwd
(
project
),
key_id
,
base64_changes
)
end
end
context
"webhook"
do
it
"fetches the correct project"
do
expect
(
Project
).
to
receive
(
:find_by
).
with
(
id:
project
.
id
.
to_s
)
...
...
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