From 27d9ac0fe8a33f0e94178c1f46826bc114e16467 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 4 Feb 2013 15:07:56 +0200 Subject: [PATCH] Make gitlab works with gitlab-shell --- app/controllers/application_controller.rb | 5 -- app/models/key.rb | 4 ++ app/models/project.rb | 14 ------ app/models/users_project.rb | 7 --- app/observers/key_observer.rb | 12 ++--- app/observers/project_observer.rb | 16 ++++-- config/initializers/5_backend.rb | 4 +- lib/gitlab/backend/{gitolite.rb => shell.rb} | 51 +++++++++----------- lib/gitolited.rb | 2 +- 9 files changed, 44 insertions(+), 71 deletions(-) rename lib/gitlab/backend/{gitolite.rb => shell.rb} (69%) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ca2a5623f42..1f211bac9c2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,11 +10,6 @@ class ApplicationController < ActionController::Base helper_method :abilities, :can? - rescue_from Gitlab::Gitolite::AccessDenied do |exception| - log_exception(exception) - render "errors/gitolite", layout: "errors", status: 500 - end - rescue_from Encoding::CompatibilityError do |exception| log_exception(exception) render "errors/encoding", layout: "errors", status: 500 diff --git a/app/models/key.rb b/app/models/key.rb index 2bf50f56565..f1c9e42f04f 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -80,4 +80,8 @@ class Key < ActiveRecord::Base def last_deploy? Key.where(identifier: identifier).count == 0 end + + def owner_name + user.username + end end diff --git a/app/models/project.rb b/app/models/project.rb index 6a3d7ab15d2..e774949ed12 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -459,20 +459,6 @@ class Project < ActiveRecord::Base namespace.try(:path) || '' end - def update_repository - GitoliteWorker.perform_async( - :update_repository, - self.id - ) - end - - def destroy_repository - GitoliteWorker.perform_async( - :remove_repository, - self.path_with_namespace - ) - end - def repo_exists? @repo_exists ||= (repository && repository.branches.present?) rescue diff --git a/app/models/users_project.rb b/app/models/users_project.rb index 94edfd9eddf..359db20000b 100644 --- a/app/models/users_project.rb +++ b/app/models/users_project.rb @@ -25,9 +25,6 @@ class UsersProject < ActiveRecord::Base attr_accessor :skip_git - after_save :update_repository, unless: :skip_git? - after_destroy :update_repository, unless: :skip_git? - validates :user, presence: true validates :user_id, uniqueness: { scope: [:project_id], message: "already exists in project" } validates :project_access, inclusion: { in: [GUEST, REPORTER, DEVELOPER, MASTER] }, presence: true @@ -136,10 +133,6 @@ class UsersProject < ActiveRecord::Base end end - def update_repository - project.update_repository - end - def project_access_human Project.access_options.key(self.project_access) end diff --git a/app/observers/key_observer.rb b/app/observers/key_observer.rb index 44e78643d83..261e2245b97 100644 --- a/app/observers/key_observer.rb +++ b/app/observers/key_observer.rb @@ -3,20 +3,16 @@ class KeyObserver < ActiveRecord::Observer def after_save(key) GitoliteWorker.perform_async( - :set_key, - key.identifier, - key.key, - key.projects.map(&:id) + :add_key, + key.owner_name, + key.key ) end def after_destroy(key) - return if key.is_deploy_key && !key.last_deploy? - GitoliteWorker.perform_async( :remove_key, - key.identifier, - key.projects.map(&:id) + key.key, ) end end diff --git a/app/observers/project_observer.rb b/app/observers/project_observer.rb index ccdb146140b..32004503fbf 100644 --- a/app/observers/project_observer.rb +++ b/app/observers/project_observer.rb @@ -1,6 +1,11 @@ class ProjectObserver < ActiveRecord::Observer def after_create(project) - project.update_repository + GitoliteWorker.perform_async( + :add_repository, + project.path_with_namespace + ) + + log_info("#{project.owner.name} created a new project \"#{project.name_with_namespace}\"") end def after_update(project) @@ -8,14 +13,15 @@ class ProjectObserver < ActiveRecord::Observer end def after_destroy(project) - log_info("Project \"#{project.name}\" was removed") + GitoliteWorker.perform_async( + :remove_repository, + self.path_with_namespace + ) project.satellite.destroy project.destroy_repository - end - def after_create project - log_info("#{project.owner.name} created a new project \"#{project.name_with_namespace}\"") + log_info("Project \"#{project.name}\" was removed") end protected diff --git a/config/initializers/5_backend.rb b/config/initializers/5_backend.rb index 85f747ac334..73436608c93 100644 --- a/config/initializers/5_backend.rb +++ b/config/initializers/5_backend.rb @@ -1,5 +1,5 @@ # GIT over HTTP require Rails.root.join("lib", "gitlab", "backend", "grack_auth") -# GITOLITE backend -require Rails.root.join("lib", "gitlab", "backend", "gitolite") +# GIT over SSH +require Rails.root.join("lib", "gitlab", "backend", "shell") diff --git a/lib/gitlab/backend/gitolite.rb b/lib/gitlab/backend/shell.rb similarity index 69% rename from lib/gitlab/backend/gitolite.rb rename to lib/gitlab/backend/shell.rb index 62eb500bcd6..7423b106a45 100644 --- a/lib/gitlab/backend/gitolite.rb +++ b/lib/gitlab/backend/shell.rb @@ -1,27 +1,16 @@ module Gitlab - class Gitolite + class Shell class AccessDenied < StandardError; end - def config - Gitlab::GitoliteConfig.new - end - - # Add new key to gitlab-shell + # Init new repository + # + # name - project path with namespace # # Ex. - # add_key("randx", "sha-rsa ...") + # add_repository("gitlab/gitlab-ci") # - def add_key(username, key_content) - # TODO: implement - end - - # Remove ssh key from gitlab shell - # - # Ex. - # remove_key("sha-rsa") - # - def remove_key(key_content) - # TODO: implement + def add_repository(name) + system("/home/git/gitlab-shell/bin/gitlab-projects add-project #{name}.git") end # Remove repository from file system @@ -32,26 +21,30 @@ module Gitlab # remove_repository("gitlab/gitlab-ci") # def remove_repository(name) - # TODO: implement + system("/home/git/gitlab-shell/bin/gitlab-projects rm-project #{name}.git") end - # Init new repository - # - # name - project path with namespace + # Add new key to gitlab-shell # # Ex. - # add_repository("gitlab/gitlab-ci") + # add_key("randx", "sha-rsa ...") # - def add_repository(name) - # TODO: implement + def add_key(username, key_content) + system("/home/git/gitlab-shell/bin/gitlab-keys add-key #{username} \"#{key_content}\"") end + # Remove ssh key from gitlab shell + # + # Ex. + # remove_key("sha-rsa") + # + def remove_key(key_content) + system("/home/git/gitlab-shell/bin/gitlab-keys rm-key \"#{key_content}\"") + end + + def url_to_repo path Gitlab.config.gitolite.ssh_path_prefix + "#{path}.git" end - - def enable_automerge - config.admin_all_repo! - end end end diff --git a/lib/gitolited.rb b/lib/gitolited.rb index 68b9b625525..4911a473f05 100644 --- a/lib/gitolited.rb +++ b/lib/gitolited.rb @@ -6,6 +6,6 @@ # module Gitolited def gitolite - Gitlab::Gitolite.new + Gitlab::Shell.new end end