Skip to content

Commit

Permalink
Merge branch 'upstream' into feature/update_to_1.7.1
Browse files Browse the repository at this point in the history
* upstream: (62 commits)
  GitLab shell up to 1.7.1
  Fix issue when developers are able to push to protected branch
  Fix typos
  Remove empty post-receive hook
  Remove gitolite support from rewrite-hooks.sh
  Use Tempfile instead of `sed -i`
  Remove unused require statement
  Mention that users can edit the hooks
  Time for 1.7.0
  Ability to clear authorized_keys file
  Version 1.6
  Update README with new methods
  gitlab-shell requires 1.9+ ruby
  Adding tests for the addition of create-branch, create-tag, rm-branch, rm-tag
  Support Adding and Removing of branches and tags This commit adds support to create and remove branches and tags from gitlab-shell.
  Update config.yml.example for grammar
  Version up to 1.5.0
  Update head feature
  Add ca_file/ca_path configuration options.
  added optional argument so to override default repo location
  ...

Conflicts:
	CHANGELOG
	README.md
	VERSION
	config.yml.example
	hooks/post-receive
	lib/gitlab_keys.rb
	lib/gitlab_projects.rb
	lib/gitlab_shell.rb
	spec/gitlab_keys_spec.rb
	spec/gitlab_projects_spec.rb
	support/rewrite-hooks.sh
  • Loading branch information
zzet committed Sep 11, 2013
2 parents 1dac588 + 2c238b7 commit c7eeea1
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 30 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
v1.7.1
- Fixed issue when developers are able to push to protected branches that contain a '/' in the branch name.

v1.7.0
- Clean authorized_keys file with `gitlab-keys clear`

v1.6.0
- Create branch/tag functionality
- Remove branch/tag functionality

v1.5.0
- Logger
- Ability to specify ca_file/ca_path
Expand All @@ -22,6 +32,6 @@ v1.1.0

v1.0.4
- requires gitlab c9ca15e
- dont use post-receive file any more. Make all updates in update
- don't use post-receive file any more. Make all updates in update
- fixed issue with invalid GL_USER
- use GL_ID instead of GL_USER
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* [![Coverage Status](https://coveralls.io/repos/gitlabhq/gitlab-shell/badge.png?branch=master)](https://coveralls.io/r/gitlabhq/gitlab-shell)


__Requires ruby 1.9+__


### Setup

./bin/install
Expand Down Expand Up @@ -37,6 +40,26 @@ Fork repo

./bin/gitlab-projects fork-project gitlab/gitlab-ci.git randx

Update HEAD

./bin/gitlab-projects update-head gitlab/gitlab-ci.git 3-2-stable

Create branch

./bin/gitlab-projects create-branch gitlab/gitlab-ci.git 3-2-stable master

Remove branch

./bin/gitlab-projects rm-branch gitlab/gitlab-ci.git 3-0-stable

Create tag

./bin/gitlab-projects create-tag gitlab/gitlab-ci.git v3.0.0 3-0-stable

Remove tag

./bin/gitlab-projects rm-tag gitlab/gitlab-ci.git v3.0.0


### Keys:

Expand All @@ -49,3 +72,7 @@ Remove key

./bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."

Remove all keys from authorized_keys file

./bin/gitlab-keys clear

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.0
1.7.1
2 changes: 2 additions & 0 deletions bin/gitlab-keys
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require_relative '../lib/gitlab_init'
#
# /bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
#
# /bin/gitlab-keys clear"
#

require File.join(ROOT_PATH, 'lib', 'gitlab_keys')

Expand Down
2 changes: 1 addition & 1 deletion config.yml.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GitLab user. git by default
user: git

# Url to gitlab instance. Used for api calls. Should be ends with slash.
# Url to gitlab instance. Used for api calls. Should end with a slash.
gitlab_url: "http://localhost/"

http_settings:
Expand Down
1 change: 1 addition & 0 deletions hooks/update
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# 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!

refname = ARGV[0]
key_id = ENV['GL_ID']
Expand Down
13 changes: 10 additions & 3 deletions lib/gitlab_keys.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'open3'
require 'tempfile'

require_relative 'gitlab_config'
require_relative 'gitlab_logger'
Expand All @@ -19,6 +19,7 @@ def exec
case @command
when 'add-key'; add_key
when 'rm-key'; rm_key
when 'clear'; clear
else
$logger.warn "Attempt to execute invalid gitlab-keys command #{@command.inspect}."
puts 'not allowed'
Expand All @@ -37,7 +38,13 @@ def add_key

def rm_key
$logger.info "Removing key #{@key_id}"
cmd = "sed -i '/shell #{@key_id}\"/d' #{auth_file}"
system(cmd)
Tempfile.open('authorized_keys') do |temp|
cmd = "sed '/shell #{@key_id}\"/d' #{auth_file} > #{temp.path} && mv #{temp.path} #{auth_file}"
system(cmd)
end
end

def clear
system("echo '# Managed by gitlab-shell' > #{auth_file}")
end
end
32 changes: 31 additions & 1 deletion lib/gitlab_projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def initialize

def exec
case @command
when 'create-branch'; create_branch
when 'rm-branch'; rm_branch
when 'create-tag'; create_tag
when 'rm-tag'; rm_tag
when 'add-project'; add_project
when 'enable-git-protocol'; enable_git_protocol
when 'disable-git-protocol'; disable_git_protocol
Expand All @@ -44,6 +48,32 @@ def exec

protected

def create_branch
branch_name = ARGV.shift
ref = ARGV.shift || "HEAD"
cmd = "cd #{full_path} && git branch #{branch_name} #{ref}"
system(cmd)
end

def rm_branch
branch_name = ARGV.shift
cmd = "cd #{full_path} && git branch -D #{branch_name}"
system(cmd)
end

def create_tag
tag_name = ARGV.shift
ref = ARGV.shift || "HEAD"
cmd = "cd #{full_path} && git tag #{tag_name} #{ref}"
system(cmd)
end

def rm_tag
tag_name = ARGV.shift
cmd = "cd #{full_path} && git tag -d #{tag_name}"
system(cmd)
end

def add_project
$logger.info "Adding project #{@project_name} at <#{full_path}>."
FileUtils.mkdir_p(full_path, mode: 0770)
Expand All @@ -61,7 +91,7 @@ def rm_project
end

# Import project via git clone --bare
# URL must be publicly clonable
# URL must be publicly cloneable
def import_project
@source = ARGV.shift
$logger.info "Importing project #{@project_name} from <#{@source}> to <#{full_path}>."
Expand Down
4 changes: 2 additions & 2 deletions lib/gitlab_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(repo_path, key_id, refname)

@key_id = key_id
@refname = refname
@branch_name = /refs\/heads\/([\w\.-]+)/.match(refname).to_a.last
@branch_name = /refs\/heads\/([\/\w\.-]+)/.match(refname).to_a.last

@oldrev = ARGV[1]
@newrev = ARGV[2]
Expand All @@ -27,7 +27,7 @@ def exec
ENV['GL_ID'] = nil

# If its push over ssh
# we need to check user persmission per branch first
# we need to check user permission per branch first
if ssh?
if api.allowed?('git-receive-pack', @repo_name, @key_id, @branch_name)
update_redis
Expand Down
5 changes: 4 additions & 1 deletion spec/gitlab_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@

describe :rm_key do
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
let(:temp_file) { mock(:temp_file, path: 'tmp_path') }
before { Tempfile.should_receive(:open).and_yield(temp_file) }

it "should receive valid cmd" do
valid_cmd = "sed -i '/shell key-741\"/d' #{GitlabConfig.new.auth_file}"
auth_file = GitlabConfig.new.auth_file
valid_cmd = "sed '/shell key-741\"/d' #{auth_file} > tmp_path && mv tmp_path #{auth_file}"
gitlab_keys.should_receive(:system).with(valid_cmd)
gitlab_keys.send :rm_key
end
Expand Down
68 changes: 68 additions & 0 deletions spec/gitlab_projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,74 @@
it { @gl_projects.instance_variable_get(:@full_path).should == "#{GitlabConfig.new.repos_path}/gitlab-ci.git" }
end

describe :create_branch do
let(:gl_projects_create) {
build_gitlab_projects('import-project', repo_name, 'https://github.com/randx/six.git')
}
let(:gl_projects) { build_gitlab_projects('create-branch', repo_name, 'test_branch', 'master') }

it "should create a branch" do
gl_projects_create.exec
gl_projects.exec
branch_ref = `cd #{tmp_repo_path} && git rev-parse test_branch`.strip
master_ref = `cd #{tmp_repo_path} && git rev-parse master`.strip
branch_ref.should == master_ref
end
end

describe :rm_branch do
let(:gl_projects_create) {
build_gitlab_projects('import-project', repo_name, 'https://github.com/randx/six.git')
}
let(:gl_projects_create_branch) {
build_gitlab_projects('create-branch', repo_name, 'test_branch', 'master')
}
let(:gl_projects) { build_gitlab_projects('rm-branch', repo_name, 'test_branch') }

it "should remove a branch" do
gl_projects_create.exec
gl_projects_create_branch.exec
branch_ref = `cd #{tmp_repo_path} && git rev-parse test_branch`.strip
gl_projects.exec
branch_del = `cd #{tmp_repo_path} && git rev-parse test_branch`.strip
branch_del.should_not == branch_ref
end
end

describe :create_tag do
let(:gl_projects_create) {
build_gitlab_projects('import-project', repo_name, 'https://github.com/randx/six.git')
}
let(:gl_projects) { build_gitlab_projects('create-tag', repo_name, 'test_tag', 'master') }

it "should create a tag" do
gl_projects_create.exec
gl_projects.exec
tag_ref = `cd #{tmp_repo_path} && git rev-parse test_tag`.strip
master_ref = `cd #{tmp_repo_path} && git rev-parse master`.strip
tag_ref.should == master_ref
end
end

describe :rm_tag do
let(:gl_projects_create) {
build_gitlab_projects('import-project', repo_name, 'https://github.com/randx/six.git')
}
let(:gl_projects_create_tag) {
build_gitlab_projects('create-tag', repo_name, 'test_tag', 'master')
}
let(:gl_projects) { build_gitlab_projects('rm-tag', repo_name, 'test_tag') }

it "should remove a branch" do
gl_projects_create.exec
gl_projects_create_tag.exec
branch_ref = `cd #{tmp_repo_path} && git rev-parse test_tag`.strip
gl_projects.exec
branch_del = `cd #{tmp_repo_path} && git rev-parse test_tag`.strip
branch_del.should_not == branch_ref
end
end

describe :add_project do
let(:gl_projects) { build_gitlab_projects('add-project', repo_name) }

Expand Down
26 changes: 6 additions & 20 deletions support/rewrite-hooks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,21 @@
home_dir="/home/git"
src=${1:-"$home_dir/repositories"}

function create_link_in {
ln -s -f "$home_dir/gitlab-shell/hooks/update" "$1/hooks/update"
}

for dir in `ls "$src/"`
do
if [ -d "$src/$dir" ]; then

if [ "$dir" = "gitolite-admin.git" ]
then
continue
fi

if [[ "$dir" =~ ^.*\.git$ ]]
then
project_hook="$src/$dir/hooks/post-receive"
gitolite_hook="$home_dir/gitlab-shell/hooks/post-receive"
ln -s -f $gitolite_hook $project_hook

project_hook="$src/$dir/hooks/update"
gitolite_hook="$home_dir/gitlab-shell/hooks/update"
ln -s -f $gitolite_hook $project_hook
create_link_in "$src/$dir"
else
for subdir in `ls "$src/$dir/"`
do
if [ -d "$src/$dir/$subdir" ] && [[ "$subdir" =~ ^.*\.git$ ]]; then
project_hook="$src/$dir/$subdir/hooks/post-receive"
gitolite_hook="$home_dir/gitlab-shell/hooks/post-receive"
ln -s -f $gitolite_hook $project_hook

project_hook="$src/$dir/$subdir/hooks/update"
gitolite_hook="$home_dir/gitlab-shell/hooks/update"
ln -s -f $gitolite_hook $project_hook
create_link_in "$src/$dir/$subdir"
fi
done
fi
Expand Down

0 comments on commit c7eeea1

Please sign in to comment.