Mercurial > hg > soundsoftware-site
comparison extra/mail_handler/rdm-mailhandler.rb @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
21 end | 21 end |
22 end | 22 end |
23 end | 23 end |
24 | 24 |
25 class RedmineMailHandler | 25 class RedmineMailHandler |
26 VERSION = '0.2.1' | 26 VERSION = '0.2.3' |
27 | 27 |
28 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate | 28 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :default_group, :no_permission_check, |
29 :url, :key, :no_check_certificate, :no_account_notice, :no_notification | |
29 | 30 |
30 def initialize | 31 def initialize |
31 self.issue_attributes = {} | 32 self.issue_attributes = {} |
32 | 33 |
33 optparse = OptionParser.new do |opts| | 34 optparse = OptionParser.new do |opts| |
38 opts.separator("Required arguments:") | 39 opts.separator("Required arguments:") |
39 opts.on("-u", "--url URL", "URL of the Redmine server") {|v| self.url = v} | 40 opts.on("-u", "--url URL", "URL of the Redmine server") {|v| self.url = v} |
40 opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v} | 41 opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v} |
41 opts.separator("") | 42 opts.separator("") |
42 opts.separator("General options:") | 43 opts.separator("General options:") |
43 opts.on("--unknown-user ACTION", "how to handle emails from an unknown user", | |
44 "ACTION can be one of the following values:", | |
45 "* ignore: email is ignored (default)", | |
46 "* accept: accept as anonymous user", | |
47 "* create: create a user account") {|v| self.unknown_user = v} | |
48 opts.on("--no-permission-check", "disable permission checking when receiving", | 44 opts.on("--no-permission-check", "disable permission checking when receiving", |
49 "the email") {self.no_permission_check = '1'} | 45 "the email") {self.no_permission_check = '1'} |
50 opts.on("--key-file FILE", "path to a file that contains the Redmine", | 46 opts.on("--key-file FILE", "path to a file that contains the Redmine", |
51 "API key (use this option instead of --key", | 47 "API key (use this option instead of --key", |
52 "if you don't the key to appear in the", | 48 "if you don't the key to appear in the", |
53 "command line)") {|v| read_key_from_file(v)} | 49 "command line)") {|v| read_key_from_file(v)} |
54 opts.on("--no-check-certificate", "do not check server certificate") {self.no_check_certificate = true} | 50 opts.on("--no-check-certificate", "do not check server certificate") {self.no_check_certificate = true} |
55 opts.on("-h", "--help", "show this help") {puts opts; exit 1} | 51 opts.on("-h", "--help", "show this help") {puts opts; exit 1} |
56 opts.on("-v", "--verbose", "show extra information") {self.verbose = true} | 52 opts.on("-v", "--verbose", "show extra information") {self.verbose = true} |
57 opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit} | 53 opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit} |
54 opts.separator("") | |
55 opts.separator("User creation options:") | |
56 opts.on("--unknown-user ACTION", "how to handle emails from an unknown user", | |
57 "ACTION can be one of the following values:", | |
58 "* ignore: email is ignored (default)", | |
59 "* accept: accept as anonymous user", | |
60 "* create: create a user account") {|v| self.unknown_user = v} | |
61 opts.on("--default-group GROUP", "add created user to GROUP (none by default)", | |
62 "GROUP can be a comma separated list of groups") { |v| self.default_group = v} | |
63 opts.on("--no-account-notice", "don't send account information to the newly", | |
64 "created user") { |v| self.no_account_notice = '1'} | |
65 opts.on("--no-notification", "disable email notifications for the created", | |
66 "user") { |v| self.no_notification = '1'} | |
58 opts.separator("") | 67 opts.separator("") |
59 opts.separator("Issue attributes control options:") | 68 opts.separator("Issue attributes control options:") |
60 opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v} | 69 opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v} |
61 opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v} | 70 opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v} |
62 opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v} | 71 opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v} |
93 headers = { 'User-Agent' => "Redmine mail handler/#{VERSION}" } | 102 headers = { 'User-Agent' => "Redmine mail handler/#{VERSION}" } |
94 | 103 |
95 data = { 'key' => key, 'email' => email, | 104 data = { 'key' => key, 'email' => email, |
96 'allow_override' => allow_override, | 105 'allow_override' => allow_override, |
97 'unknown_user' => unknown_user, | 106 'unknown_user' => unknown_user, |
107 'default_group' => default_group, | |
108 'no_account_notice' => no_account_notice, | |
109 'no_notification' => no_notification, | |
98 'no_permission_check' => no_permission_check} | 110 'no_permission_check' => no_permission_check} |
99 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value } | 111 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value } |
100 | 112 |
101 debug "Posting to #{uri}..." | 113 debug "Posting to #{uri}..." |
102 response = Net::HTTPS.post_form(URI.parse(uri), data, headers, :no_check_certificate => no_check_certificate) | 114 begin |
115 response = Net::HTTPS.post_form(URI.parse(uri), data, headers, :no_check_certificate => no_check_certificate) | |
116 rescue SystemCallError => e # connection refused, etc. | |
117 warn "An error occured while contacting your Redmine server: #{e.message}" | |
118 return 75 # temporary failure | |
119 end | |
103 debug "Response received: #{response.code}" | 120 debug "Response received: #{response.code}" |
104 | 121 |
105 case response.code.to_i | 122 case response.code.to_i |
106 when 403 | 123 when 403 |
107 warn "Request was denied by your Redmine server. " + | 124 warn "Request was denied by your Redmine server. " + |