annotate .svn/pristine/8f/8f7d415c01cd1aa2a9653282ae2c4dbd9a43ab03.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 #!/usr/bin/env ruby
Chris@1296 2
Chris@1296 3 require 'optparse'
Chris@1296 4 require 'find'
Chris@1296 5 require 'etc'
Chris@1296 6 require 'rubygems'
Chris@1296 7
Chris@1296 8 Version = "1.4"
Chris@1296 9 SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem )
Chris@1296 10
Chris@1296 11 $verbose = 0
Chris@1296 12 $quiet = false
Chris@1296 13 $redmine_host = ''
Chris@1296 14 $repos_base = ''
Chris@1296 15 $svn_owner = 'root'
Chris@1296 16 $svn_group = 'root'
Chris@1296 17 $use_groupid = true
Chris@1296 18 $svn_url = false
Chris@1296 19 $test = false
Chris@1296 20 $force = false
Chris@1296 21 $scm = 'Subversion'
Chris@1296 22
Chris@1296 23 def log(text, options={})
Chris@1296 24 level = options[:level] || 0
Chris@1296 25 puts text unless $quiet or level > $verbose
Chris@1296 26 exit 1 if options[:exit]
Chris@1296 27 end
Chris@1296 28
Chris@1296 29 def system_or_raise(command)
Chris@1296 30 raise "\"#{command}\" failed" unless system command
Chris@1296 31 end
Chris@1296 32
Chris@1296 33 module SCM
Chris@1296 34
Chris@1296 35 module Subversion
Chris@1296 36 def self.create(path)
Chris@1296 37 system_or_raise "svnadmin create #{path}"
Chris@1296 38 end
Chris@1296 39 end
Chris@1296 40
Chris@1296 41 module Git
Chris@1296 42 def self.create(path)
Chris@1296 43 Dir.mkdir path
Chris@1296 44 Dir.chdir(path) do
Chris@1296 45 system_or_raise "git --bare init --shared"
Chris@1296 46 system_or_raise "git update-server-info"
Chris@1296 47 end
Chris@1296 48 end
Chris@1296 49 end
Chris@1296 50
Chris@1296 51 end
Chris@1296 52
Chris@1296 53 def read_key_from_file(filename)
Chris@1296 54 begin
Chris@1296 55 $api_key = File.read(filename).strip
Chris@1296 56 rescue Exception => e
Chris@1296 57 $stderr.puts "Unable to read the key from #{filename}: #{e.message}"
Chris@1296 58 exit 1
Chris@1296 59 end
Chris@1296 60 end
Chris@1296 61
Chris@1296 62 def set_scm(scm)
Chris@1296 63 $scm = scm.capitalize
Chris@1296 64 unless SUPPORTED_SCM.include?($scm)
Chris@1296 65 log("Invalid SCM: #{$scm}\nValid SCM are: #{SUPPORTED_SCM.join(', ')}", :exit => true)
Chris@1296 66 end
Chris@1296 67 end
Chris@1296 68
Chris@1296 69 optparse = OptionParser.new do |opts|
Chris@1296 70 opts.banner = "Usage: reposman.rb [OPTIONS...] -s [DIR] -r [HOST] -k [KEY]"
Chris@1296 71 opts.separator("")
Chris@1296 72 opts.separator("Manages your repositories with Redmine.")
Chris@1296 73 opts.separator("")
Chris@1296 74 opts.separator("Required arguments:")
Chris@1296 75 opts.on("-s", "--svn-dir DIR", "use DIR as base directory for svn repositories") {|v| $repos_base = v}
Chris@1296 76 opts.on("-r", "--redmine-host HOST","assume Redmine is hosted on HOST. Examples:",
Chris@1296 77 " -r redmine.example.net",
Chris@1296 78 " -r http://redmine.example.net",
Chris@1296 79 " -r https://redmine.example.net") {|v| $redmine_host = v}
Chris@1296 80 opts.on("-k", "--key KEY", "use KEY as the Redmine API key",
Chris@1296 81 "(you can use --key-file option as an alternative)") {|v| $api_key = v}
Chris@1296 82 opts.separator("")
Chris@1296 83 opts.separator("Options:")
Chris@1296 84 opts.on("-o", "--owner OWNER", "owner of the repository. using the rails login",
Chris@1296 85 "allows users to browse the repository within",
Chris@1296 86 "Redmine even for private projects. If you want to",
Chris@1296 87 "share repositories through Redmine.pm, you need",
Chris@1296 88 "to use the apache owner.") {|v| $svn_owner = v; $use_groupid = false}
Chris@1296 89 opts.on("-g", "--group GROUP", "group of the repository (default: root)") {|v| $svn_group = v; $use_groupid = false}
Chris@1296 90 opts.on("-u", "--url URL", "the base url Redmine will use to access your",
Chris@1296 91 "repositories. This option is used to register",
Chris@1296 92 "the repositories in Redmine automatically. The",
Chris@1296 93 "project identifier will be appended to this url.",
Chris@1296 94 "Examples:",
Chris@1296 95 " -u https://example.net/svn",
Chris@1296 96 " -u file:///var/svn/",
Chris@1296 97 "if this option isn't set, reposman won't register",
Chris@1296 98 "the repositories in Redmine") {|v| $svn_url = v}
Chris@1296 99 opts.on( "--scm SCM", "the kind of SCM repository you want to create",
Chris@1296 100 "(and register) in Redmine (default: Subversion).",
Chris@1296 101 "reposman is able to create Git and Subversion",
Chris@1296 102 "repositories.",
Chris@1296 103 "For all other kind, you must specify a --command",
Chris@1296 104 "option") {|v| set_scm(v)}
Chris@1296 105 opts.on("-c", "--command COMMAND", "use this command instead of `svnadmin create` to",
Chris@1296 106 "create a repository. This option can be used to",
Chris@1296 107 "create repositories other than subversion and git",
Chris@1296 108 "kind.",
Chris@1296 109 "This command override the default creation for",
Chris@1296 110 "git and subversion.") {|v| $command = v}
Chris@1296 111 opts.on( "--key-file FILE", "path to a file that contains the Redmine API key",
Chris@1296 112 "(use this option instead of --key if you don't",
Chris@1296 113 "want the key to appear in the command line)") {|v| read_key_from_file(v)}
Chris@1296 114 opts.on("-t", "--test", "only show what should be done") {$test = true}
Chris@1296 115 opts.on("-f", "--force", "force repository creation even if the project", "repository is already declared in Redmine") {$force = true}
Chris@1296 116 opts.on("-v", "--verbose", "verbose") {$verbose += 1}
Chris@1296 117 opts.on("-V", "--version", "show version and exit") {puts Version; exit}
Chris@1296 118 opts.on("-h", "--help", "show help and exit") {puts opts; exit 1}
Chris@1296 119 opts.on("-q", "--quiet", "no log") {$quiet = true}
Chris@1296 120 opts.separator("")
Chris@1296 121 opts.separator("Examples:")
Chris@1296 122 opts.separator(" reposman.rb --svn-dir=/var/svn --redmine-host=redmine.host")
Chris@1296 123 opts.separator(" reposman.rb -s /var/git -r redmine.host -u http://git.host --scm git")
Chris@1296 124 opts.separator("")
Chris@1296 125 opts.separator("You can find more information on the redmine's wiki:\nhttp://www.redmine.org/projects/redmine/wiki/HowTos")
Chris@1296 126
Chris@1296 127 opts.summary_width = 25
Chris@1296 128 end
Chris@1296 129 optparse.parse!
Chris@1296 130
Chris@1296 131 if $test
Chris@1296 132 log("running in test mode")
Chris@1296 133 end
Chris@1296 134
Chris@1296 135 # Make sure command is overridden if SCM vendor is not handled internally (for the moment Subversion and Git)
Chris@1296 136 if $command.nil?
Chris@1296 137 begin
Chris@1296 138 scm_module = SCM.const_get($scm)
Chris@1296 139 rescue
Chris@1296 140 log("Please use --command option to specify how to create a #{$scm} repository.", :exit => true)
Chris@1296 141 end
Chris@1296 142 end
Chris@1296 143
Chris@1296 144 $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/)
Chris@1296 145
Chris@1296 146 if ($redmine_host.empty? or $repos_base.empty?)
Chris@1296 147 puts "Some arguments are missing. Use reposman.rb --help for getting help."
Chris@1296 148 exit 1
Chris@1296 149 end
Chris@1296 150
Chris@1296 151 unless File.directory?($repos_base)
Chris@1296 152 log("directory '#{$repos_base}' doesn't exists", :exit => true)
Chris@1296 153 end
Chris@1296 154
Chris@1296 155 begin
Chris@1296 156 require 'active_resource'
Chris@1296 157 rescue LoadError
Chris@1296 158 log("This script requires activeresource.\nRun 'gem install activeresource' to install it.", :exit => true)
Chris@1296 159 end
Chris@1296 160
Chris@1296 161 class Project < ActiveResource::Base
Chris@1296 162 self.headers["User-agent"] = "Redmine repository manager/#{Version}"
Chris@1296 163 self.format = :xml
Chris@1296 164 end
Chris@1296 165
Chris@1296 166 log("querying Redmine for projects...", :level => 1);
Chris@1296 167
Chris@1296 168 $redmine_host.gsub!(/^/, "http://") unless $redmine_host.match("^https?://")
Chris@1296 169 $redmine_host.gsub!(/\/$/, '')
Chris@1296 170
Chris@1296 171 Project.site = "#{$redmine_host}/sys";
Chris@1296 172
Chris@1296 173 begin
Chris@1296 174 # Get all active projects that have the Repository module enabled
Chris@1296 175 projects = Project.find(:all, :params => {:key => $api_key})
Chris@1296 176 rescue ActiveResource::ForbiddenAccess
Chris@1296 177 log("Request was denied by your Redmine server. Make sure that 'WS for repository management' is enabled in application settings and that you provided the correct API key.")
Chris@1296 178 rescue => e
Chris@1296 179 log("Unable to connect to #{Project.site}: #{e}", :exit => true)
Chris@1296 180 end
Chris@1296 181
Chris@1296 182 if projects.nil?
Chris@1296 183 log('No project found, perhaps you forgot to "Enable WS for repository management"', :exit => true)
Chris@1296 184 end
Chris@1296 185
Chris@1296 186 log("retrieved #{projects.size} projects", :level => 1)
Chris@1296 187
Chris@1296 188 def set_owner_and_rights(project, repos_path, &block)
Chris@1296 189 if mswin?
Chris@1296 190 yield if block_given?
Chris@1296 191 else
Chris@1296 192 uid, gid = Etc.getpwnam($svn_owner).uid, ($use_groupid ? Etc.getgrnam(project.identifier).gid : Etc.getgrnam($svn_group).gid)
Chris@1296 193 right = project.is_public ? 0775 : 0770
Chris@1296 194 yield if block_given?
Chris@1296 195 Find.find(repos_path) do |f|
Chris@1296 196 File.chmod right, f
Chris@1296 197 File.chown uid, gid, f
Chris@1296 198 end
Chris@1296 199 end
Chris@1296 200 end
Chris@1296 201
Chris@1296 202 def other_read_right?(file)
Chris@1296 203 (File.stat(file).mode & 0007).zero? ? false : true
Chris@1296 204 end
Chris@1296 205
Chris@1296 206 def owner_name(file)
Chris@1296 207 mswin? ?
Chris@1296 208 $svn_owner :
Chris@1296 209 Etc.getpwuid( File.stat(file).uid ).name
Chris@1296 210 end
Chris@1296 211
Chris@1296 212 def mswin?
Chris@1296 213 (RUBY_PLATFORM =~ /(:?mswin|mingw)/) || (RUBY_PLATFORM == 'java' && (ENV['OS'] || ENV['os']) =~ /windows/i)
Chris@1296 214 end
Chris@1296 215
Chris@1296 216 projects.each do |project|
Chris@1296 217 log("treating project #{project.name}", :level => 1)
Chris@1296 218
Chris@1296 219 if project.identifier.empty?
Chris@1296 220 log("\tno identifier for project #{project.name}")
Chris@1296 221 next
Chris@1296 222 elsif not project.identifier.match(/^[a-z0-9\-_]+$/)
Chris@1296 223 log("\tinvalid identifier for project #{project.name} : #{project.identifier}");
Chris@1296 224 next;
Chris@1296 225 end
Chris@1296 226
Chris@1296 227 repos_path = File.join($repos_base, project.identifier).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
Chris@1296 228
Chris@1296 229 if File.directory?(repos_path)
Chris@1296 230 # we must verify that repository has the good owner and the good
Chris@1296 231 # rights before leaving
Chris@1296 232 other_read = other_read_right?(repos_path)
Chris@1296 233 owner = owner_name(repos_path)
Chris@1296 234 next if project.is_public == other_read and owner == $svn_owner
Chris@1296 235
Chris@1296 236 if $test
Chris@1296 237 log("\tchange mode on #{repos_path}")
Chris@1296 238 next
Chris@1296 239 end
Chris@1296 240
Chris@1296 241 begin
Chris@1296 242 set_owner_and_rights(project, repos_path)
Chris@1296 243 rescue Errno::EPERM => e
Chris@1296 244 log("\tunable to change mode on #{repos_path} : #{e}\n")
Chris@1296 245 next
Chris@1296 246 end
Chris@1296 247
Chris@1296 248 log("\tmode change on #{repos_path}");
Chris@1296 249
Chris@1296 250 else
Chris@1296 251 # if repository is already declared in redmine, we don't create
Chris@1296 252 # unless user use -f with reposman
Chris@1296 253 if $force == false and project.respond_to?(:repository)
Chris@1296 254 log("\trepository for project #{project.identifier} already exists in Redmine", :level => 1)
Chris@1296 255 next
Chris@1296 256 end
Chris@1296 257
Chris@1296 258 project.is_public ? File.umask(0002) : File.umask(0007)
Chris@1296 259
Chris@1296 260 if $test
Chris@1296 261 log("\tcreate repository #{repos_path}")
Chris@1296 262 log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}") if $svn_url;
Chris@1296 263 next
Chris@1296 264 end
Chris@1296 265
Chris@1296 266 begin
Chris@1296 267 set_owner_and_rights(project, repos_path) do
Chris@1296 268 if scm_module.nil?
Chris@1296 269 system_or_raise "#{$command} #{repos_path}"
Chris@1296 270 else
Chris@1296 271 scm_module.create(repos_path)
Chris@1296 272 end
Chris@1296 273 end
Chris@1296 274 rescue => e
Chris@1296 275 log("\tunable to create #{repos_path} : #{e}\n")
Chris@1296 276 next
Chris@1296 277 end
Chris@1296 278
Chris@1296 279 if $svn_url
Chris@1296 280 begin
Chris@1296 281 project.post(:repository, :vendor => $scm, :repository => {:url => "#{$svn_url}#{project.identifier}"}, :key => $api_key)
Chris@1296 282 log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}");
Chris@1296 283 rescue => e
Chris@1296 284 log("\trepository #{repos_path} not registered in Redmine: #{e.message}");
Chris@1296 285 end
Chris@1296 286 end
Chris@1296 287 log("\trepository #{repos_path} created");
Chris@1296 288 end
Chris@1296 289 end