From da293fdfd8d0c72e73a9a9c27d5a29884f4e495b Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 12 May 2012 12:59:23 +0000 Subject: [PATCH] Merged #10837. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/1.4-stable@9676 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- extra/mail_handler/rdm-mailhandler.rb | 170 +++++++++-------------- extra/svn/reposman.rb | 193 +++++++++++--------------- 2 files changed, 143 insertions(+), 220 deletions(-) diff --git a/extra/mail_handler/rdm-mailhandler.rb b/extra/mail_handler/rdm-mailhandler.rb index 774eb9d9c..42622e708 100644 --- a/extra/mail_handler/rdm-mailhandler.rb +++ b/extra/mail_handler/rdm-mailhandler.rb @@ -1,64 +1,9 @@ #!/usr/bin/env ruby -# == Synopsis -# -# Reads an email from standard input and forward it to a Redmine server -# through a HTTP request. -# -# == Usage -# -# rdm-mailhandler [options] --url= --key= -# -# == Arguments -# -# -u, --url URL of the Redmine server -# -k, --key Redmine API key -# -# General options: -# --unknown-user=ACTION how to handle emails from an unknown user -# ACTION can be one of the following values: -# ignore: email is ignored (default) -# accept: accept as anonymous user -# create: create a user account -# --no-permission-check disable permission checking when receiving -# the email -# --key-file=PATH path to a file that contains the Redmine -# API key (use this option instead of --key -# if you don't the key to appear in the -# command line) -# --no-check-certificate do not check server certificate -# -h, --help show this help -# -v, --verbose show extra information -# -V, --version show version information and exit -# -# Issue attributes control options: -# -p, --project=PROJECT identifier of the target project -# -s, --status=STATUS name of the target status -# -t, --tracker=TRACKER name of the target tracker -# --category=CATEGORY name of the target category -# --priority=PRIORITY name of the target priority -# -o, --allow-override=ATTRS allow email content to override attributes -# specified by previous options -# ATTRS is a comma separated list of attributes -# -# == Examples -# No project specified. Emails MUST contain the 'Project' keyword: -# -# rdm-mailhandler --url http://redmine.domain.foo --key secret -# -# Fixed project and default tracker specified, but emails can override -# both tracker and priority attributes using keywords: -# -# rdm-mailhandler --url https://domain.foo/redmine --key secret \\ -# --project foo \\ -# --tracker bug \\ -# --allow-override tracker,priority - require 'net/http' require 'net/https' require 'uri' -require 'getoptlong' -require 'rdoc/usage' +require 'optparse' module Net class HTTPS < HTTP @@ -78,64 +23,68 @@ module Net end class RedmineMailHandler - VERSION = '0.1' + VERSION = '0.2' attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate def initialize self.issue_attributes = {} - opts = GetoptLong.new( - [ '--help', '-h', GetoptLong::NO_ARGUMENT ], - [ '--version', '-V', GetoptLong::NO_ARGUMENT ], - [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], - [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ], - [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT], - [ '--key-file', GetoptLong::REQUIRED_ARGUMENT], - [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ], - [ '--status', '-s', GetoptLong::REQUIRED_ARGUMENT ], - [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT], - [ '--category', GetoptLong::REQUIRED_ARGUMENT], - [ '--priority', GetoptLong::REQUIRED_ARGUMENT], - [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT], - [ '--unknown-user', GetoptLong::REQUIRED_ARGUMENT], - [ '--no-permission-check', GetoptLong::NO_ARGUMENT], - [ '--no-check-certificate', GetoptLong::NO_ARGUMENT] - ) - - opts.each do |opt, arg| - case opt - when '--url' - self.url = arg.dup - when '--key' - self.key = arg.dup - when '--key-file' - begin - self.key = File.read(arg).strip - rescue Exception => e - $stderr.puts "Unable to read the key from #{arg}: #{e.message}" - exit 1 - end - when '--help' - usage - when '--verbose' - self.verbose = true - when '--version' - puts VERSION; exit - when '--project', '--status', '--tracker', '--category', '--priority' - self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup - when '--allow-override' - self.allow_override = arg.dup - when '--unknown-user' - self.unknown_user = arg.dup - when '--no-permission-check' - self.no_permission_check = '1' - when '--no-check-certificate' - self.no_check_certificate = true - end + optparse = OptionParser.new do |opts| + opts.banner = "Usage: rdm-mailhandler.rb [options] --url= --key=" + opts.separator("") + opts.separator("Reads an email from standard input and forward it to a Redmine server through a HTTP request.") + opts.separator("") + opts.separator("Required arguments:") + opts.on("-u", "--url URL", "URL of the Redmine server") {|v| self.url = v} + opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v} + opts.separator("") + opts.separator("General options:") + opts.on("--unknown-user ACTION", "how to handle emails from an unknown user", + "ACTION can be one of the following values:", + "* ignore: email is ignored (default)", + "* accept: accept as anonymous user", + "* create: create a user account") {|v| self.unknown_user = v} + opts.on("--no-permission-check", "disable permission checking when receiving", + "the email") {self.no_permission_check = '1'} + opts.on("--key-file FILE", "path to a file that contains the Redmine", + "API key (use this option instead of --key", + "if you don't the key to appear in the", + "command line)") {|v| read_key_from_file(v)} + opts.on("--no-check-certificate", "do not check server certificate") {self.no_check_certificate = true} + opts.on("-h", "--help", "show this help") {puts opts; exit 1} + opts.on("-v", "--verbose", "show extra information") {self.verbose = true} + opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit} + opts.separator("") + opts.separator("Issue attributes control options:") + opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v} + opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v} + opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v} + opts.on( "--category CATEGORY", "name of the target category") {|v| self.issue_attributes['category'] = v} + opts.on( "--priority PRIORITY", "name of the target priority") {|v| self.issue_attributes['priority'] = v} + opts.on("-o", "--allow-override ATTRS", "allow email content to override attributes", + "specified by previous options", + "ATTRS is a comma separated list of attributes") {|v| self.allow_override = v} + opts.separator("") + opts.separator("Examples:") + opts.separator("No project specified. Emails MUST contain the 'Project' keyword:") + opts.separator(" rdm-mailhandler.rb --url http://redmine.domain.foo --key secret") + opts.separator("") + opts.separator("Fixed project and default tracker specified, but emails can override") + opts.separator("both tracker and priority attributes using keywords:") + opts.separator(" rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\") + opts.separator(" --project foo \\") + opts.separator(" --tracker bug \\") + opts.separator(" --allow-override tracker,priority") + + opts.summary_width = 27 end + optparse.parse! - RDoc.usage if url.nil? + unless url && key + puts "Some arguments are missing. Use `rdm-mailhandler.rb --help` for getting help." + exit 1 + end end def submit(email) @@ -181,6 +130,15 @@ class RedmineMailHandler def debug(msg) puts msg if verbose end + + def read_key_from_file(filename) + begin + self.key = File.read(filename).strip + rescue Exception => e + $stderr.puts "Unable to read the key from #{filename}:\n#{e.message}" + exit 1 + end + end end handler = RedmineMailHandler.new diff --git a/extra/svn/reposman.rb b/extra/svn/reposman.rb index 3eae905a8..46b9adc19 100755 --- a/extra/svn/reposman.rb +++ b/extra/svn/reposman.rb @@ -1,96 +1,12 @@ #!/usr/bin/env ruby -# == Synopsis -# -# reposman: manages your repositories with Redmine -# -# == Usage -# -# reposman [OPTIONS...] -s [DIR] -r [HOST] -# -# Examples: -# reposman --svn-dir=/var/svn --redmine-host=redmine.example.net --scm subversion -# reposman -s /var/git -r redmine.example.net -u http://svn.example.net --scm git -# -# == Arguments (mandatory) -# -# -s, --svn-dir=DIR use DIR as base directory for svn repositories -# -r, --redmine-host=HOST assume Redmine is hosted on HOST. Examples: -# -r redmine.example.net -# -r http://redmine.example.net -# -r https://example.net/redmine -# -k, --key=KEY use KEY as the Redmine API key (you can use the -# --key-file option as an alternative) -# -# == Options -# -# -o, --owner=OWNER owner of the repository. using the rails login -# allow user to browse the repository within -# Redmine even for private project. If you want to -# share repositories through Redmine.pm, you need -# to use the apache owner. -# -g, --group=GROUP group of the repository. (default: root) -# --scm=SCM the kind of SCM repository you want to create (and -# register) in Redmine (default: Subversion). -# reposman is able to create Git and Subversion -# repositories. For all other kind, you must specify -# a --command option -# -u, --url=URL the base url Redmine will use to access your -# repositories. This option is used to automatically -# register the repositories in Redmine. The project -# identifier will be appended to this url. Examples: -# -u https://example.net/svn -# -u file:///var/svn/ -# if this option isn't set, reposman won't register -# the repositories in Redmine -# -c, --command=COMMAND use this command instead of "svnadmin create" to -# create a repository. This option can be used to -# create repositories other than subversion and git -# kind. -# This command override the default creation for git -# and subversion. -# -f, --force force repository creation even if the project -# repository is already declared in Redmine -# --key-file=PATH path to a file that contains the Redmine API key -# (use this option instead of --key if you don't -# the key to appear in the command line) -# -t, --test only show what should be done -# -h, --help show help and exit -# -v, --verbose verbose -# -V, --version print version and exit -# -q, --quiet no log -# -# == References -# -# You can find more information on the redmine's wiki : http://www.redmine.org/wiki/redmine/HowTos - - -require 'getoptlong' -require 'rdoc/usage' +require 'optparse' require 'find' require 'etc' -Version = "1.3" +Version = "1.4" SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem ) -opts = GetoptLong.new( - ['--svn-dir', '-s', GetoptLong::REQUIRED_ARGUMENT], - ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT], - ['--key', '-k', GetoptLong::REQUIRED_ARGUMENT], - ['--key-file', GetoptLong::REQUIRED_ARGUMENT], - ['--owner', '-o', GetoptLong::REQUIRED_ARGUMENT], - ['--group', '-g', GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-u', GetoptLong::REQUIRED_ARGUMENT], - ['--command' , '-c', GetoptLong::REQUIRED_ARGUMENT], - ['--scm', GetoptLong::REQUIRED_ARGUMENT], - ['--test', '-t', GetoptLong::NO_ARGUMENT], - ['--force', '-f', GetoptLong::NO_ARGUMENT], - ['--verbose', '-v', GetoptLong::NO_ARGUMENT], - ['--version', '-V', GetoptLong::NO_ARGUMENT], - ['--help' , '-h', GetoptLong::NO_ARGUMENT], - ['--quiet' , '-q', GetoptLong::NO_ARGUMENT] - ) - $verbose = 0 $quiet = false $redmine_host = '' @@ -133,36 +49,84 @@ module SCM end -begin - opts.each do |opt, arg| - case opt - when '--svn-dir'; $repos_base = arg.dup - when '--redmine-host'; $redmine_host = arg.dup - when '--key'; $api_key = arg.dup - when '--key-file' - begin - $api_key = File.read(arg).strip - rescue Exception => e - $stderr.puts "Unable to read the key from #{arg}: #{e.message}" - exit 1 - end - when '--owner'; $svn_owner = arg.dup; $use_groupid = false; - when '--group'; $svn_group = arg.dup; $use_groupid = false; - when '--url'; $svn_url = arg.dup - when '--scm'; $scm = arg.dup.capitalize; log("Invalid SCM: #{$scm}", :exit => true) unless SUPPORTED_SCM.include?($scm) - when '--command'; $command = arg.dup - when '--verbose'; $verbose += 1 - when '--test'; $test = true - when '--force'; $force = true - when '--version'; puts Version; exit - when '--help'; RDoc::usage - when '--quiet'; $quiet = true - end +def read_key_from_file(filename) + begin + $api_key = File.read(filename).strip + rescue Exception => e + $stderr.puts "Unable to read the key from #{filename}: #{e.message}" + exit 1 end -rescue - exit 1 end +def set_scm(scm) + $scm = scm.capitalize + unless SUPPORTED_SCM.include?($scm) + log("Invalid SCM: #{$scm}\nValid SCM are: #{SUPPORTED_SCM.join(', ')}", :exit => true) + end +end + +optparse = OptionParser.new do |opts| + opts.banner = "Usage: reposman.rb [OPTIONS...] -s [DIR] -r [HOST] -k [KEY]" + opts.separator("") + opts.separator("Manages your repositories with Redmine.") + opts.separator("") + opts.separator("Required arguments:") + opts.on("-s", "--svn-dir DIR", "use DIR as base directory for svn repositories") {|v| $repos_base = v} + opts.on("-r", "--redmine-host HOST","assume Redmine is hosted on HOST. Examples:", + " -r redmine.example.net", + " -r http://redmine.example.net", + " -r https://redmine.example.net") {|v| $redmine_host = v} + opts.on("-k", "--key KEY", "use KEY as the Redmine API key", + "(you can use --key-file option as an alternative)") {|v| $api_key = v} + opts.separator("") + opts.separator("Options:") + opts.on("-o", "--owner OWNER", "owner of the repository. using the rails login", + "allows users to browse the repository within", + "Redmine even for private projects. If you want to", + "share repositories through Redmine.pm, you need", + "to use the apache owner.") {|v| $svn_owner = v; $use_groupid = false} + opts.on("-g", "--group GROUP", "group of the repository (default: root)") {|v| $svn_group = v; $use_groupid = false} + opts.on("-u", "--url URL", "the base url Redmine will use to access your", + "repositories. This option is used to register", + "the repositories in Redmine automatically. The", + "project identifier will be appended to this url.", + "Examples:", + " -u https://example.net/svn", + " -u file:///var/svn/", + "if this option isn't set, reposman won't register", + "the repositories in Redmine") {|v| $svn_url = v} + opts.on( "--scm SCM", "the kind of SCM repository you want to create", + "(and register) in Redmine (default: Subversion).", + "reposman is able to create Git and Subversion", + "repositories.", + "For all other kind, you must specify a --command", + "option") {|v| set_scm(v)} + opts.on("-c", "--command COMMAND", "use this command instead of `svnadmin create` to", + "create a repository. This option can be used to", + "create repositories other than subversion and git", + "kind.", + "This command override the default creation for", + "git and subversion.") {|v| $command = v} + opts.on( "--key-file FILE", "path to a file that contains the Redmine API key", + "(use this option instead of --key if you don't", + "want the key to appear in the command line)") {|v| read_key_from_file(v)} + opts.on("-t", "--test", "only show what should be done") {$test = true} + opts.on("-f", "--force", "force repository creation even if the project", "repository is already declared in Redmine") {$force = true} + opts.on("-v", "--verbose", "verbose") {$verbose += 1} + opts.on("-V", "--version", "show version and exit") {puts Version; exit} + opts.on("-h", "--help", "show help and exit") {puts opts; exit 1} + opts.on("-q", "--quiet", "no log") {$quiet = true} + opts.separator("") + opts.separator("Examples:") + opts.separator(" reposman.rb --svn-dir=/var/svn --redmine-host=redmine.host") + opts.separator(" reposman.rb -s /var/git -r redmine.host -u http://git.host --scm git") + opts.separator("") + opts.separator("You can find more information on the redmine's wiki:\nhttp://www.redmine.org/projects/redmine/wiki/HowTos") + + opts.summary_width = 25 +end +optparse.parse! + if $test log("running in test mode") end @@ -179,7 +143,8 @@ end $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/) if ($redmine_host.empty? or $repos_base.empty?) - RDoc::usage + puts "Some arguments are missing. Use reposman.rb --help for getting help." + exit 1 end unless File.directory?($repos_base) -- 2.39.5