Class: Lita::Handlers::Redmine2
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::Redmine2
- Defined in:
- lib/lita/handlers/redmine2.rb
Instance Method Summary collapse
-
#decrypt_token(token, user_id) ⇒ Object
try decrypt token and if not ecrypted, update redis record.
-
#encryptor ⇒ Object
encrypting mechanism.
-
#get_activity_by_name(name, resource) ⇒ Object
get activity id by name.
-
#get_closing_status_id(resource) ⇒ Object
get status id with close attribute.
-
#get_project_by_name(name, resource) ⇒ Object
find user id by name via API.
- #get_status_by_name(name, resource) ⇒ Object
-
#get_user_by_name(name, resource) ⇒ Object
find user id by name via API.
-
#get_user_token(user_id) ⇒ Object
retrieve user API token from redis.
- #info(response) ⇒ Object
-
#issue(response) ⇒ Object
show issue subject.
-
#issue_assign(response) ⇒ Object
assign issue to user specified by name.
-
#issue_change_state(response) ⇒ Object
change status of issue.
-
#issue_close(response) ⇒ Object
close issue.
-
#issue_detail(response) ⇒ Object
show issue subject and description.
-
#issue_journal(response) ⇒ Object
show issue subject and description.
-
#issue_link(response) ⇒ Object
show issue link.
-
#issue_mention(response) ⇒ Object
show issue subject when someone mention issue #.
-
#issue_new(response) ⇒ Object
create issue in project specified by name.
-
#issue_note(response) ⇒ Object
write new issue note.
-
#issues(response) ⇒ Object
list issues of user.
-
#list_objects(response) ⇒ Object
get list of objects.
-
#parse_list_objects(list) ⇒ Object
parse list of objects.
-
#projects(response) ⇒ Object
list projects of user.
-
#register(response) ⇒ Object
save user API token into redis.
-
#time_entry_new(response) ⇒ Object
create new time entry.
-
#token(response) ⇒ Object
send saved API token to DM/PM.
-
#unregister(response) ⇒ Object
delete saved API token.
Instance Method Details
#decrypt_token(token, user_id) ⇒ Object
try decrypt token and if not ecrypted, update redis record
513 514 515 516 517 518 519 520 |
# File 'lib/lita/handlers/redmine2.rb', line 513 def decrypt_token(token, user_id) decrypted_token = encryptor.decrypt_string(token) rescue encrypted_token = encryptor.encrypt_string(token) redis.set("user_#{user_id}", encrypted_token) token end |
#encryptor ⇒ Object
encrypting mechanism
523 524 525 526 |
# File 'lib/lita/handlers/redmine2.rb', line 523 def encryptor secret_key = config.secret_key.nil? ? "BqJYq7FQjhXuaPSkbTxw" : config.secret_key @encryptor ||= Crypt::Blowfish.new(secret_key) end |
#get_activity_by_name(name, resource) ⇒ Object
get activity id by name
420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/lita/handlers/redmine2.rb', line 420 def get_activity_by_name(name, resource) activities = resource.list(object: 'enumerations/time_entry_activities') activities = activities[:resource_error].nil? ? activities['time_entry_activities'] : {} activity_id = nil activities.each do |a| activity_id = a['id'] if a['name'] == name end activity_id end |
#get_closing_status_id(resource) ⇒ Object
get status id with close attribute
445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/lita/handlers/redmine2.rb', line 445 def get_closing_status_id(resource) states = resource.list(object: 'issue_statuses') states = states['issue_statuses'].nil? ? [] : states['issue_statuses'] status_id = nil states.each do |s| if s['is_closed'] status_id = s['id'] break end end status_id end |
#get_project_by_name(name, resource) ⇒ Object
find user id by name via API
407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/lita/handlers/redmine2.rb', line 407 def get_project_by_name(name, resource) projects = resource.projects projects = projects[:resource_error].nil? ? projects['projects'] : {} id = nil projects.each do |prj| id = prj['id'] if prj['name'] == name end id end |
#get_status_by_name(name, resource) ⇒ Object
432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/lita/handlers/redmine2.rb', line 432 def get_status_by_name(name, resource) states = resource.list(object: 'issue_statuses') states = states['issue_statuses'].nil? ? [] : states['issue_statuses'] status_id = nil states.each do |s| status_id = s['id'] if s['name'] == name end status_id end |
#get_user_by_name(name, resource) ⇒ Object
find user id by name via API
393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/lita/handlers/redmine2.rb', line 393 def get_user_by_name(name, resource) users = resource.users users = users[:resource_error].nil? ? users['users'] : {} id = nil users.each do |user| user_name = user['firstname'] + " " + user['lastname'] id = user['id'] if user_name == name end id end |
#get_user_token(user_id) ⇒ Object
retrieve user API token from redis
380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/lita/handlers/redmine2.rb', line 380 def get_user_token(user_id) token = redis.get("user_#{user_id}") token = decrypt_token(token, user_id) if !token.nil? && !token.empty? connection = Faraday.new(url: config.url, headers: { "X-Redmine-API-Key" => token }) resource = RedmineResource.new(connection: connection) else resource = nil end end |
#info(response) ⇒ Object
35 36 37 |
# File 'lib/lita/handlers/redmine2.rb', line 35 def info(response) response.reply "Lita Redmine Bot by NetBrick" end |
#issue(response) ⇒ Object
show issue subject
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/lita/handlers/redmine2.rb', line 89 def issue(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] issue = resource.issue(id: issue_id) if issue[:resource_error].nil? issue = issue['issue'] response.reply "#{issue['id']}: (#{issue['project']['name']}) #{issue["subject"]}" else response.reply_privately issue[:resource_error] end end end |
#issue_assign(response) ⇒ Object
assign issue to user specified by name
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/lita/handlers/redmine2.rb', line 208 def issue_assign(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] name = response.matches.flatten[2] note = response.matches.flatten[3].nil? ? "" : response.matches.flatten[3].strip note = note[1, note.length - 2] if user_id = get_user_by_name(name, resource) issue = resource.update_issue({ assigned_to_id: user_id, notes: note }, id: issue_id) if issue[:resource_error].nil? response.reply issue[:message] else response.reply_privately issue[:resource_error] end else response.reply_privately "Unable to find user by name" end end end |
#issue_change_state(response) ⇒ Object
change status of issue
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/lita/handlers/redmine2.rb', line 163 def issue_change_state(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] status_name = response.matches.flatten[2] if status_id = get_status_by_name(status_name, resource) issue = resource.update_issue({ status_id: status_id }, id: issue_id) if issue[:resource_error].nil? response.reply issue[:message] else response.reply_privately issue[:resource_error] end else response.reply_privately "Unable to find status by specified name" end end end |
#issue_close(response) ⇒ Object
close issue
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/lita/handlers/redmine2.rb', line 186 def issue_close(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] if status_id = get_closing_status_id(resource) issue = resource.update_issue({ status_id: status_id }, id: issue_id) if issue[:resource_error].nil? response.reply issue[:message] else response.reply_privately issue[:resource_error] end else response.reply_privately "Unable to find any closing status" end end end |
#issue_detail(response) ⇒ Object
show issue subject and description
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/lita/handlers/redmine2.rb', line 108 def issue_detail(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] issue = resource.issue(id: issue_id) if issue[:resource_error].nil? issue = issue['issue'] response.reply "#{issue['id']}: (#{issue['project']['name']}) #{issue["subject"]}\n#{issue['description']}" else response.reply_privately issue[:resource_error] end end end |
#issue_journal(response) ⇒ Object
show issue subject and description
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/lita/handlers/redmine2.rb', line 127 def issue_journal(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] issue = resource.issue(id: issue_id) if issue[:resource_error].nil? issue = issue['issue'] = "#{issue['id']}: (#{issue['project']['name']}) #{issue["subject"]}\n#{issue['description']}" issue['journals'] = issue['journals'].nil? ? {} : issue['journals'] issue['journals'].each do |j| if !j['notes'].nil? && !j['notes'].empty? = + "\n" = + "Author: #{j['user']['name']}\n" = + j['notes'] + "\n" end end response.reply else response.reply_privately issue[:resource_error] end end end |
#issue_link(response) ⇒ Object
show issue link
158 159 160 |
# File 'lib/lita/handlers/redmine2.rb', line 158 def issue_link(response) response.reply "Issue ##{response.matches.flatten[1]}: #{config.url}/issues/#{response.matches.flatten[1]}" end |
#issue_mention(response) ⇒ Object
show issue subject when someone mention issue #
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/lita/handlers/redmine2.rb', line 263 def issue_mention(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[0] issue = resource.issue(id: issue_id) if issue[:resource_error].nil? issue = issue['issue'] response.reply "#{issue['id']}: (#{issue['project']['name']}) #{issue["subject"]}" else response.reply_privately issue[:resource_error] end end end |
#issue_new(response) ⇒ Object
create issue in project specified by name
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/lita/handlers/redmine2.rb', line 233 def issue_new(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else project_name = response.matches.flatten[1] subject = response.matches.flatten[2] description = response.matches.flatten[3] assignee_name = response.matches.flatten[4].nil? ? "" : response.matches.flatten[4].strip assignee_name = assignee_name[1, assignee_name.length - 2] project_id = get_project_by_name(project_name, resource) user_id = get_user_by_name(assignee_name, resource) if project_id issue = resource.create_issue({ project_id: project_id, subject: subject, description: description, assigned_to_id: user_id }) if issue[:resource_error].nil? issue = issue['issue'] response.reply "Issue created with id #{issue['id']}" else response.reply_privately issue[:resource_error] end else response.reply_privately "Unable to find project by name" end end end |
#issue_note(response) ⇒ Object
write new issue note
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/lita/handlers/redmine2.rb', line 282 def issue_note(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] issue_note = response.matches.flatten[2] issue = resource.update_issue({ notes: issue_note }, id: issue_id) if issue[:resource_error].nil? response.reply issue[:message] else response.reply_privately issue[:resource_error] end end end |
#issues(response) ⇒ Object
list issues of user
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/lita/handlers/redmine2.rb', line 301 def issues(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issues = resource.issues if issues[:resource_error].nil? = '' issues = issues['issues'].nil? ? {} : issues['issues'] issues.each do |issue| << "#{issue['id']}: (#{issue['project']['name']}) #{issue['subject']}\n" end response.reply else response.reply_privately issue[:resource_error] end end end |
#list_objects(response) ⇒ Object
get list of objects
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lita/handlers/redmine2.rb', line 40 def list_objects(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else object = response.matches.flatten[1] # Update route as there objects require 'enumerations' in the url if object == 'time_entry_activities' || object == 'issue_priorities' object = 'enumerations/' + object end = resource.list(object: object) if [:resource_error].nil? response.reply parse_list_objects() else response.reply_privately [:resource_error] end end end |
#parse_list_objects(list) ⇒ Object
parse list of objects
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/lita/handlers/redmine2.rb', line 461 def parse_list_objects(list) = '' case list.keys[0] when 'users' users = list['users'] users.each do |u| << "#{u['id']}: #{u['firstname']} #{u['lastname']}\n" end when 'issues' issues = list['issues'] issues.each do |i| << "#{i['id']}: (#{i['project']['name']}) #{i['subject']}\n" end when 'projects' projects = list['projects'] projects.each do |p| << "#{p['id']}: (#{p['name']})\n" end when 'time_entries' time_entries = list['time_entries'] time_entries.each do |t| << "Issue_id: #{t['issue']['id']}, spent_on: #{t['spent_on']}, hours: #{t['hours']}\n" end when 'time_entry_activities' time_entry_activities = list['time_entry_activities'] time_entry_activities.each do |t| << "Name: #{t['name']}\n" end when 'issue_statuses' issue_statuses = list['issue_statuses'] issue_statuses.each do |s| << "Name: #{s['name']}, closes issue: #{s['is_closed'] ? 'yes' : 'no'}\n" end when 'trackers' trackers = list['trackers'] trackers.each do |t| << "Name: #{t['name']}\n" end when 'issue_priorities' issue_priorities = list['issue_priorities'] issue_priorities.each do |p| << "Name: #{p['name']}\n" end else = 'Currently unsupported' end end |
#projects(response) ⇒ Object
list projects of user
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/lita/handlers/redmine2.rb', line 324 def projects(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else projects = resource.projects if projects[:resource_error].nil? = '' projects = projects['projects'].nil? ? {} : projects['projects'] projects.each do |prj| << "#{prj['id']}: (#{prj['name']})\n" end response.reply else response.reply_privately projects[:resource_error] end end end |
#register(response) ⇒ Object
save user API token into redis
347 348 349 350 351 352 353 354 355 356 |
# File 'lib/lita/handlers/redmine2.rb', line 347 def register(response) user_id = response.user.id api_token = response.args[1] api_token = encryptor.encrypt_string(api_token) redis.set("user_#{user_id}", api_token) = "Registered: #{user_id} url #{config.url}" response.reply end |
#time_entry_new(response) ⇒ Object
create new time entry
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lita/handlers/redmine2.rb', line 63 def time_entry_new(response) resource = get_user_token(response.user.id) if resource.nil? response.reply_privately "Token not registered, register token via 'redmine register <api_token>'" else issue_id = response.matches.flatten[1] hours = response.matches.flatten[2] activity_name = response.matches.flatten[3] comments = response.matches.flatten[4] if activity_id = get_activity_by_name(activity_name, resource) = resource.create_entry({issue_id: issue_id, hours: hours, activity_id: activity_id, comments: comments}) if [:resource_error].nil? response.reply [:message] else response.reply_privately [:resource_error] end else response.reply_privately "Unable to find activity by specified name" end end end |
#token(response) ⇒ Object
send saved API token to DM/PM
359 360 361 362 363 364 365 366 367 |
# File 'lib/lita/handlers/redmine2.rb', line 359 def token(response) user_id = response.user.id token = redis.get("user_#{user_id}") token = decrypt_token(token, user_id) = "Token: #{token}" response.reply_privately end |
#unregister(response) ⇒ Object
delete saved API token
370 371 372 373 374 375 376 377 |
# File 'lib/lita/handlers/redmine2.rb', line 370 def unregister(response) user_id = response.user.id redis.set("user_#{user_id}", "") = "Unregistered: #{user_id}" response.reply end |