Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
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
Kazuhiko Shiozaki
gitlab-shell
Commits
ff9808c0
Commit
ff9808c0
authored
Nov 07, 2014
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #190 from dblessing/feature/custom_hooks
Custom hooks
parents
00b3d2cb
b6d84f62
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
99 additions
and
8 deletions
+99
-8
hooks/post-receive
hooks/post-receive
+4
-3
hooks/pre-receive
hooks/pre-receive
+3
-2
hooks/update
hooks/update
+17
-0
lib/gitlab_access.rb
lib/gitlab_access.rb
+1
-1
lib/gitlab_custom_hook.rb
lib/gitlab_custom_hook.rb
+69
-0
lib/gitlab_post_receive.rb
lib/gitlab_post_receive.rb
+5
-2
No files found.
hooks/post-receive
View file @
ff9808c0
...
...
@@ -2,15 +2,16 @@
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
# You can add your own hooks to this file, but be careful when updating gitlab-shell!
change
s
=
ARGF
.
read
ref
s
=
ARGF
.
read
key_id
=
ENV
[
'GL_ID'
]
repo_path
=
Dir
.
pwd
require_relative
'../lib/gitlab_custom_hook'
require_relative
'../lib/gitlab_post_receive'
if
GitlabPostReceive
.
new
(
repo_path
,
key_id
,
changes
).
exec
if
GitlabPostReceive
.
new
(
repo_path
,
key_id
,
refs
).
exec
&&
GitlabCustomHook
.
new
.
post_receive
(
refs
,
repo_path
)
exit
0
else
exit
1
...
...
hooks/pre-receive
View file @
ff9808c0
...
...
@@ -2,15 +2,16 @@
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
# You can add your own hooks to this file, but be careful when updating gitlab-shell!
refs
=
ARGF
.
read
key_id
=
ENV
[
'GL_ID'
]
repo_path
=
Dir
.
pwd
require_relative
'../lib/gitlab_custom_hook'
require_relative
'../lib/gitlab_access'
if
GitlabAccess
.
new
(
repo_path
,
key_id
,
refs
).
exec
if
GitlabAccess
.
new
(
repo_path
,
key_id
,
refs
).
exec
&&
GitlabCustomHook
.
new
.
pre_receive
(
refs
,
repo_path
)
exit
0
else
exit
1
...
...
hooks/update
0 → 100755
View file @
ff9808c0
#!/usr/bin/env ruby
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
ref_name
=
ARGV
[
0
]
old_value
=
ARGV
[
1
]
new_value
=
ARGV
[
2
]
repo_path
=
Dir
.
pwd
require_relative
'../lib/gitlab_custom_hook'
if
GitlabCustomHook
.
new
.
update
(
ref_name
,
old_value
,
new_value
,
repo_path
)
exit
0
else
exit
1
end
lib/gitlab_access.rb
View file @
ff9808c0
...
...
@@ -23,7 +23,7 @@ class GitlabAccess
# reset GL_ID env since we stop git push here
ENV
[
'GL_ID'
]
=
nil
puts
"GitLab: You are not allowed to access some of the refs!"
exit
1
return
false
end
end
...
...
lib/gitlab_custom_hook.rb
0 → 100644
View file @
ff9808c0
require
'open3'
class
GitlabCustomHook
def
pre_receive
(
changes
,
repo_path
)
hook
=
hook_file
(
'pre-receive'
,
repo_path
)
return
true
if
hook
.
nil?
if
call_receive_hook
(
hook
,
changes
)
return
true
else
# reset GL_ID env since we stop git push here
ENV
[
'GL_ID'
]
=
nil
return
false
end
end
def
post_receive
(
changes
,
repo_path
)
hook
=
hook_file
(
'post-receive'
,
repo_path
)
return
true
if
hook
.
nil?
call_receive_hook
(
hook
,
changes
)
?
true
:
false
end
def
update
(
ref_name
,
old_value
,
new_value
,
repo_path
)
hook
=
hook_file
(
'update'
,
repo_path
)
return
true
if
hook
.
nil?
system
(
hook
,
ref_name
,
old_value
,
new_value
)
?
true
:
false
end
private
def
call_receive_hook
(
hook
,
changes
)
# function will return true if succesful
exit_status
=
false
# we combine both stdout and stderr as we don't know what stream
# will be used by the custom hook
Open3
.
popen2e
(
hook
)
do
|
stdin
,
stdout_stderr
,
wait_thr
|
exit_status
=
true
stdin
.
sync
=
true
# in git, pre- and post- receive hooks may just exit without
# reading stdin. We catch the exception to avoid a broken pipe
# warning
begin
# inject all the changes as stdin to the hook
changes
.
lines
do
|
line
|
stdin
.
puts
(
line
)
end
rescue
Errno
::
EPIPE
end
# need to close stdin before reading stdout
stdin
.
close
# only output stdut_stderr if scripts doesn't return 0
unless
wait_thr
.
value
==
0
exit_status
=
false
stdout_stderr
.
each_line
{
|
line
|
puts
line
}
end
end
exit_status
end
def
hook_file
(
hook_type
,
repo_path
)
hook_path
=
File
.
join
(
repo_path
.
strip
,
'custom_hooks'
)
hook_file
=
"
#{
hook_path
}
/
#{
hook_type
}
"
hook_file
if
File
.
exist?
(
hook_file
)
end
end
lib/gitlab_post_receive.rb
View file @
ff9808c0
...
...
@@ -23,9 +23,12 @@ class GitlabPostReceive
def
update_redis
queue
=
"
#{
config
.
redis_namespace
}
:queue:post_receive"
msg
=
JSON
.
dump
({
'class'
=>
'PostReceive'
,
'args'
=>
[
@repo_path
,
@actor
,
@changes
]})
unless
system
(
*
config
.
redis_command
,
'rpush'
,
queue
,
msg
,
err:
'/dev/null'
,
out:
'/dev/null'
)
if
system
(
*
config
.
redis_command
,
'rpush'
,
queue
,
msg
,
err:
'/dev/null'
,
out:
'/dev/null'
)
return
true
else
puts
"GitLab: An unexpected error occurred (redis-cli returned
#{
$?
.
exitstatus
}
)."
exit
1
return
false
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