Commit 5d88cdd9b82251b70559ba5172c15e333f28ff3c
Exists in
master
and in
4 other branches
Merge branch 'refactor/githost'
Showing
22 changed files
with
361 additions
and
294 deletions
Show diff stats
app/models/key.rb
| 1 | 1 | require 'digest/md5' |
| 2 | 2 | |
| 3 | 3 | class Key < ActiveRecord::Base |
| 4 | - include SshKey | |
| 5 | 4 | belongs_to :user |
| 6 | 5 | belongs_to :project |
| 7 | 6 | |
| ... | ... | @@ -50,6 +49,10 @@ class Key < ActiveRecord::Base |
| 50 | 49 | user.projects |
| 51 | 50 | end |
| 52 | 51 | end |
| 52 | + | |
| 53 | + def last_deploy? | |
| 54 | + Key.where(identifier: identifier).count == 0 | |
| 55 | + end | |
| 53 | 56 | end |
| 54 | 57 | # == Schema Information |
| 55 | 58 | # | ... | ... |
app/models/protected_branch.rb
| 1 | 1 | class ProtectedBranch < ActiveRecord::Base |
| 2 | + include GitHost | |
| 3 | + | |
| 2 | 4 | belongs_to :project |
| 3 | 5 | validates_presence_of :project_id |
| 4 | 6 | validates_presence_of :name |
| ... | ... | @@ -7,7 +9,7 @@ class ProtectedBranch < ActiveRecord::Base |
| 7 | 9 | after_destroy :update_repository |
| 8 | 10 | |
| 9 | 11 | def update_repository |
| 10 | - Gitlab::GitHost.system.update_project(project.path, project) | |
| 12 | + git_host.update_repository(project) | |
| 11 | 13 | end |
| 12 | 14 | |
| 13 | 15 | def commit | ... | ... |
app/models/users_project.rb
| 1 | 1 | class UsersProject < ActiveRecord::Base |
| 2 | + include GitHost | |
| 3 | + | |
| 2 | 4 | GUEST = 10 |
| 3 | 5 | REPORTER = 20 |
| 4 | 6 | DEVELOPER = 30 |
| ... | ... | @@ -58,9 +60,7 @@ class UsersProject < ActiveRecord::Base |
| 58 | 60 | end |
| 59 | 61 | |
| 60 | 62 | def update_repository |
| 61 | - Gitlab::GitHost.system.new.configure do |c| | |
| 62 | - c.update_project(project.path, project) | |
| 63 | - end | |
| 63 | + git_host.update_repository(project) | |
| 64 | 64 | end |
| 65 | 65 | |
| 66 | 66 | def project_access_human | ... | ... |
app/observers/key_observer.rb
| 1 | 1 | class KeyObserver < ActiveRecord::Observer |
| 2 | + include GitHost | |
| 3 | + | |
| 2 | 4 | def after_save(key) |
| 3 | - key.update_repository | |
| 5 | + git_host.set_key(key.identifier, key.key, key.projects) | |
| 4 | 6 | end |
| 5 | 7 | |
| 6 | 8 | def after_destroy(key) |
| 7 | - key.repository_delete_key | |
| 9 | + return if key.is_deploy_key && !key.last_deploy? | |
| 10 | + git_host.remove_key(key.identifier, key.projects) | |
| 8 | 11 | end |
| 9 | 12 | end | ... | ... |
app/roles/git_merge.rb
app/roles/repository.rb
| 1 | 1 | module Repository |
| 2 | + include GitHost | |
| 3 | + | |
| 2 | 4 | def valid_repo? |
| 3 | 5 | repo |
| 4 | 6 | rescue |
| ... | ... | @@ -48,7 +50,7 @@ module Repository |
| 48 | 50 | end |
| 49 | 51 | |
| 50 | 52 | def url_to_repo |
| 51 | - Gitlab::GitHost.url_to_repo(path) | |
| 53 | + git_host.url_to_repo(path) | |
| 52 | 54 | end |
| 53 | 55 | |
| 54 | 56 | def path_to_repo |
| ... | ... | @@ -56,11 +58,11 @@ module Repository |
| 56 | 58 | end |
| 57 | 59 | |
| 58 | 60 | def update_repository |
| 59 | - Gitlab::GitHost.system.update_project(path, self) | |
| 61 | + git_host.update_repository(self) | |
| 60 | 62 | end |
| 61 | 63 | |
| 62 | 64 | def destroy_repository |
| 63 | - Gitlab::GitHost.system.destroy_project(self) | |
| 65 | + git_host.remove_repository(self) | |
| 64 | 66 | end |
| 65 | 67 | |
| 66 | 68 | def repo_exists? | ... | ... |
app/roles/ssh_key.rb
| ... | ... | @@ -1,18 +0,0 @@ |
| 1 | -module SshKey | |
| 2 | - def update_repository | |
| 3 | - Gitlab::GitHost.system.new.configure do |c| | |
| 4 | - c.update_keys(identifier, key) | |
| 5 | - c.update_projects(projects) | |
| 6 | - end | |
| 7 | - end | |
| 8 | - | |
| 9 | - def repository_delete_key | |
| 10 | - Gitlab::GitHost.system.new.configure do |c| | |
| 11 | - #delete key file is there is no identically deploy keys | |
| 12 | - if !is_deploy_key || Key.where(identifier: identifier).count() == 0 | |
| 13 | - c.delete_key(identifier) | |
| 14 | - end | |
| 15 | - c.update_projects(projects) | |
| 16 | - end | |
| 17 | - end | |
| 18 | -end |
config/environment.rb
config/initializers/grack_auth.rb
| ... | ... | @@ -1,54 +0,0 @@ |
| 1 | -module Grack | |
| 2 | - class Auth < Rack::Auth::Basic | |
| 3 | - | |
| 4 | - def valid? | |
| 5 | - # Authentication with username and password | |
| 6 | - email, password = @auth.credentials | |
| 7 | - user = User.find_by_email(email) | |
| 8 | - return false unless user.try(:valid_password?, password) | |
| 9 | - | |
| 10 | - # Set GL_USER env variable | |
| 11 | - ENV['GL_USER'] = email | |
| 12 | - # Pass Gitolite update hook | |
| 13 | - ENV['GL_BYPASS_UPDATE_HOOK'] = "true" | |
| 14 | - | |
| 15 | - # Need this patch because the rails mount | |
| 16 | - @env['PATH_INFO'] = @env['REQUEST_PATH'] | |
| 17 | - | |
| 18 | - # Find project by PATH_INFO from env | |
| 19 | - if m = /^\/([\w-]+).git/.match(@env['PATH_INFO']).to_a | |
| 20 | - return false unless project = Project.find_by_path(m.last) | |
| 21 | - end | |
| 22 | - | |
| 23 | - # Git upload and receive | |
| 24 | - if @env['REQUEST_METHOD'] == 'GET' | |
| 25 | - true | |
| 26 | - elsif @env['REQUEST_METHOD'] == 'POST' | |
| 27 | - if @env['REQUEST_URI'].end_with?('git-upload-pack') | |
| 28 | - return project.dev_access_for?(user) | |
| 29 | - elsif @env['REQUEST_URI'].end_with?('git-receive-pack') | |
| 30 | - if project.protected_branches.map(&:name).include?(current_ref) | |
| 31 | - project.master_access_for?(user) | |
| 32 | - else | |
| 33 | - project.dev_access_for?(user) | |
| 34 | - end | |
| 35 | - else | |
| 36 | - false | |
| 37 | - end | |
| 38 | - else | |
| 39 | - false | |
| 40 | - end | |
| 41 | - end# valid? | |
| 42 | - | |
| 43 | - def current_ref | |
| 44 | - if @env["HTTP_CONTENT_ENCODING"] =~ /gzip/ | |
| 45 | - input = Zlib::GzipReader.new(@request.body).read | |
| 46 | - else | |
| 47 | - input = @request.body.read | |
| 48 | - end | |
| 49 | - # Need to reset seek point | |
| 50 | - @request.body.rewind | |
| 51 | - /refs\/heads\/([\w-]+)/.match(input).to_a.first | |
| 52 | - end | |
| 53 | - end# Auth | |
| 54 | -end# Grack |
features/support/env.rb
| ... | ... | @@ -5,10 +5,12 @@ end |
| 5 | 5 | |
| 6 | 6 | require 'cucumber/rails' |
| 7 | 7 | require 'webmock/cucumber' |
| 8 | + | |
| 8 | 9 | WebMock.allow_net_connect! |
| 9 | 10 | |
| 10 | 11 | require Rails.root.join 'spec/factories' |
| 11 | 12 | require Rails.root.join 'spec/support/monkeypatch' |
| 13 | +require Rails.root.join 'spec/support/gitolite_stub' | |
| 12 | 14 | require Rails.root.join 'spec/support/login_helpers' |
| 13 | 15 | require Rails.root.join 'spec/support/valid_commit' |
| 14 | 16 | |
| ... | ... | @@ -48,3 +50,9 @@ headless = Headless.new |
| 48 | 50 | headless.start |
| 49 | 51 | |
| 50 | 52 | require 'cucumber/rspec/doubles' |
| 53 | + | |
| 54 | +include GitoliteStub | |
| 55 | + | |
| 56 | +Before do | |
| 57 | + stub_gitolite! | |
| 58 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,195 @@ |
| 1 | +require 'gitolite' | |
| 2 | +require 'timeout' | |
| 3 | +require 'fileutils' | |
| 4 | + | |
| 5 | +# TODO: refactor & cleanup | |
| 6 | +module Gitlab | |
| 7 | + class Gitolite | |
| 8 | + class AccessDenied < StandardError; end | |
| 9 | + | |
| 10 | + def set_key key_id, key_content, projects | |
| 11 | + self.configure do |c| | |
| 12 | + c.update_keys(key_id, key_content) | |
| 13 | + c.update_project(project.path, projects) | |
| 14 | + end | |
| 15 | + end | |
| 16 | + | |
| 17 | + def remove_key key_id, projects | |
| 18 | + self.configure do |c| | |
| 19 | + c.delete_key(key_id) | |
| 20 | + c.update_project(project.path, projects) | |
| 21 | + end | |
| 22 | + end | |
| 23 | + | |
| 24 | + def update_repository project | |
| 25 | + self.configure do |c| | |
| 26 | + c.update_project(project.path, project) | |
| 27 | + end | |
| 28 | + end | |
| 29 | + | |
| 30 | + alias_method :create_repository, :update_repository | |
| 31 | + | |
| 32 | + def remove_repository project | |
| 33 | + self.configure do |c| | |
| 34 | + c.destroy_project(project) | |
| 35 | + end | |
| 36 | + end | |
| 37 | + | |
| 38 | + def url_to_repo path | |
| 39 | + Gitlab.config.ssh_path + "#{path}.git" | |
| 40 | + end | |
| 41 | + | |
| 42 | + def initialize | |
| 43 | + # create tmp dir | |
| 44 | + @local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") | |
| 45 | + end | |
| 46 | + | |
| 47 | + def enable_automerge | |
| 48 | + self.configure do |git| | |
| 49 | + git.admin_all_repo | |
| 50 | + end | |
| 51 | + end | |
| 52 | + | |
| 53 | + private | |
| 54 | + | |
| 55 | + def pull | |
| 56 | + # create tmp dir | |
| 57 | + @local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") | |
| 58 | + Dir.mkdir @local_dir | |
| 59 | + | |
| 60 | + `git clone #{self.class.admin_uri} #{@local_dir}/gitolite` | |
| 61 | + end | |
| 62 | + | |
| 63 | + def push | |
| 64 | + Dir.chdir(File.join(@local_dir, "gitolite")) | |
| 65 | + `git add -A` | |
| 66 | + `git commit -am "Gitlab"` | |
| 67 | + `git push` | |
| 68 | + Dir.chdir(Rails.root) | |
| 69 | + | |
| 70 | + FileUtils.rm_rf(@local_dir) | |
| 71 | + end | |
| 72 | + | |
| 73 | + def configure | |
| 74 | + Timeout::timeout(30) do | |
| 75 | + File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| | |
| 76 | + begin | |
| 77 | + f.flock(File::LOCK_EX) | |
| 78 | + pull | |
| 79 | + yield(self) | |
| 80 | + push | |
| 81 | + ensure | |
| 82 | + f.flock(File::LOCK_UN) | |
| 83 | + end | |
| 84 | + end | |
| 85 | + end | |
| 86 | + rescue Exception => ex | |
| 87 | + Gitlab::Logger.error(ex.message) | |
| 88 | + raise Gitolite::AccessDenied.new("gitolite timeout") | |
| 89 | + end | |
| 90 | + | |
| 91 | + def destroy_project(project) | |
| 92 | + FileUtils.rm_rf(project.path_to_repo) | |
| 93 | + | |
| 94 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 95 | + conf = ga_repo.config | |
| 96 | + conf.rm_repo(project.path) | |
| 97 | + ga_repo.save | |
| 98 | + end | |
| 99 | + | |
| 100 | + #update or create | |
| 101 | + def update_keys(user, key) | |
| 102 | + File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) } | |
| 103 | + end | |
| 104 | + | |
| 105 | + def delete_key(user) | |
| 106 | + File.unlink(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub")) | |
| 107 | + `cd #{File.join(@local_dir,'gitolite')} ; git rm keydir/#{user}.pub` | |
| 108 | + end | |
| 109 | + | |
| 110 | + # update or create | |
| 111 | + def update_project(repo_name, project) | |
| 112 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 113 | + conf = ga_repo.config | |
| 114 | + repo = update_project_config(project, conf) | |
| 115 | + conf.add_repo(repo, true) | |
| 116 | + | |
| 117 | + ga_repo.save | |
| 118 | + end | |
| 119 | + | |
| 120 | + # Updates many projects and uses project.path as the repo path | |
| 121 | + # An order of magnitude faster than update_project | |
| 122 | + def update_projects(projects) | |
| 123 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 124 | + conf = ga_repo.config | |
| 125 | + | |
| 126 | + projects.each do |project| | |
| 127 | + repo = update_project_config(project, conf) | |
| 128 | + conf.add_repo(repo, true) | |
| 129 | + end | |
| 130 | + | |
| 131 | + ga_repo.save | |
| 132 | + end | |
| 133 | + | |
| 134 | + def update_project_config(project, conf) | |
| 135 | + repo_name = project.path | |
| 136 | + | |
| 137 | + repo = if conf.has_repo?(repo_name) | |
| 138 | + conf.get_repo(repo_name) | |
| 139 | + else | |
| 140 | + ::Gitolite::Config::Repo.new(repo_name) | |
| 141 | + end | |
| 142 | + | |
| 143 | + name_readers = project.repository_readers | |
| 144 | + name_writers = project.repository_writers | |
| 145 | + name_masters = project.repository_masters | |
| 146 | + | |
| 147 | + pr_br = project.protected_branches.map(&:name).join("$ ") | |
| 148 | + | |
| 149 | + repo.clean_permissions | |
| 150 | + | |
| 151 | + # Deny access to protected branches for writers | |
| 152 | + unless name_writers.blank? || pr_br.blank? | |
| 153 | + repo.add_permission("-", pr_br.strip + "$ ", name_writers) | |
| 154 | + end | |
| 155 | + | |
| 156 | + # Add read permissions | |
| 157 | + repo.add_permission("R", "", name_readers) unless name_readers.blank? | |
| 158 | + | |
| 159 | + # Add write permissions | |
| 160 | + repo.add_permission("RW+", "", name_writers) unless name_writers.blank? | |
| 161 | + repo.add_permission("RW+", "", name_masters) unless name_masters.blank? | |
| 162 | + | |
| 163 | + repo | |
| 164 | + end | |
| 165 | + | |
| 166 | + def admin_all_repo | |
| 167 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 168 | + conf = ga_repo.config | |
| 169 | + owner_name = "" | |
| 170 | + | |
| 171 | + # Read gitolite-admin user | |
| 172 | + # | |
| 173 | + begin | |
| 174 | + repo = conf.get_repo("gitolite-admin") | |
| 175 | + owner_name = repo.permissions[0]["RW+"][""][0] | |
| 176 | + raise StandardError if owner_name.blank? | |
| 177 | + rescue => ex | |
| 178 | + puts "Can't determine gitolite-admin owner".red | |
| 179 | + raise StandardError | |
| 180 | + end | |
| 181 | + | |
| 182 | + # @ALL repos premission for gitolite owner | |
| 183 | + repo_name = "@all" | |
| 184 | + repo = if conf.has_repo?(repo_name) | |
| 185 | + conf.get_repo(repo_name) | |
| 186 | + else | |
| 187 | + ::Gitolite::Config::Repo.new(repo_name) | |
| 188 | + end | |
| 189 | + | |
| 190 | + repo.add_permission("RW+", "", owner_name) | |
| 191 | + conf.add_repo(repo, true) | |
| 192 | + ga_repo.save | |
| 193 | + end | |
| 194 | + end | |
| 195 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,54 @@ |
| 1 | +module Grack | |
| 2 | + class Auth < Rack::Auth::Basic | |
| 3 | + | |
| 4 | + def valid? | |
| 5 | + # Authentication with username and password | |
| 6 | + email, password = @auth.credentials | |
| 7 | + user = User.find_by_email(email) | |
| 8 | + return false unless user.try(:valid_password?, password) | |
| 9 | + | |
| 10 | + # Set GL_USER env variable | |
| 11 | + ENV['GL_USER'] = email | |
| 12 | + # Pass Gitolite update hook | |
| 13 | + ENV['GL_BYPASS_UPDATE_HOOK'] = "true" | |
| 14 | + | |
| 15 | + # Need this patch because the rails mount | |
| 16 | + @env['PATH_INFO'] = @env['REQUEST_PATH'] | |
| 17 | + | |
| 18 | + # Find project by PATH_INFO from env | |
| 19 | + if m = /^\/([\w-]+).git/.match(@env['PATH_INFO']).to_a | |
| 20 | + return false unless project = Project.find_by_path(m.last) | |
| 21 | + end | |
| 22 | + | |
| 23 | + # Git upload and receive | |
| 24 | + if @env['REQUEST_METHOD'] == 'GET' | |
| 25 | + true | |
| 26 | + elsif @env['REQUEST_METHOD'] == 'POST' | |
| 27 | + if @env['REQUEST_URI'].end_with?('git-upload-pack') | |
| 28 | + return project.dev_access_for?(user) | |
| 29 | + elsif @env['REQUEST_URI'].end_with?('git-receive-pack') | |
| 30 | + if project.protected_branches.map(&:name).include?(current_ref) | |
| 31 | + project.master_access_for?(user) | |
| 32 | + else | |
| 33 | + project.dev_access_for?(user) | |
| 34 | + end | |
| 35 | + else | |
| 36 | + false | |
| 37 | + end | |
| 38 | + else | |
| 39 | + false | |
| 40 | + end | |
| 41 | + end# valid? | |
| 42 | + | |
| 43 | + def current_ref | |
| 44 | + if @env["HTTP_CONTENT_ENCODING"] =~ /gzip/ | |
| 45 | + input = Zlib::GzipReader.new(@request.body).read | |
| 46 | + else | |
| 47 | + input = @request.body.read | |
| 48 | + end | |
| 49 | + # Need to reset seek point | |
| 50 | + @request.body.rewind | |
| 51 | + /refs\/heads\/([\w-]+)/.match(input).to_a.first | |
| 52 | + end | |
| 53 | + end# Auth | |
| 54 | +end# Grack | ... | ... |
lib/gitlab/git_host.rb
| ... | ... | @@ -1,17 +0,0 @@ |
| 1 | -require File.join(Rails.root, "lib", "gitlab", "gitolite") | |
| 2 | - | |
| 3 | -module Gitlab | |
| 4 | - class GitHost | |
| 5 | - def self.system | |
| 6 | - Gitlab::Gitolite | |
| 7 | - end | |
| 8 | - | |
| 9 | - def self.admin_uri | |
| 10 | - Gitlab.config.git_host.admin_uri | |
| 11 | - end | |
| 12 | - | |
| 13 | - def self.url_to_repo(path) | |
| 14 | - Gitlab.config.ssh_path + "#{path}.git" | |
| 15 | - end | |
| 16 | - end | |
| 17 | -end |
lib/gitlab/gitolite.rb
| ... | ... | @@ -1,157 +0,0 @@ |
| 1 | -require 'gitolite' | |
| 2 | -require 'timeout' | |
| 3 | -require 'fileutils' | |
| 4 | - | |
| 5 | -module Gitlab | |
| 6 | - class Gitolite | |
| 7 | - class AccessDenied < StandardError; end | |
| 8 | - | |
| 9 | - def self.update_project(path, project) | |
| 10 | - self.new.configure { |git| git.update_project(path, project) } | |
| 11 | - end | |
| 12 | - | |
| 13 | - def self.destroy_project(project) | |
| 14 | - self.new.configure { |git| git.destroy_project(project) } | |
| 15 | - end | |
| 16 | - | |
| 17 | - def pull | |
| 18 | - # create tmp dir | |
| 19 | - @local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") | |
| 20 | - Dir.mkdir @local_dir | |
| 21 | - | |
| 22 | - `git clone #{GitHost.admin_uri} #{@local_dir}/gitolite` | |
| 23 | - end | |
| 24 | - | |
| 25 | - def push | |
| 26 | - Dir.chdir(File.join(@local_dir, "gitolite")) | |
| 27 | - `git add -A` | |
| 28 | - `git commit -am "Gitlab"` | |
| 29 | - `git push` | |
| 30 | - Dir.chdir(Rails.root) | |
| 31 | - | |
| 32 | - FileUtils.rm_rf(@local_dir) | |
| 33 | - end | |
| 34 | - | |
| 35 | - def configure | |
| 36 | - Timeout::timeout(30) do | |
| 37 | - File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| | |
| 38 | - begin | |
| 39 | - f.flock(File::LOCK_EX) | |
| 40 | - pull | |
| 41 | - yield(self) | |
| 42 | - push | |
| 43 | - ensure | |
| 44 | - f.flock(File::LOCK_UN) | |
| 45 | - end | |
| 46 | - end | |
| 47 | - end | |
| 48 | - rescue Exception => ex | |
| 49 | - Gitlab::Logger.error(ex.message) | |
| 50 | - raise Gitolite::AccessDenied.new("gitolite timeout") | |
| 51 | - end | |
| 52 | - | |
| 53 | - def destroy_project(project) | |
| 54 | - FileUtils.rm_rf(project.path_to_repo) | |
| 55 | - | |
| 56 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 57 | - conf = ga_repo.config | |
| 58 | - conf.rm_repo(project.path) | |
| 59 | - ga_repo.save | |
| 60 | - end | |
| 61 | - | |
| 62 | - #update or create | |
| 63 | - def update_keys(user, key) | |
| 64 | - File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) } | |
| 65 | - end | |
| 66 | - | |
| 67 | - def delete_key(user) | |
| 68 | - File.unlink(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub")) | |
| 69 | - `cd #{File.join(@local_dir,'gitolite')} ; git rm keydir/#{user}.pub` | |
| 70 | - end | |
| 71 | - | |
| 72 | - # update or create | |
| 73 | - def update_project(repo_name, project) | |
| 74 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 75 | - conf = ga_repo.config | |
| 76 | - repo = update_project_config(project, conf) | |
| 77 | - conf.add_repo(repo, true) | |
| 78 | - | |
| 79 | - ga_repo.save | |
| 80 | - end | |
| 81 | - | |
| 82 | - # Updates many projects and uses project.path as the repo path | |
| 83 | - # An order of magnitude faster than update_project | |
| 84 | - def update_projects(projects) | |
| 85 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 86 | - conf = ga_repo.config | |
| 87 | - | |
| 88 | - projects.each do |project| | |
| 89 | - repo = update_project_config(project, conf) | |
| 90 | - conf.add_repo(repo, true) | |
| 91 | - end | |
| 92 | - | |
| 93 | - ga_repo.save | |
| 94 | - end | |
| 95 | - | |
| 96 | - def update_project_config(project, conf) | |
| 97 | - repo_name = project.path | |
| 98 | - | |
| 99 | - repo = if conf.has_repo?(repo_name) | |
| 100 | - conf.get_repo(repo_name) | |
| 101 | - else | |
| 102 | - ::Gitolite::Config::Repo.new(repo_name) | |
| 103 | - end | |
| 104 | - | |
| 105 | - name_readers = project.repository_readers | |
| 106 | - name_writers = project.repository_writers | |
| 107 | - name_masters = project.repository_masters | |
| 108 | - | |
| 109 | - pr_br = project.protected_branches.map(&:name).join("$ ") | |
| 110 | - | |
| 111 | - repo.clean_permissions | |
| 112 | - | |
| 113 | - # Deny access to protected branches for writers | |
| 114 | - unless name_writers.blank? || pr_br.blank? | |
| 115 | - repo.add_permission("-", pr_br.strip + "$ ", name_writers) | |
| 116 | - end | |
| 117 | - | |
| 118 | - # Add read permissions | |
| 119 | - repo.add_permission("R", "", name_readers) unless name_readers.blank? | |
| 120 | - | |
| 121 | - # Add write permissions | |
| 122 | - repo.add_permission("RW+", "", name_writers) unless name_writers.blank? | |
| 123 | - repo.add_permission("RW+", "", name_masters) unless name_masters.blank? | |
| 124 | - | |
| 125 | - repo | |
| 126 | - end | |
| 127 | - | |
| 128 | - def admin_all_repo | |
| 129 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 130 | - conf = ga_repo.config | |
| 131 | - owner_name = "" | |
| 132 | - | |
| 133 | - # Read gitolite-admin user | |
| 134 | - # | |
| 135 | - begin | |
| 136 | - repo = conf.get_repo("gitolite-admin") | |
| 137 | - owner_name = repo.permissions[0]["RW+"][""][0] | |
| 138 | - raise StandardError if owner_name.blank? | |
| 139 | - rescue => ex | |
| 140 | - puts "Can't determine gitolite-admin owner".red | |
| 141 | - raise StandardError | |
| 142 | - end | |
| 143 | - | |
| 144 | - # @ALL repos premission for gitolite owner | |
| 145 | - repo_name = "@all" | |
| 146 | - repo = if conf.has_repo?(repo_name) | |
| 147 | - conf.get_repo(repo_name) | |
| 148 | - else | |
| 149 | - ::Gitolite::Config::Repo.new(repo_name) | |
| 150 | - end | |
| 151 | - | |
| 152 | - repo.add_permission("RW+", "", owner_name) | |
| 153 | - conf.add_repo(repo, true) | |
| 154 | - ga_repo.save | |
| 155 | - end | |
| 156 | - end | |
| 157 | -end |
lib/tasks/gitlab/enable_automerge.rake
| ... | ... | @@ -2,9 +2,7 @@ namespace :gitlab do |
| 2 | 2 | namespace :app do |
| 3 | 3 | desc "GITLAB | Enable auto merge" |
| 4 | 4 | task :enable_automerge => :environment do |
| 5 | - Gitlab::GitHost.system.new.configure do |git| | |
| 6 | - git.admin_all_repo | |
| 7 | - end | |
| 5 | + Gitlab::Gitolite.new.enable_automerge | |
| 8 | 6 | |
| 9 | 7 | Project.find_each do |project| |
| 10 | 8 | if project.repo_exists? && !project.satellite.exists? | ... | ... |
lib/tasks/gitlab/gitolite_rebuild.rake
| ... | ... | @@ -16,7 +16,7 @@ namespace :gitlab do |
| 16 | 16 | task :update_keys => :environment do |
| 17 | 17 | puts "Starting Key" |
| 18 | 18 | Key.find_each(:batch_size => 100) do |key| |
| 19 | - key.update_repository | |
| 19 | + Gitlab::Gitolite.new.set_key(key.identifier, key.key, key.projects) | |
| 20 | 20 | print '.' |
| 21 | 21 | end |
| 22 | 22 | puts "Done with keys" | ... | ... |
| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe KeyObserver do | |
| 4 | + before do | |
| 5 | + @key = double('Key', | |
| 6 | + identifier: 'admin_654654', | |
| 7 | + key: '== a vaild ssh key', | |
| 8 | + projects: [], | |
| 9 | + is_deploy_key: false | |
| 10 | + ) | |
| 11 | + | |
| 12 | + @gitolite = double('Gitlab::Gitolite', | |
| 13 | + set_key: true, | |
| 14 | + remove_key: true | |
| 15 | + ) | |
| 16 | + | |
| 17 | + @observer = KeyObserver.instance | |
| 18 | + @observer.stub(:git_host => @gitolite) | |
| 19 | + end | |
| 20 | + | |
| 21 | + context :after_save do | |
| 22 | + it do | |
| 23 | + @gitolite.should_receive(:set_key).with(@key.identifier, @key.key, @key.projects) | |
| 24 | + @observer.after_save(@key) | |
| 25 | + end | |
| 26 | + end | |
| 27 | + | |
| 28 | + context :after_destroy do | |
| 29 | + it do | |
| 30 | + @gitolite.should_receive(:remove_key).with(@key.identifier, @key.projects) | |
| 31 | + @observer.after_destroy(@key) | |
| 32 | + end | |
| 33 | + end | |
| 34 | +end | ... | ... |
spec/spec_helper.rb
| ... | ... | @@ -27,6 +27,7 @@ RSpec.configure do |config| |
| 27 | 27 | config.mock_with :rspec |
| 28 | 28 | |
| 29 | 29 | config.include LoginHelpers, type: :request |
| 30 | + config.include GitoliteStub | |
| 30 | 31 | |
| 31 | 32 | # If you're not using ActiveRecord, or you'd prefer not to run each of your |
| 32 | 33 | # examples within a transaction, remove the following line or assign false |
| ... | ... | @@ -39,6 +40,8 @@ RSpec.configure do |config| |
| 39 | 40 | end |
| 40 | 41 | |
| 41 | 42 | config.before do |
| 43 | + stub_gitolite! | |
| 44 | + | |
| 42 | 45 | # !!! Observers disabled by default in tests |
| 43 | 46 | ActiveRecord::Base.observers.disable(:all) |
| 44 | 47 | # ActiveRecord::Base.observers.enable(:all) | ... | ... |
| ... | ... | @@ -0,0 +1,35 @@ |
| 1 | +module GitoliteStub | |
| 2 | + def stub_gitolite! | |
| 3 | + stub_gitlab_gitolite | |
| 4 | + stub_gitolite_admin | |
| 5 | + end | |
| 6 | + | |
| 7 | + def stub_gitolite_admin | |
| 8 | + gitolite_repo = mock( | |
| 9 | + clean_permissions: true, | |
| 10 | + add_permission: true | |
| 11 | + ) | |
| 12 | + | |
| 13 | + gitolite_config = mock( | |
| 14 | + add_repo: true, | |
| 15 | + get_repo: gitolite_repo, | |
| 16 | + has_repo?: true | |
| 17 | + ) | |
| 18 | + | |
| 19 | + gitolite_admin = double( | |
| 20 | + 'Gitolite::GitoliteAdmin', | |
| 21 | + config: gitolite_config, | |
| 22 | + save: true, | |
| 23 | + ) | |
| 24 | + | |
| 25 | + Gitolite::GitoliteAdmin.stub(new: gitolite_admin) | |
| 26 | + | |
| 27 | + end | |
| 28 | + | |
| 29 | + def stub_gitlab_gitolite | |
| 30 | + gitlab_gitolite = Gitlab::Gitolite.new | |
| 31 | + Gitlab::Gitolite.stub(new: gitlab_gitolite) | |
| 32 | + gitlab_gitolite.stub(configure: ->() { yield(self) }) | |
| 33 | + gitlab_gitolite.stub(update_keys: true) | |
| 34 | + end | |
| 35 | +end | ... | ... |
spec/support/monkeypatch.rb
| 1 | 1 | # Stubbing Project <-> git host path |
| 2 | 2 | # create project using Factory only |
| 3 | 3 | class Project |
| 4 | - def update_repository | |
| 5 | - true | |
| 6 | - end | |
| 7 | - | |
| 8 | - def destroy_repository | |
| 9 | - true | |
| 10 | - end | |
| 11 | - | |
| 12 | 4 | def path_to_repo |
| 13 | 5 | File.join(Rails.root, "tmp", "tests", path) |
| 14 | 6 | end |
| ... | ... | @@ -18,22 +10,6 @@ class Project |
| 18 | 10 | end |
| 19 | 11 | end |
| 20 | 12 | |
| 21 | -class Key | |
| 22 | - def update_repository | |
| 23 | - true | |
| 24 | - end | |
| 25 | - | |
| 26 | - def repository_delete_key | |
| 27 | - true | |
| 28 | - end | |
| 29 | -end | |
| 30 | - | |
| 31 | -class UsersProject | |
| 32 | - def update_repository | |
| 33 | - true | |
| 34 | - end | |
| 35 | -end | |
| 36 | - | |
| 37 | 13 | class FakeSatellite |
| 38 | 14 | def exists? |
| 39 | 15 | true |
| ... | ... | @@ -43,9 +19,3 @@ class FakeSatellite |
| 43 | 19 | true |
| 44 | 20 | end |
| 45 | 21 | end |
| 46 | - | |
| 47 | -class ProtectedBranch | |
| 48 | - def update_repository | |
| 49 | - true | |
| 50 | - end | |
| 51 | -end | ... | ... |