Class: Caperoma

Inherits:
Object
  • Object
show all
Defined in:
lib/caperoma.rb,
lib/caperoma/version.rb

Defined Under Namespace

Modules: Version

Class Method Summary collapse

Class Method Details

.create_task(argv) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
# File 'lib/caperoma.rb', line 317

def self.create_task(argv)
  # test if Capefile exists
  capefile_filename = ENV['CAPEROMA_TEST'].blank? && ENV['CAPEROMA_INTEGRATION_TEST'].blank? ? 'Capefile' : 'Capefile.test'

  if File.exist?(capefile_filename)
    capedata = nil
    jira_url = nil
    jira_project_id = nil
    github_repo = nil
    feature_jira_task_id = nil
    bug_jira_task_id = nil
    chore_jira_task_id = nil
    fix_jira_task_id = nil
    meeting_jira_task_id = nil
    jira_transition_id_todo = nil
    jira_transition_id_in_progress = nil
    jira_transition_id_done = nil
    pivotal_tracker_project_id = nil

    create_features_in_pivotal = nil
    create_bugs_in_pivotal = nil
    create_chores_in_pivotal = nil
    create_fixes_in_pivotal_as_chores = nil
    create_meetings_in_pivotal_as_chores = nil

    capedata = YAML.load_file(capefile_filename)
    if capedata
      jira_url = capedata['jira_url']
      jira_project_id = capedata['jira_project_id']
      github_repo = capedata['github_repo']

      jira_issue_type_ids = capedata['jira_issue_type_ids']

      if jira_issue_type_ids
        feature_jira_task_id = jira_issue_type_ids['feature']
        bug_jira_task_id = jira_issue_type_ids['bug']
        chore_jira_task_id = jira_issue_type_ids['chore']
        fix_jira_task_id = jira_issue_type_ids['fix']
        meeting_jira_task_id = jira_issue_type_ids['meeting']
      end

      jira_transition_ids = capedata['jira_transition_ids']

      if jira_transition_ids
        jira_transition_id_todo = jira_transition_ids['todo']
        jira_transition_id_in_progress = jira_transition_ids['in_progress']
        jira_transition_id_done = jira_transition_ids['done']
      end

      pivotal_tracker_project_id = capedata['pivotal_tracker_project_id']

      create_features_in_pivotal = capedata['create_features_in_pivotal']
      create_bugs_in_pivotal = capedata['create_bugs_in_pivotal']
      create_chores_in_pivotal = capedata['create_chores_in_pivotal']
      create_fixes_in_pivotal_as_chores = capedata['create_fixes_in_pivotal_as_chores']
      create_meetings_in_pivotal_as_chores = capedata['create_meetings_in_pivotal_as_chores']

      folder_path = `git rev-parse --show-toplevel`.strip

      # find a project
      title_flag_position = nil
      title = nil
      description_flag_position = nil
      description = nil
      project_id_flag_position = nil
      project_id = nil
      pivotal_id_flag_position = nil
      pivotal_id = nil
      additional_time_flag_position = nil
      additional_time = nil

      title_flag_position = argv.index('-t') || argv.index('--title')
      title = argv[title_flag_position + 1] if title_flag_position
      description_flag_position = argv.index('-d') || argv.index('--description')
      description = argv[description_flag_position + 1] if description_flag_position
      pivotal_id_flag_position = argv.index('-p') || argv.index('-ptid') || argv.index('--pivotal_task_id')
      pivotal_id = argv[pivotal_id_flag_position + 1] if pivotal_id_flag_position
      additional_time_flag_position = argv.index('-a') || argv.index('--additional_time')
      additional_time = argv[additional_time_flag_position + 1] if additional_time_flag_position

      pivotal_id = pivotal_id.gsub(/#/, '') if pivotal_id.present?

      project = Project.all.select { |project| project.jira_project_id == jira_project_id || project.pivotal_tracker_project_id == pivotal_tracker_project_id || project.folder_path == folder_path || project.github_repo == github_repo }.first

      project ||= Project.new

      project.folder_path = folder_path
      project.jira_url = jira_url
      project.jira_project_id = jira_project_id
      project.github_repo = github_repo
      project.feature_jira_task_id = feature_jira_task_id
      project.bug_jira_task_id = bug_jira_task_id
      project.chore_jira_task_id = chore_jira_task_id
      project.fix_jira_task_id = fix_jira_task_id
      project.meeting_jira_task_id = meeting_jira_task_id
      project.jira_transition_id_todo = jira_transition_id_todo
      project.jira_transition_id_in_progress = jira_transition_id_in_progress
      project.jira_transition_id_done = jira_transition_id_done
      project.pivotal_tracker_project_id = pivotal_tracker_project_id
      project.create_features_in_pivotal = create_features_in_pivotal
      project.create_bugs_in_pivotal = create_bugs_in_pivotal
      project.create_chores_in_pivotal = create_chores_in_pivotal
      project.create_fixes_in_pivotal_as_chores = create_fixes_in_pivotal_as_chores
      project.create_meetings_in_pivotal_as_chores = create_meetings_in_pivotal_as_chores
      project.save

      pivotal_data = get_pivotal_data(pivotal_id)
      title ||= pivotal_data[:title]
      description ||= pivotal_data[:description]
      pivotal_id = pivotal_data[:pivotal_id]
      pivotal_estimate = pivotal_data[:estimate]

      if title
        record = nil

        case argv[0]
        when 'chore'
          record = project.chores.new(title: title, description: description, project_id: project_id, pivotal_id: pivotal_id, additional_time: additional_time, pivotal_estimate: pivotal_estimate)
        when 'bug'
          record = project.bugs.new(title: title, description: description, project_id: project_id, pivotal_id: pivotal_id, additional_time: additional_time, pivotal_estimate: pivotal_estimate)
        when 'feature'
          record = project.features.new(title: title, description: description, project_id: project_id, pivotal_id: pivotal_id, additional_time: additional_time, pivotal_estimate: pivotal_estimate)
        when 'fix'
          record = project.fixes.new(title: title, description: description, project_id: project_id, pivotal_id: pivotal_id, additional_time: additional_time, pivotal_estimate: pivotal_estimate)
        when 'meeting'
          record = project.meetings.new(title: title, description: description, project_id: project_id, pivotal_id: pivotal_id, additional_time: additional_time, pivotal_estimate: pivotal_estimate)
        end

        if record.valid?
          record.save
        else
          puts record.errors.details.to_s
        end
      else
        puts "Title is required. Add -t \"my #{argv[0]} title\" flag."
      end
    else
      puts 'Can not parse Capfile. Is it formatted properly?'
    end
  else
    puts 'Capefile not found. Are you in the project folder? If yes, run "caperoma init" to create Capefile.'
  end
end

.drop_dbObject



310
311
312
313
314
315
# File 'lib/caperoma.rb', line 310

def self.drop_db
  puts 'Deleting work history and settings'
  ActiveRecord::Base.connection.close
  File.delete(DB_SPEC[:database])
  puts 'Work history and settings deleted'
end

.get_jira_issue_type_idsObject

TODO: here is project.jira_url. but it has an undefined method project. should move this method to be inside a project (get ids for this project or something).



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/caperoma.rb', line 160

def self.get_jira_issue_type_ids
  puts 'Getting issue ids from Jira'

  if Account.jira.present?
    capefile_filename = ENV['CAPEROMA_TEST'].blank? && ENV['CAPEROMA_INTEGRATION_TEST'].blank? ? 'Capefile' : 'Capefile.test'
    if File.exist?(capefile_filename)
      capedata = YAML.load_file(capefile_filename)
      if capedata
        jira_url = capedata['jira_url']
        if jira_url
          puts 'Receiving issue types'
          conn = Faraday.new(url: jira_url) do |c|
            c.basic_auth(Account.jira.email, Account.jira.password)
            c.adapter Faraday.default_adapter
          end

          response = conn.get do |request|
            request.url 'rest/api/3/issuetype.json'
            request.headers['User-Agent'] = 'Caperoma'
            request.headers['Content-Type'] = 'application/json'
          end

          case response.status
          when 200, 201, 202, 204, 301, 302, 303, 304, 307
            puts 'Received these issue types:'

            result = JSON.parse(response.body)

            result.each do |item|
              puts "Name: #{item['name']}, ID: #{item['id']}"
            end
          when 401, 403
            puts 'No access to Jira. Maybe login or api_key are incorrect.'
          when 404
            puts 'Resource not found. Maybe Jira ID is incorrect.'
          else
            puts 'Could not get data from Jira.'
            puts "Error status: #{response.status}"
            puts "Message from server: #{response.reason_phrase}"
          end
        else
          puts 'Please put at least jira url into your Capefile'
        end
      else
        puts 'Can not parse Capfile. Is it formatted properly?'
      end
    else
      puts 'Capefile not found. Are you in the project folder? If yes, run "caperoma init" to create Capefile.'
    end
  else
    puts 'Please set up Jira account first'
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed.'
end

.get_jira_project_idsObject



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/caperoma.rb', line 518

def self.get_jira_project_ids
  puts 'Getting projects from Jira'

  if Account.jira.present?

    capefile_filename = ENV['CAPEROMA_TEST'].blank? && ENV['CAPEROMA_INTEGRATION_TEST'].blank? ? 'Capefile' : 'Capefile.test'
    if File.exist?(capefile_filename)
      capedata = YAML.load_file(capefile_filename)
      if capedata
        jira_url = capedata['jira_url']
        if jira_url
          conn = Faraday.new(url: jira_url) do |c|
            c.basic_auth(Account.jira.email, Account.jira.password)
            c.adapter Faraday.default_adapter
          end

          response = conn.get do |request|
            request.url 'rest/api/3/project.json'
            request.headers['User-Agent'] = 'Caperoma'
            request.headers['Content-Type'] = 'application/json'
          end

          case response.status
          when 200, 201, 202, 204, 301, 302, 303, 304, 307
            puts 'Received these projects'

            result = JSON.parse(response.body)

            result.each do |project|
              puts "Name: #{project['name']}, jira_project_id: #{project['id']}"
            end
          when 401, 403
            puts 'No access to Jira. Maybe login or api_key are incorrect.'
          when 404
            puts 'Resource not found. Maybe Jira ID is incorrect.'
          else
            puts 'Could not get data from Jira.'
            puts "Error status: #{response.status}"
            puts "Message from server: #{response.reason_phrase}"
          end
        else
          puts 'Please put at least jira url into your Capefile'
        end
      else
        puts 'Can not parse Capfile. Is it formatted properly?'
      end
    else
      puts 'Capefile not found. Are you in the project folder? If yes, run "caperoma init" to create Capefile.'
    end
  else
    puts 'Please set up Jira account first'
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed.'
end

.get_jira_transition_idsObject



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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/caperoma.rb', line 216

def self.get_jira_transition_ids
  puts 'Getting transition ids from Jira'

  if Account.jira.present?
    capefile_filename = ENV['CAPEROMA_TEST'].blank? && ENV['CAPEROMA_INTEGRATION_TEST'].blank? ? 'Capefile' : 'Capefile.test'
    if File.exist?(capefile_filename)
      capedata = YAML.load_file(capefile_filename)
      if capedata
        jira_url = capedata['jira_url']
        if jira_url
          jira_project_id = capedata['jira_project_id']

          if jira_project_id
            puts 'Receiving an issue to get transition ids:'

            conn = Faraday.new(url: jira_url) do |c|
              c.basic_auth(Account.jira.email, Account.jira.password)
              c.adapter Faraday.default_adapter
            end

            response = conn.get do |request|
              request.url "rest/api/3/search?jql=project=%22#{jira_project_id}%22"
              request.headers['User-Agent'] = 'Caperoma'
              request.headers['Content-Type'] = 'application/json'
            end

            case response.status
            when 200, 201, 202, 204, 301, 302, 303, 304, 307
              issues = JSON.parse(response.body)['issues']

              if !issues.empty?
                first_issue = issues[0]
                first_issue_key = first_issue['key']

                conn2 = Faraday.new(url: jira_url) do |c|
                  c.basic_auth(Account.jira.email, Account.jira.password)
                  c.adapter Faraday.default_adapter
                end

                response2 = conn2.get do |request|
                  request.url "rest/api/3/issue/#{first_issue_key}/transitions"
                  request.headers['User-Agent'] = 'Caperoma'
                  request.headers['Content-Type'] = 'application/json'
                end

                case response2.status
                when 200, 201, 202, 204, 301, 302, 303, 304, 307
                  puts 'Received these transitions:'
                  transitions = JSON.parse(response2.body)['transitions']

                  transitions.each do |transition|
                    pp "Name: #{transition['name']}, ID: #{transition['id']}"
                  end
                when 401, 403
                  puts 'No access to Jira. Maybe login or api_key are incorrect.'
                when 404
                  puts 'Resource not found. Maybe Jira ID is incorrect.'
                else
                  puts 'Could not get data from Jira.'
                  puts "Error status: #{response2.status}"
                  puts "Message from server: #{response2.reason_phrase}"
                end
              else
                puts 'Please create at least one issue in this project manually in the browser.'
              end
            when 401, 403
              puts 'No access to Jira. Maybe login or api_key are incorrect.'
            when 404
              puts 'Resource not found. Maybe Jira ID is incorrect.'
            else
              puts 'Could not get data from Jira.'
              puts "Error status: #{response.status}"
              puts "Message from server: #{response.reason_phrase}"
            end
          else
            puts 'Please put jira_project_id into your Capefile'
          end

        else
          puts 'Please put jira_url into your Capefile'
        end
      else
        puts 'Can not parse Capfile. Is it formatted properly?'
      end
    else
      puts 'Capefile not found. Are you in the project folder? If yes, run "caperoma init" to create Capefile.'
    end
  else
    puts 'Please set up Jira account first'
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed.'
end

.get_pivotal_data(pivotal_id) ⇒ Object



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
511
512
513
514
515
516
# File 'lib/caperoma.rb', line 461

def self.get_pivotal_data(pivotal_id)
  resulting_hash = { title: nil, description: nil, pivotal_id: pivotal_id, estimate: 0 }

  if ENV['CAPEROMA_INTEGRATION_TEST'].blank?
    if pivotal_id.present?
      if Account.pivotal.present?

        if pivotal_id.match? /\d+/
          conn = Faraday.new(url: 'https://www.pivotaltracker.com/') do |c|
            c.adapter Faraday.default_adapter
          end

          response = conn.get do |request|
            request.url "services/v5/stories/#{pivotal_id}"
            request.headers['User-Agent'] = 'Caperoma'
            request.headers['Content-Type'] = 'application/json'
            request.headers['X-TrackerToken'] = Account.pivotal.password
          end

          case response.status
          when 200, 201, 202, 204, 301, 302, 303, 304, 307
            result = JSON.parse response.body

            resulting_hash[:title] = result['name']
            resulting_hash[:description] = result['description']
            resulting_hash[:estimate] = result['estimate'].to_i
          when 401, 403
            puts 'No access Pivotal. Maybe login or api_key are incorrect.'
          when 404
            puts 'Resource not found. Maybe Pivotal ID is incorrect.'
          else
            puts 'Could not get data from Pivotal.'
            puts "Error status: #{response.status}"
            puts "Message from server: #{response.reason_phrase}"
          end

          resulting_hash
        else
          puts 'Pivotal ID needs to be copied from the task in Pivotal (in either 12345678 or #12345678 format).'
          puts "Pivotal ID you entered was #{pivotal_id}, which does not match the allowed format."
          puts 'Skipping usage of this ID.'
          puts 'Proceeding as if Pivotal ID was not set.'
          puts 'Please start/finish the needed task in Pivotal manually.'

          resulting_hash[:pivotal_id] = nil
        end
      else
        puts 'Please set up Pivotal account.'
      end
    end
  end
  resulting_hash
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Pivotal.'
  resulting_hash
end

.helpObject



625
626
627
# File 'lib/caperoma.rb', line 625

def self.help
  puts File.read(File.join(File.dirname(__FILE__), '..', 'HELP'))
end

.initObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/caperoma.rb', line 142

def self.init
  if `git rev-parse --is-inside-work-tree`.strip == 'true'
    template_path = File.join(File.dirname(__FILE__), '..', 'Capefile.template')
    capefile_filename = ENV['CAPEROMA_TEST'].blank? && ENV['CAPEROMA_INTEGRATION_TEST'].blank? ? 'Capefile' : 'Capefile.test'

    new_path = `git rev-parse --show-toplevel`.strip + '/' + capefile_filename

    FileUtils.cp template_path, new_path

    puts 'Capefile successfully created.'
    puts 'Please open Capefile and add your Jira/Git/Pivotal settings.'
  else
    puts "You don't seem to be inside a git project. Please go into a folder that has a git repository initiated."
  end
end

.manage_accounts(argv) ⇒ Object



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/caperoma.rb', line 609

def self.manage_accounts(argv)
  case argv[1] # flag
  when /^(create|add|--add|--create|-a|-c)$/
    Account.create(type: argv[2], email: argv[3], password: argv[4], username: argv[5])
  when /^(remove|delete|--remove|--delete|-d|-r)$/
    Account.where(type: argv[2]).destroy_all
  when nil
    Account.all.each { |x| puts "#{x.type[2..-1].capitalize}: #{x.email} #{x.password} #{x.username}" }
    puts ''
    puts 'to delete run "caperoma accounts remove "--<type>"'
    puts 'to update run "... accounts --add "--<type>" again'
  else
    help
  end
end

.manage_recipients(argv) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/caperoma.rb', line 574

def self.manage_recipients(argv)
  case argv[1] # flag
  when /^(create|add|--add|--create|-a|-c)$/
    ReportRecipient.create email: argv[2]
  when /^(remove|delete|--remove|--delete|-d|-r)$/
    ReportRecipient.where(email: argv[2]).destroy_all
  when nil
    ReportRecipient.all.each { |x| puts x.email }
  else
    help
  end
end

.manage_reports(argv) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/caperoma.rb', line 587

def self.manage_reports(argv)
  case argv[1] # flag
  when /^(daily|-d)$/
    DailyReport.create
  when /^(three_day|-t)$/
    ThreeDayReport.create
  when /^(weekly|-w)$/
    RetrospectiveReport.create
  when 'auto'
    case argv[2] # subflag
    when 'on'
      Report.schedule
    when 'off'
      Report.unschedule
    else
      help
    end
  else
    help
  end
end

.setupObject



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
89
90
91
92
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
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/caperoma.rb', line 51

def self.setup
  puts 'Initializing Caperoma'
  ActiveRecord::Base.connection.close
  File.delete(DB_SPEC[:database])

  puts 'Creating local database for storing work information'
  ActiveRecord::Base.establish_connection(DB_SPEC)
  ActiveRecord::Schema.define do
    create_table :accounts do |t|
      t.column :username, :string
      t.column :email, :string
      t.column :password, :string
      t.column :type, :string

      t.timestamps
    end

    create_table :projects do |t|
      t.column :name, :string
      t.column :jira_project_id, :string
      t.column :folder_path, :string
      t.column :jira_url, :string
      t.column :jira_project_id, :string
      t.column :github_repo, :string
      t.column :feature_jira_task_id, :string
      t.column :bug_jira_task_id, :string
      t.column :chore_jira_task_id, :string
      t.column :fix_jira_task_id, :string
      t.column :meeting_jira_task_id, :string
      t.column :jira_transition_id_todo, :string
      t.column :jira_transition_id_in_progress, :string
      t.column :jira_transition_id_done, :string
      t.column :pivotal_tracker_project_id, :string
      t.column :create_features_in_pivotal, :boolean
      t.column :create_bugs_in_pivotal, :boolean
      t.column :create_chores_in_pivotal, :boolean
      t.column :create_fixes_in_pivotal_as_chores, :boolean
      t.column :create_meetings_in_pivotal_as_chores, :boolean

      t.timestamps
    end
    add_index :projects, :jira_project_id

    create_table :tasks do |t|
      t.column :project_id, :integer
      t.column :title, :string
      t.column :description, :text
      t.column :url, :text
      t.column :uuid, :string
      t.column :pivotal_estimate, :integer, default: 0
      t.column :type, :string
      t.column :jira_id, :string
      t.column :jira_key, :string
      t.column :jira_url, :string
      t.column :parent_branch, :string
      t.column :branch, :string
      t.column :pivotal_id, :string
      t.column :additional_time, :integer
      t.column :started_at, :datetime
      t.column :finished_at, :datetime
      t.column :daily_report_id, :integer
      t.column :three_day_report_id, :integer
      t.column :retrospective_report_id, :integer

      t.timestamps
    end
    add_index :tasks, :project_id
    add_index :tasks, :daily_report_id
    add_index :tasks, :three_day_report_id
    add_index :tasks, :retrospective_report_id

    create_table :reports do |t|
      t.column :content, :text
      t.column :tasks_for_tomorrow, :text
      t.column :type, :string

      t.timestamps
    end

    create_table :report_recipients do |t|
      t.column :email, :string

      t.timestamps
    end
  end

  puts 'Done'

  puts "Database with all your data is located at: #{DB_SPEC[:database]}"
end