Use similar interface to access gitolite

Simplified gitolite handle logic
Stubn over monkeypatch
Stub only specific methods in Gitlab:Gitolite
Moved grach auth to lib
added specs for keys observer
removes SshKey role
This commit is contained in:
randx 2012-08-29 00:04:06 +03:00
parent aded7056fd
commit 7cdc5b9e04
20 changed files with 155 additions and 88 deletions

View file

@ -2,24 +2,62 @@ require 'gitolite'
require 'timeout'
require 'fileutils'
# TODO: refactor & cleanup
module Gitlab
class Gitolite
class AccessDenied < StandardError; end
def self.update_project(path, project)
self.new.configure { |git| git.update_project(path, project) }
def set_key key_id, key_content, projects
self.configure do |c|
c.update_keys(key_id, key_content)
c.update_project(project.path, projects)
end
end
def self.destroy_project(project)
self.new.configure { |git| git.destroy_project(project) }
def remove_key key_id, projects
self.configure do |c|
c.delete_key(key_id)
c.update_project(project.path, projects)
end
end
def update_repository project
self.configure do |c|
c.update_project(project.path, project)
end
end
alias_method :create_repository, :update_repository
def remove_repository project
self.configure do |c|
c.destroy_project(project)
end
end
def url_to_repo path
Gitlab.config.ssh_path + "#{path}.git"
end
def initialize
# create tmp dir
@local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}")
end
def enable_automerge
self.configure do |git|
git.admin_all_repo
end
end
private
def pull
# create tmp dir
@local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}")
Dir.mkdir @local_dir
`git clone #{GitHost.admin_uri} #{@local_dir}/gitolite`
`git clone #{self.class.admin_uri} #{@local_dir}/gitolite`
end
def push

View file

@ -0,0 +1,54 @@
module Grack
class Auth < Rack::Auth::Basic
def valid?
# Authentication with username and password
email, password = @auth.credentials
user = User.find_by_email(email)
return false unless user.try(:valid_password?, password)
# Set GL_USER env variable
ENV['GL_USER'] = email
# Pass Gitolite update hook
ENV['GL_BYPASS_UPDATE_HOOK'] = "true"
# Need this patch because the rails mount
@env['PATH_INFO'] = @env['REQUEST_PATH']
# Find project by PATH_INFO from env
if m = /^\/([\w-]+).git/.match(@env['PATH_INFO']).to_a
return false unless project = Project.find_by_path(m.last)
end
# Git upload and receive
if @env['REQUEST_METHOD'] == 'GET'
true
elsif @env['REQUEST_METHOD'] == 'POST'
if @env['REQUEST_URI'].end_with?('git-upload-pack')
return project.dev_access_for?(user)
elsif @env['REQUEST_URI'].end_with?('git-receive-pack')
if project.protected_branches.map(&:name).include?(current_ref)
project.master_access_for?(user)
else
project.dev_access_for?(user)
end
else
false
end
else
false
end
end# valid?
def current_ref
if @env["HTTP_CONTENT_ENCODING"] =~ /gzip/
input = Zlib::GzipReader.new(@request.body).read
else
input = @request.body.read
end
# Need to reset seek point
@request.body.rewind
/refs\/heads\/([\w-]+)/.match(input).to_a.first
end
end# Auth
end# Grack

View file

@ -1,17 +0,0 @@
require File.join(Rails.root, "lib", "gitlab", "gitolite")
module Gitlab
class GitHost
def self.system
Gitlab::Gitolite
end
def self.admin_uri
Gitlab.config.git_host.admin_uri
end
def self.url_to_repo(path)
Gitlab.config.ssh_path + "#{path}.git"
end
end
end