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
Boxiang Sun
gitlab-ce
Commits
85007434
Commit
85007434
authored
Nov 19, 2012
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Post Receive Refactored. Service hooks also triggered now
parent
95c23b2f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
57 deletions
+86
-57
app/roles/push_observer.rb
app/roles/push_observer.rb
+73
-42
spec/models/project_hooks_spec.rb
spec/models/project_hooks_spec.rb
+8
-6
spec/workers/post_receive_spec.rb
spec/workers/post_receive_spec.rb
+5
-9
No files found.
app/roles/push_observer.rb
View file @
85007434
...
@@ -2,45 +2,83 @@
...
@@ -2,45 +2,83 @@
#
#
# Triggered by PostReceive job
# Triggered by PostReceive job
module
PushObserver
module
PushObserver
def
observe_push
(
oldrev
,
newrev
,
ref
,
user
)
# This method will be called after each post receive and only if the provided
# user is present in GitLab.
#
# All callbacks for post receive should be placed here.
def
trigger_post_receive
(
oldrev
,
newrev
,
ref
,
user
)
data
=
post_receive_data
(
oldrev
,
newrev
,
ref
,
user
)
data
=
post_receive_data
(
oldrev
,
newrev
,
ref
,
user
)
Event
.
create
(
# Create push event
project:
self
,
self
.
observe_push
(
data
)
action:
Event
::
Pushed
,
data:
data
,
author_id:
data
[
:user_id
]
)
end
def
update_merge_requests
(
oldrev
,
newrev
,
ref
,
user
)
if
push_to_branch?
ref
,
oldrev
return
true
unless
ref
=~
/heads/
# Close merged MR
branch_name
=
ref
.
gsub
(
"refs/heads/"
,
""
)
self
.
update_merge_requests
(
oldrev
,
newrev
,
ref
,
user
)
c_ids
=
self
.
commits_between
(
oldrev
,
newrev
).
map
(
&
:id
)
# Update code for merge requests
# Execute web hooks
mrs
=
self
.
merge_requests
.
opened
.
find_all_by_branch
(
branch_name
).
all
self
.
execute_hooks
(
data
.
dup
)
mrs
.
each
{
|
merge_request
|
merge_request
.
reload_code
;
merge_request
.
mark_as_unchecked
}
# Close merge requests
# Execute project services
mrs
=
self
.
merge_requests
.
opened
.
where
(
target_branch:
branch_name
).
all
self
.
execute_services
(
data
.
dup
)
mrs
=
mrs
.
select
(
&
:last_commit
).
select
{
|
mr
|
c_ids
.
include?
(
mr
.
last_commit
.
id
)
}
end
mrs
.
each
{
|
merge_request
|
merge_request
.
merge!
(
user
.
id
)
}
true
# Create satellite
self
.
satellite
.
create
unless
self
.
satellite
.
exists?
# Discover the default branch, but only if it hasn't already been set to
# something else
if
default_branch
.
nil?
update_attributes
(
default_branch:
discover_default_branch
)
end
end
end
def
execute_hooks
(
oldrev
,
newrev
,
ref
,
user
)
def
push_to_branch?
ref
,
oldrev
ref_parts
=
ref
.
split
(
'/'
)
ref_parts
=
ref
.
split
(
'/'
)
# Return if this is not a push to a branch (e.g. new commits)
# Return if this is not a push to a branch (e.g. new commits)
return
if
ref_parts
[
1
]
!~
/heads/
||
oldrev
==
"00000000000000000000000000000000"
!
(
ref_parts
[
1
]
!~
/heads/
||
oldrev
==
"00000000000000000000000000000000"
)
end
data
=
post_receive_data
(
oldrev
,
newrev
,
ref
,
user
)
def
observe_push
(
data
)
Event
.
create
(
project:
self
,
action:
Event
::
Pushed
,
data:
data
,
author_id:
data
[
:user_id
]
)
end
def
execute_hooks
(
data
)
hooks
.
each
{
|
hook
|
hook
.
execute
(
data
)
}
hooks
.
each
{
|
hook
|
hook
.
execute
(
data
)
}
end
end
def
execute_services
(
data
)
services
.
each
do
|
service
|
# Call service hook for service if it has one
service
.
service_hook
.
execute
if
service
.
service_hook
end
end
# Produce a hash of post-receive data
#
# data = {
# before: String,
# after: String,
# ref: String,
# user_id: String,
# user_name: String,
# repository: {
# name: String,
# url: String,
# description: String,
# homepage: String,
# },
# commits: Array,
# total_commits_count: Fixnum
# }
#
def
post_receive_data
(
oldrev
,
newrev
,
ref
,
user
)
def
post_receive_data
(
oldrev
,
newrev
,
ref
,
user
)
push_commits
=
commits_between
(
oldrev
,
newrev
)
push_commits
=
commits_between
(
oldrev
,
newrev
)
...
@@ -87,27 +125,20 @@ module PushObserver
...
@@ -87,27 +125,20 @@ module PushObserver
data
data
end
end
# This method will be called after each post receive and only if the provided
def
update_merge_requests
(
oldrev
,
newrev
,
ref
,
user
)
# user is present in GitLab.
return
true
unless
ref
=~
/heads/
#
branch_name
=
ref
.
gsub
(
"refs/heads/"
,
""
)
# All callbacks for post receive should be placed here.
c_ids
=
self
.
commits_between
(
oldrev
,
newrev
).
map
(
&
:id
)
def
trigger_post_receive
(
oldrev
,
newrev
,
ref
,
user
)
# Create push event
self
.
observe_push
(
oldrev
,
newrev
,
ref
,
user
)
# Close merged MR
self
.
update_merge_requests
(
oldrev
,
newrev
,
ref
,
user
)
# Execute web hooks
# Update code for merge requests
self
.
execute_hooks
(
oldrev
,
newrev
,
ref
,
user
)
mrs
=
self
.
merge_requests
.
opened
.
find_all_by_branch
(
branch_name
).
all
mrs
.
each
{
|
merge_request
|
merge_request
.
reload_code
;
merge_request
.
mark_as_unchecked
}
# Create satellite
# Close merge requests
self
.
satellite
.
create
unless
self
.
satellite
.
exists?
mrs
=
self
.
merge_requests
.
opened
.
where
(
target_branch:
branch_name
).
all
mrs
=
mrs
.
select
(
&
:last_commit
).
select
{
|
mr
|
c_ids
.
include?
(
mr
.
last_commit
.
id
)
}
mrs
.
each
{
|
merge_request
|
merge_request
.
merge!
(
user
.
id
)
}
# Discover the default branch, but only if it hasn't already been set to
true
# something else
if
default_branch
.
nil?
update_attributes
(
default_branch:
discover_default_branch
)
end
end
end
end
end
spec/models/project_hooks_spec.rb
View file @
85007434
...
@@ -11,13 +11,15 @@ describe Project, "Hooks" do
...
@@ -11,13 +11,15 @@ describe Project, "Hooks" do
describe
"Post Receive Event"
do
describe
"Post Receive Event"
do
it
"should create push event"
do
it
"should create push event"
do
oldrev
,
newrev
,
ref
=
'00000000000000000000000000000000'
,
'newrev'
,
'refs/heads/master'
oldrev
,
newrev
,
ref
=
'00000000000000000000000000000000'
,
'newrev'
,
'refs/heads/master'
project
.
observe_push
(
oldrev
,
newrev
,
ref
,
@user
)
data
=
project
.
post_receive_data
(
oldrev
,
newrev
,
ref
,
@user
)
project
.
observe_push
(
data
)
event
=
Event
.
last
event
=
Event
.
last
event
.
should_not
be_nil
event
.
should_not
be_nil
event
.
project
.
should
==
project
event
.
project
.
should
==
project
event
.
action
.
should
==
Event
::
Pushed
event
.
action
.
should
==
Event
::
Pushed
event
.
data
==
project
.
post_receive_data
(
oldrev
,
newrev
,
ref
,
@user
)
event
.
data
.
should
==
data
end
end
end
end
...
@@ -25,7 +27,7 @@ describe Project, "Hooks" do
...
@@ -25,7 +27,7 @@ describe Project, "Hooks" do
context
"with no web hooks"
do
context
"with no web hooks"
do
it
"raises no errors"
do
it
"raises no errors"
do
lambda
{
lambda
{
project
.
execute_hooks
(
'oldrev'
,
'newrev'
,
'ref'
,
@user
)
project
.
execute_hooks
(
{}
)
}.
should_not
raise_error
}.
should_not
raise_error
end
end
end
end
...
@@ -41,7 +43,7 @@ describe Project, "Hooks" do
...
@@ -41,7 +43,7 @@ describe Project, "Hooks" do
@project_hook
.
should_receive
(
:execute
).
once
@project_hook
.
should_receive
(
:execute
).
once
@project_hook_2
.
should_receive
(
:execute
).
once
@project_hook_2
.
should_receive
(
:execute
).
once
project
.
execute_hooks
(
'oldrev'
,
'newrev'
,
'refs/heads/master'
,
@user
)
project
.
trigger_post_receive
(
'oldrev'
,
'newrev'
,
'refs/heads/master'
,
@user
)
end
end
end
end
...
@@ -53,12 +55,12 @@ describe Project, "Hooks" do
...
@@ -53,12 +55,12 @@ describe Project, "Hooks" do
it
"when pushing a branch for the first time"
do
it
"when pushing a branch for the first time"
do
@project_hook
.
should_not_receive
(
:execute
)
@project_hook
.
should_not_receive
(
:execute
)
project
.
execute_hooks
(
'00000000000000000000000000000000'
,
'newrev'
,
'refs/heads/master'
,
@user
)
project
.
trigger_post_receive
(
'00000000000000000000000000000000'
,
'newrev'
,
'refs/heads/master'
,
@user
)
end
end
it
"when pushing tags"
do
it
"when pushing tags"
do
@project_hook
.
should_not_receive
(
:execute
)
@project_hook
.
should_not_receive
(
:execute
)
project
.
execute_hooks
(
'oldrev'
,
'newrev'
,
'refs/tags/v1.0.0'
,
@user
)
project
.
trigger_post_receive
(
'oldrev'
,
'newrev'
,
'refs/tags/v1.0.0'
,
@user
)
end
end
end
end
...
...
spec/workers/post_receive_spec.rb
View file @
85007434
...
@@ -27,16 +27,12 @@ describe PostReceive do
...
@@ -27,16 +27,12 @@ describe PostReceive do
PostReceive
.
perform
(
project
.
path
,
'sha-old'
,
'sha-new'
,
'refs/heads/master'
,
key_id
).
should
be_false
PostReceive
.
perform
(
project
.
path
,
'sha-old'
,
'sha-new'
,
'refs/heads/master'
,
key_id
).
should
be_false
end
end
it
"asks the project to
execute web
hooks"
do
it
"asks the project to
trigger all
hooks"
do
Project
.
stub
(
find_by_path:
project
)
Project
.
stub
(
find_by_path:
project
)
project
.
should_receive
(
:execute_hooks
).
with
(
'sha-old'
,
'sha-new'
,
'refs/heads/master'
,
project
.
owner
)
project
.
should_receive
(
:execute_hooks
)
project
.
should_receive
(
:execute_services
)
PostReceive
.
perform
(
project
.
path
,
'sha-old'
,
'sha-new'
,
'refs/heads/master'
,
key_id
)
project
.
should_receive
(
:update_merge_requests
)
end
project
.
should_receive
(
:observe_push
)
it
"asks the project to observe push/create event data"
do
Project
.
stub
(
find_by_path:
project
)
project
.
should_receive
(
:observe_push
).
with
(
'sha-old'
,
'sha-new'
,
'refs/heads/master'
,
project
.
owner
)
PostReceive
.
perform
(
project
.
path
,
'sha-old'
,
'sha-new'
,
'refs/heads/master'
,
key_id
)
PostReceive
.
perform
(
project
.
path
,
'sha-old'
,
'sha-new'
,
'refs/heads/master'
,
key_id
)
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