Module: Jirarest2Bin
- Defined in:
- lib/jirarest2bin.rb
Overview
Module to keep some methods most executable scripts need
Class Method Summary collapse
-
.check_ruby_version ⇒ Object
Checks for the minimum jira version (1.9.1).
-
.command(scriptopts, connection, command, *args) ⇒ Connection, Object
Execute a command to the server This version is safe if the credentials are wrong.
-
.defaultoptions(opts, scriptopts) ⇒ Openstruct
This method is here because I am to lazy to rewrite the options every time use it with scriptopts = Jirarest2Bin::defaultoptions(opts,scriptopts) fills out the part of the options we output every time.
-
.get_connection(scriptopts, connection, reconnect = false) ⇒ Connection
If there is already a connection known returns that connection.
-
.get_credentials(scriptopts) ⇒ Credentials
Gather all the credentials and build the credentials file.
-
.get_password(username) ⇒ String
Get the password from an interactive shell.
-
.required_argument(name) ⇒ Object
Output a message if a required argument is not set.
-
.write_configfile(scriptopts) ⇒ Object
write the config file for the conenction to jira.
Class Method Details
.check_ruby_version ⇒ Object
Checks for the minimum jira version (1.9.1)
29 30 31 32 33 34 |
# File 'lib/jirarest2bin.rb', line 29 def self.check_ruby_version if RUBY_VERSION < "1.9" puts "Sorry, I need ruby 1.9.1 or higher!" exit 1 end end |
.command(scriptopts, connection, command, *args) ⇒ Connection, Object
Execute a command to the server This version is safe if the credentials are wrong. Extend the case if more requests should be safeguarded.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/jirarest2bin.rb', line 164 def self.command(scriptopts,connection,command,*args) begin if connection.nil? then connection = get_connection(scriptopts,connection) end case command when :issue return connection, NewIssue.new(args[0],args[1],connection) when :connection return connection end rescue Jirarest2::PasswordAuthenticationError => e scriptopts.pass = Jirarest2Bin.get_password(scriptopts.username) retry rescue Jirarest2::CookieAuthenticationError => e connection.credentials.login(scriptopts.username,scriptopts.pass) retry rescue Jirarest2::AuthenticationCaptchaError => e puts "Wrong Password too many times.\nCaptcha time at #{e.to_s} to reenable your account." exit 1 end end |
.defaultoptions(opts, scriptopts) ⇒ Openstruct
This method is here because I am to lazy to rewrite the options every time use it with scriptopts = Jirarest2Bin::defaultoptions(opts,scriptopts) fills out the part of the options we output every time
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/jirarest2bin.rb', line 194 def self.(opts,scriptopts) #header opts.separator "" #tail opts.on_tail("--config-file CONFIGFILE", "Config file containing the jira credentials. (Default: ~/.jiraconfig)") do |conffile| scriptopts.configfile = conffile end opts.on_tail("--write-config-file", "Writes the configfile with the data given if it does not alredy exist.") do |wc| scriptopts.writeconf = :write end opts.on_tail("--force-write-config-file", "Writes the configfile with the data given even if it does alredy exist.") do |wc| scriptopts.writeconf = :forcewrite end opts.on_tail("-u", "--username USERNAME", "Your Jira Username if it is not the one in the master file") do |u| scriptopts.username = u end opts.on_tail("-H", "--jira-url URL", "URL to rest api (without \"/rest/api/2\").") do |url| uri = URI(url) splitURI = URI.split(url) if splitURI[3] then url = splitURI[0].to_s + "://" + splitURI[2].to_s + ":" + splitURI[3].to_s + splitURI[5].to_s else url = splitURI[0].to_s + "://" + splitURI[2].to_s + splitURI[5].to_s end scriptopts.url = url end opts.on_tail("--no-cookie", "Don't try to authenticate via cookies") do || scriptopts. = true end opts.on_tail("-h", "--help", "Display this screen") do puts opts exit end opts.on_tail("--version", "Show version") do puts opts.ver exit end return scriptopts end |
.get_connection(scriptopts, connection, reconnect = false) ⇒ Connection
Don’t call this directly as it will fail if your credentials are wrong.
If there is already a connection known returns that connection. If not or if the parameter is true it tries to create a new Connect object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/jirarest2bin.rb', line 136 def self.get_connection(scriptopts, connection, reconnect = false) if ! connection || reconnect then begin connection = Connect.new(get_credentials(scriptopts)) connection.heal_uri! # We want to be sure so we try to heal the connection_url if possible connection rescue Jirarest2::CookieAuthenticationError => e connection.credentials.login(scriptopts.username,scriptopts.pass) retry rescue Jirarest2::CouldNotHealURIError => e puts "REST API not found at #{e.to_s}" exit 3 end else return connection end end |
.get_credentials(scriptopts) ⇒ Credentials
Gather all the credentials and build the credentials file
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jirarest2bin.rb', line 93 def self.get_credentials(scriptopts) filefail = false begin fileconf = MadbitConfig::read_configfile(scriptopts.configfile) # We don't want to set the Values from the configfile if we have them already set. scriptopts.username = fileconf["username"] if ( scriptopts.username.nil? && fileconf["username"] ) scriptopts.pass = fileconf["password"] if ( scriptopts.pass.nil? && fileconf["password"] ) if ( scriptopts.url.nil? && fileconf["URL"] ) then scriptopts.url = fileconf["URL"] end rescue IOError => e puts e filefail = false end scriptopts.url = (scriptopts.url + "/rest/api/2/") if !( scriptopts.url =~ /\/rest\/api\/2\/$/) missing = Array.new missing << "URL" if scriptopts.url.nil? missing << "username" if scriptopts.username.nil? if missing != [] then puts "Missing essential parameter(s) #{missing.join(",")}. Exiting..." exit 1 end if !scriptopts. then credentials = CookieCredentials.new(scriptopts.url,scriptopts.username,true) credentials. return credentials # TODO What to do if the cookie expired? else if scriptopts.pass.nil? && !( scriptopts.username.nil?) then scriptopts.pass = Jirarest2Bin::get_password(scriptopts.username) end return PasswordCredentials.new(scriptopts.url, scriptopts.username, scriptopts.pass) end end |
.get_password(username) ⇒ String
Get the password from an interactive shell
82 83 84 85 86 87 88 |
# File 'lib/jirarest2bin.rb', line 82 def self.get_password(username) STDIN.reopen '/dev/tty' password = ask("Enter your password for user \"#{username}\": ") { |q| q.echo = "*" } return password end |
.required_argument(name) ⇒ Object
Output a message if a required argument is not set
73 74 75 76 |
# File 'lib/jirarest2bin.rb', line 73 def self.required_argument(name) puts "Argument \"#{name}\" is mandatory." exit 1 end |
.write_configfile(scriptopts) ⇒ Object
write the config file for the conenction to jira
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/jirarest2bin.rb', line 39 def self.write_configfile(scriptopts) text = Hash.new if scriptopts.url.nil? then text["#URL"] = "https://host.domain.com:port/path/" else text["URL"] = "#{scriptopts.url}" end if scriptopts.username.nil? then text["#username"] = "USERNAME" else text["username"] = "#{scriptopts.username}" end text["#password"] = "Your!PassW0rd" if scriptopts..nil? || scriptopts. then text["nocookieauth"] = "false" else text["nocookieauth"] = "true" end begin if scriptopts.writeconf == :forcewrite then MadbitConfig::write_configfile(scriptopts.configfile,text,:force) else MadbitConfig::write_configfile(scriptopts.configfile,text) end puts "Configfile written to #{scriptopts.configfile}. Exiting." exit 0 rescue MadbitConfig::FileExistsException => e puts "Configfile #{e} already exists. Use \"--force-write-config-file\" to replace." exit 1 end end |