Module: RedmineIssue
- Defined in:
- lib/redmine_issue.rb
Defined Under Namespace
Classes: Undefined
Constant Summary collapse
- STATUS_INPROGRESS =
2
- STATUS_COMPLETED =
3
- STATUS_FEEDBACK =
4
- STATUS_CLOSED =
5
- CONFIG_PATH =
File.('~/.config/redmine-issue/config')
- CONFIG =
JSON.parse(File.read(CONFIG_PATH))
Class Method Summary collapse
- ._expand_issue_id(id) ⇒ Object
- ._get_arguments_hash(args) ⇒ Object
- ._get_arguments_key(key) ⇒ Object
- ._get_console_option(args, option) ⇒ Object
- ._get_current_elapsed_hours ⇒ Object
- ._get_responsible_user_id(issue) ⇒ Object
- ._get_user_id ⇒ Object
- ._pause_current(id) ⇒ Object
- ._split_text(text, length = 60) ⇒ Object
- .cancel ⇒ Object
- .close(id = nil) ⇒ Object
- .complete(id = nil) ⇒ Object
- .config(key, value = Undefined.new()) ⇒ Object
- .description(id = nil) ⇒ Object
- .help ⇒ Object
- .list(*args) ⇒ Object
- .pause ⇒ Object
- .reply(*args) ⇒ Object
- .request(method, get = {}, data = nil, options = {method: 'POST'}) ⇒ Object
- .set_config(key, value) ⇒ Object
- .start(id) ⇒ Object
- .status ⇒ Object
Class Method Details
._expand_issue_id(id) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/redmine_issue.rb', line 102 def self.(id) if id.nil?() return config('current')['id'] end if id.start_with?('#') return id[1..-1].to_i() end found = config('last_listed_issues', []).select() { |found_id| found_id.to_s().end_with?(id.to_s()) } if found.length > 1 raise "Issue #id is ambiguos; specify more digits" end if found.length == 0 return id end return found.first().to_i() end |
._get_arguments_hash(args) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/redmine_issue.rb', line 183 def self._get_arguments_hash(args) result = {} args.each_with_index() { |value, index| if value.start_with?('--no-') result[_get_arguments_key(value[5..-1])] = true next end if value.start_with?('--') if args[index + 1].nil?() || args[index + 1].start_with?('--') result[_get_arguments_key(value[2..-1])] = true next end result[_get_arguments_key(value[2..-1])] = args[index + 1] end } return result end |
._get_arguments_key(key) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/redmine_issue.rb', line 141 def self._get_arguments_key(key) key = key.gsub('_', '{{__DASH__}}') key = key.gsub('-', '_') key = key.gsub('{{__DASH__}}', '-') return key end |
._get_console_option(args, option) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/redmine_issue.rb', line 148 def self._get_console_option(args, option) if option.instance_of?(::Array) option.each() { |option_item| result = _get_console_option(args, option_item) if !result.nil?() return result end } return nil end if option.length == 1 index = args.index('-' + _get_arguments_key(option)) if !index.nil?() return args[index + 1] end end if args.index('--no-' + _get_arguments_key(option)) return false end index = args.index('--' + _get_arguments_key(option)) if index.nil?() return nil end if args[index + 1].nil?() return true end return args[index + 1] end |
._get_current_elapsed_hours ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/redmine_issue.rb', line 126 def self._get_current_elapsed_hours() current = config('current', nil) if current.nil?() || current['id'].nil?() raise 'No active issue in progress' end seconds = DateTime.now().to_time() - DateTime.parse(current['started']).to_time() hours = (seconds / 3600.0).round(2).to_s() return hours end |
._get_responsible_user_id(issue) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/redmine_issue.rb', line 215 def self._get_responsible_user_id(issue) if issue.instance_of?(::Fixnum) || issue.instance_of?(::String) issue = request("issues/#{issue}", {include: 'journals'})['issue'] end if !issue.has_key?('journals') raise "Issue should have journals to find out adminitrator id" end user_id = _get_user_id() issue['journals'].reverse().each() { |note| if note['user']['id'] != user_id return note['user']['id'] end } return issue['author']['id'] end |
._get_user_id ⇒ Object
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/redmine_issue.rb', line 204 def self._get_user_id() user_id = config('user_id', nil) if !user_id.nil?() return user_id end user_id = request('users/current')['user']['id'] set_config('user_id', user_id) return user_id end |
._pause_current(id) ⇒ Object
234 235 236 237 238 239 |
# File 'lib/redmine_issue.rb', line 234 def self._pause_current(id) current = config('current', nil) if !current.nil?() && current['id'].to_i() == id.to_i() pause() end end |
._split_text(text, length = 60) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/redmine_issue.rb', line 90 def self._split_text(text, length = 60) result = 0. upto((text.length / length.to_f()).floor()). collect() { |index| text[index * length, length] }. join("\n") return result end |
.cancel ⇒ Object
310 311 312 313 314 315 316 317 318 |
# File 'lib/redmine_issue.rb', line 310 def self.cancel() current = config('current', nil) if current.nil?() raise "No active issue in progress" end set_config('current', nil) return current end |
.close(id = nil) ⇒ Object
341 342 343 344 345 346 347 348 349 350 |
# File 'lib/redmine_issue.rb', line 341 def self.close(id = nil) real_id = (id) _pause_current(real_id) issue = { 'status_id' => STATUS_CLOSED } return request("issues/#{real_id}", {}, {'issue' => issue}, {method: 'PUT'}) end |
.complete(id = nil) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/redmine_issue.rb', line 329 def self.complete(id = nil) real_id = (id) _pause_current(real_id) issue = { 'assigned_to_id' => _get_responsible_user_id(real_id), 'status_id' => STATUS_COMPLETED, } return request("issues/#{real_id}", {}, {'issue' => issue}, {method: 'PUT'}) end |
.config(key, value = Undefined.new()) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/redmine_issue.rb', line 26 def self.config(key, value = Undefined.new()) if value.instance_of?(Undefined) && !CONFIG.has_key?(key) raise "#{key} should be set in config" end return CONFIG.fetch(key, value) end |
.description(id = nil) ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/redmine_issue.rb', line 352 def self.description(id = nil) real_id = (id) issue = request("issues/#{real_id}", {include: 'journals'})['issue'] journal = issue.delete('journals') info = issue. collect() { |key, value| if value.instance_of?(::Hash) if value.has_key?('name') value = value['name'] else value = value.to_s() end end [key.capitalize(), _split_text(value.to_s(), 100)] } comments = journal. select() { |element| !element['notes'].empty?() }. collect() { |element| [element['user']['name'], _split_text(element['notes'], 80)] } if comments.length > 0 info.push(['Comments', Terminal::Table.new(rows: comments)]) end return Terminal::Table.new(rows: info) end |
.help ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/redmine_issue.rb', line 406 def self.help() return <<-TEXT USAGE: redmine-issue [command] [args] Manage redmine issues. Commands: list List issues; arguments is API get params: http://www.redmine.org/projects/redmine/wiki/Rest_Issues; Example: --project-id 10 --status-id closed; default arguments: --assigned-to-id me --status-id open --sort "priority:desc,project" description [id] Get issue description and comments reply [id] -m message Reply to issue; adds comment, sets status "Feedback" and returns issue to responsible user start id Starts issue specified by id; starts tracking current issue and spent time and set issue status "In progress" pause Pause current issue; save spent time to issue and untrack current issue. cancel Cancel current issue; untrack current issue without time saving. status Get current issue id and spent time. complete [id] Complete issue; set status "Completed" to issue and returns issue to responsible user; if completes current - save spent time. close [id] Same as complete but set status "Closed"; you have to have permission to close issues to tun this command. config key Displays config value. set-config key value Sets config value. TEXT end |
.list(*args) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/redmine_issue.rb', line 241 def self.list(*args) params = _get_arguments_hash(args) params['assigned_to_id'] ||= 'me' params['status_id'] ||= 'open' params['sort'] ||= 'priority:desc,project' issues = request('issues', params).fetch('issues') ids = issues.collect() { |issue| issue['id'] } set_config('last_listed_issues', ids) info = [['id', 'priority', 'subject', 'info']] info.push(:separator) info += issues.collect() { |issue| next [ '#' + issue['id'].to_s(), [ issue.fetch('priority', {})['name'], issue.fetch('status', {})['name'], " ", ].join("\n"), _split_text(issue['subject']), [ issue.fetch('project', {})['name'], issue.fetch('author', {})['name'], ].join("\n") ] } info.pop() return Terminal::Table.new(rows: info) end |
.pause ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/redmine_issue.rb', line 294 def self.pause() current = config('current', nil) if current.nil?() raise "No active issue in progress" end time_entry = { 'issue_id' => current['id'], 'hours' => _get_current_elapsed_hours() } result = request('time_entries', {}, {'time_entry' => time_entry}) set_config('current', nil) return result end |
.reply(*args) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/redmine_issue.rb', line 387 def self.reply(*args) if args.length > 0 && args[0].match(/^\d+/) real_id = (args[0]) else id = _get_console_option(args, ['i', 'issue']) real_id = (id) end = _get_console_option(args, ['m', 'message']) issue = { 'assigned_to_id' => _get_responsible_user_id(real_id), 'status' => STATUS_FEEDBACK, 'notes' => , } return request("issues/#{real_id}", {}, {'issue' => issue}, {method: 'PUT'}) end |
.request(method, get = {}, data = nil, options = {method: 'POST'}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/redmine_issue.rb', line 46 def self.request(method, get = {}, data = nil, = {method: 'POST'}) query = get. collect() { |key, value | CGI.escape(key.to_s()) + '=' + CGI.escape(value.to_s()) }. join('&') uri = URI(config('address') + '/' + method + '.json?' + query) if data.nil?() request = Net::HTTP::Get.new(uri) else if [:method] == 'POST' request = Net::HTTP::Post.new(uri) elsif [:method] == 'PUT' request = Net::HTTP::Put.new(uri) else raise 'Wrong request method' end end request['X-Redmine-API-Key'] = config('secret') request['Content-Type'] = 'application/json' if !data.nil?() request.body = JSON.generate(data) end result = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) } if result.is_a?(Net::HTTPOK) && result.body == '' return true end begin return JSON.parse(result.body) rescue => error raise "Failed to parse json: #{result} (#{error})" end end |
.set_config(key, value) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/redmine_issue.rb', line 34 def self.set_config(key, value) if value.nil?() CONFIG.delete(key) else CONFIG[key] = value end FileUtils.mkdir_p(File.dirname(CONFIG_PATH)) new_config = JSON.pretty_generate(CONFIG) File.write(File.(CONFIG_PATH), new_config) end |
.start(id) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/redmine_issue.rb', line 276 def self.start(id) current = config('current', nil) if !current.nil?() raise "Can not start; issue #{current['id']} is already " + 'in progress' end real_id = (id) issue = { 'issue' => {'status_id' => STATUS_INPROGRESS} } result = request("issues/#{real_id}", {}, issue, {method: 'PUT'}) set_config('current', {'id' => real_id, 'started' => DateTime.now().to_s()}) return result end |
.status ⇒ Object
320 321 322 323 324 325 326 327 |
# File 'lib/redmine_issue.rb', line 320 def self.status() current = config('current', nil) if current.nil?() raise "No active issue in progress" end return "Issue: ##{current['id']}\nTime: #{_get_current_elapsed_hours()}" end |