Commit 9a407166 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Update hook and support for protected branches

parent 96454ac5
#!/usr/bin/env bash
# Version 4.1
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
......
#!/usr/bin/env ruby
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
refname = ARGV[0]
key_id = ENV['GL_USER']
repo_path = `pwd`
require_relative '../lib/gitlab_update'
GitlabUpdate.new(repo_path, key_id, refname).exec
......@@ -9,7 +9,7 @@ class GitlabNet
project_name = project_name.gsub(/\.git$/, "")
key_id = key.gsub("key-", "")
url = "#{host}/allowed?project=#{project_name}&key_id=#{key_id}&action=#{cmd}&ref=#{ref}"
url = "#{host}/allowed?key_id=#{key_id}&action=#{cmd}&ref=#{ref}&project=#{project_name}"
resp = get(url)
......
......@@ -10,7 +10,6 @@ class GitlabProjects
@project_name = ARGV.shift
@repos_path = GitlabConfig.new.repos_path
@full_path = File.join(@repos_path, @project_name)
@hook_path = File.join(ROOT_PATH, 'hooks', 'post-receive')
end
def exec
......@@ -27,17 +26,24 @@ class GitlabProjects
def add_project
FileUtils.mkdir_p(full_path, mode: 0770)
cmd = "cd #{full_path} && git init --bare && ln -s #{@hook_path} #{full_path}/hooks/post-receive"
cmd = "cd #{full_path} && git init --bare && #{create_hooks_cmd}"
system(cmd)
end
def create_hooks_cmd
pr_hook_path = File.join(ROOT_PATH, 'hooks', 'post-receive')
up_hook_path = File.join(ROOT_PATH, 'hooks', 'update')
"ln -s #{pr_hook_path} #{full_path}/hooks/post-receive && ln -s #{up_hook_path} #{full_path}/hooks/update"
end
def rm_project
FileUtils.rm_rf(full_path)
end
def import_project
dir = @project_name.match(/[a-zA-Z\.\_\-]+\.git$/).to_s
cmd = "cd #{@repos_path} && git clone --bare #{@project_name} #{dir} && ln -s #{@hook_path} #{@repos_path}/#{dir}/hooks/post-receive"
cmd = "cd #{@repos_path} && git clone --bare #{@project_name} #{dir} && #{create_hooks_cmd}"
system(cmd)
end
end
......@@ -49,9 +49,7 @@ class GitlabShell
end
def validate_access
@ref_name = 'master' # just hardcode it cause we dont know ref
api.allowed?(@git_cmd, @repo_name, @key_id, @ref_name)
api.allowed?(@git_cmd, @repo_name, @key_id, '_any')
end
def api
......
require_relative 'gitlab_init'
require_relative 'gitlab_net'
class GitlabUpdate
def initialize(repo_path, key_id, refname)
@repo_name = repo_path
@repo_name.gsub!(GitlabConfig.new.repos_path.to_s, "")
@repo_name.gsub!(/.git$/, "")
@repo_name.gsub!(/^\//, "")
@key_id = key_id
@refname = /refs\/heads\/([\w\.-]+)/.match(refname).to_a.last
end
def exec
if api.allowed?('git-receive-pack', @repo_name, @key_id, @refname)
exit 0
else
puts "GitLab: You are not allowed to access #{@refname}! "
exit 1
end
end
protected
def api
GitlabNet.new
end
end
......@@ -15,7 +15,10 @@ do
then
project_hook="$src/$dir/hooks/post-receive"
gitolite_hook="/home/git/gitlab-shell/hooks/post-receive"
ln -s -f $gitolite_hook $project_hook
project_hook="$src/$dir/hooks/update"
gitolite_hook="/home/git/gitlab-shell/hooks/update"
ln -s -f $gitolite_hook $project_hook
else
for subdir in `ls "$src/$dir/"`
......@@ -23,7 +26,10 @@ do
if [ -d "$src/$dir/$subdir" ] && [[ "$subdir" =~ ^.*.git$ ]]; then
project_hook="$src/$dir/$subdir/hooks/post-receive"
gitolite_hook="/home/git/gitlab-shell/hooks/post-receive"
ln -s -f $gitolite_hook $project_hook
project_hook="$src/$dir/$subdir/hooks/update"
gitolite_hook="/home/git/gitlab-shell/hooks/update"
ln -s -f $gitolite_hook $project_hook
fi
done
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment