diff --git a/app/observers/key_observer.rb b/app/observers/key_observer.rb index bf5fa647..44e78643 100644 --- a/app/observers/key_observer.rb +++ b/app/observers/key_observer.rb @@ -2,11 +2,21 @@ class KeyObserver < ActiveRecord::Observer include Gitolited def after_save(key) - gitolite.set_key(key.identifier, key.key, key.projects) + GitoliteWorker.perform_async( + :set_key, + key.identifier, + key.key, + key.projects.map(&:id) + ) end def after_destroy(key) return if key.is_deploy_key && !key.last_deploy? - gitolite.remove_key(key.identifier, key.projects) + + GitoliteWorker.perform_async( + :remove_key, + key.identifier, + key.projects.map(&:id) + ) end end diff --git a/app/workers/gitolite_worker.rb b/app/workers/gitolite_worker.rb index d134ea03..bff7a8c6 100644 --- a/app/workers/gitolite_worker.rb +++ b/app/workers/gitolite_worker.rb @@ -4,7 +4,7 @@ class GitoliteWorker sidekiq_options queue: :gitolite - def perform(action, arg) - gitolite.send(action, arg) + def perform(action, *arg) + gitolite.send(action, *arg) end end diff --git a/lib/gitlab/backend/gitolite.rb b/lib/gitlab/backend/gitolite.rb index 1bcf1264..cd9ac155 100644 --- a/lib/gitlab/backend/gitolite.rb +++ b/lib/gitlab/backend/gitolite.rb @@ -8,14 +8,28 @@ module Gitlab Gitlab::GitoliteConfig.new end - def set_key key_id, key_content, projects + # Update gitolite config with new key + # + # Ex. + # set_key("m_gitlab_com_12343", "sha-rsa ...", [2, 3, 6]) + # + def set_key(key_id, key_content, project_ids) + projects = Project.where(id: project_ids) + config.apply do |config| config.write_key(key_id, key_content) config.update_projects(projects) end end - def remove_key key_id, projects + # Remove ssh key from gitolite config + # + # Ex. + # remove_key("m_gitlab_com_12343", [2, 3, 6]) + # + def remove_key(key_id, project_ids) + projects = Project.where(id: project_ids) + config.apply do |config| config.rm_key(key_id) config.update_projects(projects)