Skip to content
Browse files

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
  • Loading branch information...
1 parent aded705 commit 7cdc5b9e0438c35c83fce739a764cb146d20c004 @randx randx committed Aug 29, 2012
View
5 app/models/key.rb
@@ -1,7 +1,6 @@
require 'digest/md5'
class Key < ActiveRecord::Base
- include SshKey
belongs_to :user
belongs_to :project
@@ -50,6 +49,10 @@ def projects
user.projects
end
end
+
+ def last_deploy?
+ Key.where(identifier: identifier).count == 0
+ end
end
# == Schema Information
#
View
4 app/models/protected_branch.rb
@@ -1,4 +1,6 @@
class ProtectedBranch < ActiveRecord::Base
+ include GitHost
+
belongs_to :project
validates_presence_of :project_id
validates_presence_of :name
@@ -7,7 +9,7 @@ class ProtectedBranch < ActiveRecord::Base
after_destroy :update_repository
def update_repository
- Gitlab::GitHost.system.update_project(project.path, project)
+ git_host.update_repository(project)
end
def commit
View
6 app/models/users_project.rb
@@ -1,4 +1,6 @@
class UsersProject < ActiveRecord::Base
+ include GitHost
+
GUEST = 10
REPORTER = 20
DEVELOPER = 30
@@ -58,9 +60,7 @@ def role_access
end
def update_repository
- Gitlab::GitHost.system.new.configure do |c|
- c.update_project(project.path, project)
- end
+ git_host.update_repository(project)
end
def project_access_human
View
7 app/observers/key_observer.rb
@@ -1,9 +1,12 @@
class KeyObserver < ActiveRecord::Observer
+ include GitHost
+
def after_save(key)
- key.update_repository
+ git_host.set_key(key.identifier, key.key, key.projects)
end
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
View
5 app/roles/git_host.rb
@@ -0,0 +1,5 @@
+module GitHost
+ def git_host
+ Gitlab::Gitolite.new
+ end
+end
View
2 app/roles/git_merge.rb
@@ -1,2 +0,0 @@
-module GitMerge
-end
View
8 app/roles/repository.rb
@@ -1,4 +1,6 @@
module Repository
+ include GitHost
+
def valid_repo?
repo
rescue
@@ -48,19 +50,19 @@ def repo
end
def url_to_repo
- Gitlab::GitHost.url_to_repo(path)
+ git_host.url_to_repo(path)
end
def path_to_repo
File.join(Gitlab.config.git_base_path, "#{path}.git")
end
def update_repository
- Gitlab::GitHost.system.update_project(path, self)
+ git_host.update_repository(self)
end
def destroy_repository
- Gitlab::GitHost.system.destroy_project(self)
+ git_host.remove_repository(self)
end
def repo_exists?
View
18 app/roles/ssh_key.rb
@@ -1,18 +0,0 @@
-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
View
2 config/environment.rb
@@ -3,5 +3,3 @@
# Initialize the rails application
Gitlab::Application.initialize!
-
-require File.join(Rails.root, "lib", "gitlab", "git_host")
View
5 config/initializers/5_backend.rb
@@ -0,0 +1,5 @@
+# GIT over HTTP
+require Rails.root.join("lib", "gitlab", "backend", "grack_auth")
+
+# GITOLITE backend
+require Rails.root.join("lib", "gitlab", "backend", "gitolite")
View
8 features/support/env.rb
@@ -5,10 +5,12 @@
require 'cucumber/rails'
require 'webmock/cucumber'
+
WebMock.allow_net_connect!
require Rails.root.join 'spec/factories'
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/valid_commit'
@@ -48,3 +50,9 @@
headless.start
require 'cucumber/rspec/doubles'
+
+include GitoliteStub
+
+Before do
+ stub_gitolite!
+end
View
48 lib/gitlab/gitolite.rb → lib/gitlab/backend/gitolite.rb
@@ -2,24 +2,62 @@
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 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
- def self.destroy_project(project)
- self.new.configure { |git| git.destroy_project(project) }
+ 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
0 config/initializers/grack_auth.rb → lib/gitlab/backend/grack_auth.rb
File renamed without changes.
View
17 lib/gitlab/git_host.rb
@@ -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
View
4 lib/tasks/gitlab/enable_automerge.rake
@@ -2,9 +2,7 @@ namespace :gitlab do
namespace :app do
desc "GITLAB | Enable auto merge"
task :enable_automerge => :environment do
- Gitlab::GitHost.system.new.configure do |git|
- git.admin_all_repo
- end
+ Gitlab::Gitolite.new.enable_automerge
Project.find_each do |project|
if project.repo_exists? && !project.satellite.exists?
View
2 lib/tasks/gitlab/gitolite_rebuild.rake
@@ -16,7 +16,7 @@ namespace :gitlab do
task :update_keys => :environment do
puts "Starting Key"
Key.find_each(:batch_size => 100) do |key|
- key.update_repository
+ Gitlab::Gitolite.new.set_key(key.identifier, key.key, key.projects)
print '.'
end
puts "Done with keys"
View
34 spec/observers/key_observer_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe KeyObserver do
+ before do
+ @key = double('Key',
+ identifier: 'admin_654654',
+ key: '== a vaild ssh key',
@timspin
timspin added a note Aug 29, 2012

here is mistake in word "valid"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
View
3 spec/spec_helper.rb
@@ -27,6 +27,7 @@
config.mock_with :rspec
config.include LoginHelpers, type: :request
+ config.include GitoliteStub
# 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
@@ -39,6 +40,8 @@
end
config.before do
+ stub_gitolite!
+
# !!! Observers disabled by default in tests
ActiveRecord::Base.observers.disable(:all)
# ActiveRecord::Base.observers.enable(:all)
View
35 spec/support/gitolite_stub.rb
@@ -0,0 +1,35 @@
+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
View
30 spec/support/monkeypatch.rb
@@ -1,14 +1,6 @@
# Stubbing Project <-> git host path
# create project using Factory only
class Project
- def update_repository
- true
- end
-
- def destroy_repository
- true
- end
-
def path_to_repo
File.join(Rails.root, "tmp", "tests", path)
end
@@ -18,22 +10,6 @@ def satellite
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
def exists?
true
@@ -43,9 +19,3 @@ def create
true
end
end
-
-class ProtectedBranch
- def update_repository
- true
- end
-end

0 comments on commit 7cdc5b9

Please sign in to comment.
Something went wrong with that request. Please try again.