Commit 7cdc5b9e authored by randx's avatar randx

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
parent aded7056
require 'digest/md5' require 'digest/md5'
class Key < ActiveRecord::Base class Key < ActiveRecord::Base
include SshKey
belongs_to :user belongs_to :user
belongs_to :project belongs_to :project
...@@ -50,6 +49,10 @@ class Key < ActiveRecord::Base ...@@ -50,6 +49,10 @@ class Key < ActiveRecord::Base
user.projects user.projects
end end
end end
def last_deploy?
Key.where(identifier: identifier).count == 0
end
end end
# == Schema Information # == Schema Information
# #
......
class ProtectedBranch < ActiveRecord::Base class ProtectedBranch < ActiveRecord::Base
include GitHost
belongs_to :project belongs_to :project
validates_presence_of :project_id validates_presence_of :project_id
validates_presence_of :name validates_presence_of :name
...@@ -7,7 +9,7 @@ class ProtectedBranch < ActiveRecord::Base ...@@ -7,7 +9,7 @@ class ProtectedBranch < ActiveRecord::Base
after_destroy :update_repository after_destroy :update_repository
def update_repository def update_repository
Gitlab::GitHost.system.update_project(project.path, project) git_host.update_repository(project)
end end
def commit def commit
......
class UsersProject < ActiveRecord::Base class UsersProject < ActiveRecord::Base
include GitHost
GUEST = 10 GUEST = 10
REPORTER = 20 REPORTER = 20
DEVELOPER = 30 DEVELOPER = 30
...@@ -58,9 +60,7 @@ class UsersProject < ActiveRecord::Base ...@@ -58,9 +60,7 @@ class UsersProject < ActiveRecord::Base
end end
def update_repository def update_repository
Gitlab::GitHost.system.new.configure do |c| git_host.update_repository(project)
c.update_project(project.path, project)
end
end end
def project_access_human def project_access_human
......
class KeyObserver < ActiveRecord::Observer class KeyObserver < ActiveRecord::Observer
include GitHost
def after_save(key) def after_save(key)
key.update_repository git_host.set_key(key.identifier, key.key, key.projects)
end end
def after_destroy(key) def after_destroy(key)
key.repository_delete_key return if key.is_deploy_key && !key.last_deploy?
git_host.remove_key(key.identifier, key.projects)
end end
end end
module GitHost
def git_host
Gitlab::Gitolite.new
end
end
module Repository module Repository
include GitHost
def valid_repo? def valid_repo?
repo repo
rescue rescue
...@@ -48,7 +50,7 @@ module Repository ...@@ -48,7 +50,7 @@ module Repository
end end
def url_to_repo def url_to_repo
Gitlab::GitHost.url_to_repo(path) git_host.url_to_repo(path)
end end
def path_to_repo def path_to_repo
...@@ -56,11 +58,11 @@ module Repository ...@@ -56,11 +58,11 @@ module Repository
end end
def update_repository def update_repository
Gitlab::GitHost.system.update_project(path, self) git_host.update_repository(self)
end end
def destroy_repository def destroy_repository
Gitlab::GitHost.system.destroy_project(self) git_host.remove_repository(self)
end end
def repo_exists? def repo_exists?
......
module SshKey
def update_repository
Gitlab::GitHost.system.new.configure do |c|
c.update_keys(identifier, key)
c.update_projects(projects)
end
end
def repository_delete_key
Gitlab::GitHost.system.new.configure do |c|
#delete key file is there is no identically deploy keys
if !is_deploy_key || Key.where(identifier: identifier).count() == 0
c.delete_key(identifier)
end
c.update_projects(projects)
end
end
end
...@@ -3,5 +3,3 @@ require File.expand_path('../application', __FILE__) ...@@ -3,5 +3,3 @@ require File.expand_path('../application', __FILE__)
# Initialize the rails application # Initialize the rails application
Gitlab::Application.initialize! Gitlab::Application.initialize!
require File.join(Rails.root, "lib", "gitlab", "git_host")
# GIT over HTTP
require Rails.root.join("lib", "gitlab", "backend", "grack_auth")
# GITOLITE backend
require Rails.root.join("lib", "gitlab", "backend", "gitolite")
...@@ -5,10 +5,12 @@ end ...@@ -5,10 +5,12 @@ end
require 'cucumber/rails' require 'cucumber/rails'
require 'webmock/cucumber' require 'webmock/cucumber'
WebMock.allow_net_connect! WebMock.allow_net_connect!
require Rails.root.join 'spec/factories' require Rails.root.join 'spec/factories'
require Rails.root.join 'spec/support/monkeypatch' require Rails.root.join 'spec/support/monkeypatch'
require Rails.root.join 'spec/support/gitolite_stub'
require Rails.root.join 'spec/support/login_helpers' require Rails.root.join 'spec/support/login_helpers'
require Rails.root.join 'spec/support/valid_commit' require Rails.root.join 'spec/support/valid_commit'
...@@ -48,3 +50,9 @@ headless = Headless.new ...@@ -48,3 +50,9 @@ headless = Headless.new
headless.start headless.start
require 'cucumber/rspec/doubles' require 'cucumber/rspec/doubles'
include GitoliteStub
Before do
stub_gitolite!
end
...@@ -2,24 +2,62 @@ require 'gitolite' ...@@ -2,24 +2,62 @@ require 'gitolite'
require 'timeout' require 'timeout'
require 'fileutils' require 'fileutils'
# TODO: refactor & cleanup
module Gitlab module Gitlab
class Gitolite class Gitolite
class AccessDenied < StandardError; end class AccessDenied < StandardError; end
def self.update_project(path, project) def set_key key_id, key_content, projects
self.new.configure { |git| git.update_project(path, project) } self.configure do |c|
c.update_keys(key_id, key_content)
c.update_project(project.path, projects)
end end
end
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 self.destroy_project(project) def remove_repository project
self.new.configure { |git| git.destroy_project(project) } self.configure do |c|
c.destroy_project(project)
end 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 def pull
# create tmp dir # create tmp dir
@local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") @local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}")
Dir.mkdir @local_dir Dir.mkdir @local_dir
`git clone #{GitHost.admin_uri} #{@local_dir}/gitolite` `git clone #{self.class.admin_uri} #{@local_dir}/gitolite`
end end
def push def push
......
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
...@@ -2,9 +2,7 @@ namespace :gitlab do ...@@ -2,9 +2,7 @@ namespace :gitlab do
namespace :app do namespace :app do
desc "GITLAB | Enable auto merge" desc "GITLAB | Enable auto merge"
task :enable_automerge => :environment do task :enable_automerge => :environment do
Gitlab::GitHost.system.new.configure do |git| Gitlab::Gitolite.new.enable_automerge
git.admin_all_repo
end
Project.find_each do |project| Project.find_each do |project|
if project.repo_exists? && !project.satellite.exists? if project.repo_exists? && !project.satellite.exists?
......
...@@ -16,7 +16,7 @@ namespace :gitlab do ...@@ -16,7 +16,7 @@ namespace :gitlab do
task :update_keys => :environment do task :update_keys => :environment do
puts "Starting Key" puts "Starting Key"
Key.find_each(:batch_size => 100) do |key| Key.find_each(:batch_size => 100) do |key|
key.update_repository Gitlab::Gitolite.new.set_key(key.identifier, key.key, key.projects)
print '.' print '.'
end end
puts "Done with keys" puts "Done with keys"
......
require 'spec_helper'
describe KeyObserver do
before do
@key = double('Key',
identifier: 'admin_654654',
key: '== a vaild ssh key',
projects: [],
is_deploy_key: false
)
@gitolite = double('Gitlab::Gitolite',
set_key: true,
remove_key: true
)
@observer = KeyObserver.instance
@observer.stub(:git_host => @gitolite)
end
context :after_save do
it do
@gitolite.should_receive(:set_key).with(@key.identifier, @key.key, @key.projects)
@observer.after_save(@key)
end
end
context :after_destroy do
it do
@gitolite.should_receive(:remove_key).with(@key.identifier, @key.projects)
@observer.after_destroy(@key)
end
end
end
...@@ -27,6 +27,7 @@ RSpec.configure do |config| ...@@ -27,6 +27,7 @@ RSpec.configure do |config|
config.mock_with :rspec config.mock_with :rspec
config.include LoginHelpers, type: :request config.include LoginHelpers, type: :request
config.include GitoliteStub
# If you're not using ActiveRecord, or you'd prefer not to run each of your # If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false # examples within a transaction, remove the following line or assign false
...@@ -39,6 +40,8 @@ RSpec.configure do |config| ...@@ -39,6 +40,8 @@ RSpec.configure do |config|
end end
config.before do config.before do
stub_gitolite!
# !!! Observers disabled by default in tests # !!! Observers disabled by default in tests
ActiveRecord::Base.observers.disable(:all) ActiveRecord::Base.observers.disable(:all)
# ActiveRecord::Base.observers.enable(:all) # ActiveRecord::Base.observers.enable(:all)
......
module GitoliteStub
def stub_gitolite!
stub_gitlab_gitolite
stub_gitolite_admin
end
def stub_gitolite_admin
gitolite_repo = mock(
clean_permissions: true,
add_permission: true
)
gitolite_config = mock(
add_repo: true,
get_repo: gitolite_repo,
has_repo?: true
)
gitolite_admin = double(
'Gitolite::GitoliteAdmin',
config: gitolite_config,
save: true,
)
Gitolite::GitoliteAdmin.stub(new: gitolite_admin)
end
def stub_gitlab_gitolite
gitlab_gitolite = Gitlab::Gitolite.new
Gitlab::Gitolite.stub(new: gitlab_gitolite)
gitlab_gitolite.stub(configure: ->() { yield(self) })
gitlab_gitolite.stub(update_keys: true)
end
end
# Stubbing Project <-> git host path # Stubbing Project <-> git host path
# create project using Factory only # create project using Factory only
class Project class Project
def update_repository
true
end
def destroy_repository
true
end
def path_to_repo def path_to_repo
File.join(Rails.root, "tmp", "tests", path) File.join(Rails.root, "tmp", "tests", path)
end end
...@@ -18,22 +10,6 @@ class Project ...@@ -18,22 +10,6 @@ class Project
end end
end end
class Key
def update_repository
true
end
def repository_delete_key
true
end
end
class UsersProject
def update_repository
true
end
end
class FakeSatellite class FakeSatellite
def exists? def exists?
true true
...@@ -43,9 +19,3 @@ class FakeSatellite ...@@ -43,9 +19,3 @@ class FakeSatellite
true true
end end
end end
class ProtectedBranch
def update_repository
true
end
end
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