|
| 1 | +module RedmineGitHosting |
| 2 | + module GitoliteWrapper |
| 3 | + class Global < Admin |
| 4 | + |
| 5 | + def enable_readme_creation |
| 6 | + if all_repository_config.nil? |
| 7 | + admin.transaction do |
| 8 | + gitolite_config.add_repo(create_readme_config) |
| 9 | + gitolite_admin_repo_commit("Enable README file creation for repositories") |
| 10 | + end |
| 11 | + else |
| 12 | + logger.info("'@all' repository already configured, check for RedmineGitHosting key presence") |
| 13 | + admin.transaction do |
| 14 | + add_redmine_key |
| 15 | + gitolite_admin_repo_commit("Enable README file creation for repositories") |
| 16 | + end |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + |
| 21 | + def disable_readme_creation |
| 22 | + if all_repository_config.nil? |
| 23 | + logger.info('README file creation already disabled.') |
| 24 | + return |
| 25 | + else |
| 26 | + admin.transaction do |
| 27 | + remove_redmine_key |
| 28 | + gitolite_admin_repo_commit("Disable README file creation for repositories") |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + |
| 34 | + private |
| 35 | + |
| 36 | + |
| 37 | + def redmine_gitolite_key |
| 38 | + 'redmine_gitolite_admin_id_rsa' |
| 39 | + end |
| 40 | + |
| 41 | + |
| 42 | + def all_repository |
| 43 | + '@all' |
| 44 | + end |
| 45 | + |
| 46 | + |
| 47 | + def all_repository_config |
| 48 | + gitolite_config.repos[all_repository] |
| 49 | + end |
| 50 | + |
| 51 | + |
| 52 | + def create_readme_config |
| 53 | + repo_conf = ::Gitolite::Config::Repo.new(all_repository) |
| 54 | + repo_conf.permissions = create_readme_perms |
| 55 | + repo_conf |
| 56 | + end |
| 57 | + |
| 58 | + |
| 59 | + def create_readme_perms |
| 60 | + permissions = {} |
| 61 | + permissions['RW+'] = {} |
| 62 | + permissions['RW+'][''] = [redmine_gitolite_key] |
| 63 | + [permissions] |
| 64 | + end |
| 65 | + |
| 66 | + |
| 67 | + def remove_redmine_key |
| 68 | + repo_conf = all_repository_config |
| 69 | + |
| 70 | + # RedmineGitHosting key must be in RW+ group |
| 71 | + perms = repo_conf.permissions.select{ |p| p.has_key? 'RW+' } |
| 72 | + |
| 73 | + # RedmineGitHosting key must be in [RW+][''] group |
| 74 | + # Return if those groups are absent : it means that our key is not here |
| 75 | + return if perms.empty? || !perms[0]['RW+'].include?('') |
| 76 | + |
| 77 | + # Check for key presence |
| 78 | + users = perms[0]['RW+'][''] |
| 79 | + return if !users.include?(redmine_gitolite_key) |
| 80 | + |
| 81 | + # Delete the key |
| 82 | + repo_conf.permissions[0]['RW+'][''].delete(redmine_gitolite_key) |
| 83 | + |
| 84 | + # We cannot remove this repository as it may contains other configuration that we didn't check. |
| 85 | + # Instead add a dummy key so the repo_conf is still valid for Gitolite |
| 86 | + # RW+ = <empty string> is not valid |
| 87 | + repo_conf.permissions[0]['RW+'][''].push('DUMMY_REDMINE_KEY') if repo_conf.permissions[0]['RW+'][''].empty? |
| 88 | + end |
| 89 | + |
| 90 | + |
| 91 | + def add_redmine_key |
| 92 | + repo_conf = all_repository_config |
| 93 | + |
| 94 | + # RedmineGitHosting key must be in RW+ group |
| 95 | + perms = repo_conf.permissions.select{ |p| p.has_key? 'RW+' } |
| 96 | + |
| 97 | + # If not create the RW+ group and add the key |
| 98 | + if perms.empty? |
| 99 | + logger.info("No permissions set for '@all' repository, add RedmineGitHosting key") |
| 100 | + repo_conf.permissions = create_readme_perms |
| 101 | + return |
| 102 | + end |
| 103 | + |
| 104 | + # RedmineGitHosting key can act on any refspec ('') so it should be it that 'subgroup' |
| 105 | + users = perms[0]['RW+'][''] |
| 106 | + |
| 107 | + # If not create if |
| 108 | + if users.nil? |
| 109 | + logger.info("RedmineGitHosting key is not present, add it !") |
| 110 | + repo_conf.permissions[0]['RW+'][''] = [redmine_gitolite_key] |
| 111 | + # Check that the 'subgroup' contains our key |
| 112 | + elsif !users.include?(redmine_gitolite_key) |
| 113 | + logger.info("RedmineGitHosting key is not present, add it !") |
| 114 | + repo_conf.permissions[0]['RW+'][''].push(redmine_gitolite_key) |
| 115 | + # Our key already here, nothing to do |
| 116 | + else |
| 117 | + logger.info("RedmineGitHosting key is present, nothing to do.") |
| 118 | + end |
| 119 | + |
| 120 | + # Delete DUMMY_REDMINE_KEY if present |
| 121 | + repo_conf.permissions[0]['RW+'][''].delete('DUMMY_REDMINE_KEY') if repo_conf.permissions[0]['RW+'][''].include?('DUMMY_REDMINE_KEY') |
| 122 | + end |
| 123 | + |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments