class Task

Public Class Methods

abort_started(comment) click to toggle source
# File lib/caperoma/models/task.rb, line 40
def self.abort_started(comment)
  puts 'Aborting current task'
  unfinished.each { |task| task.abort(comment) }
  puts 'Current task aborted'
end
abort_started_without_time(comment) click to toggle source
# File lib/caperoma/models/task.rb, line 46
def self.abort_started_without_time(comment)
  puts 'Aborting current task without putting time into Jira'
  unfinished.each { |task| task.abort_without_time(comment) }
  puts 'Current task aborted without putting time into Jira'
end
finish_started(comment) click to toggle source
# File lib/caperoma/models/task.rb, line 28
def self.finish_started(comment)
  puts 'Finishing current task'
  unfinished.each { |task| task.finish(comment) }
  puts 'Current task finished'
end
pause_started(comment) click to toggle source
# File lib/caperoma/models/task.rb, line 34
def self.pause_started(comment)
  puts 'Pausing current task'
  unfinished.each { |task| task.pause(comment) }
  puts 'Current task paused'
end
status() click to toggle source
# File lib/caperoma/models/task.rb, line 52
def self.status
  if unfinished.empty?
    puts 'You are not working on anything now.'
  else
    unfinished.each do |task|
      puts 'You are working on: '
      puts "Title: #{task.title}"
      puts "Type: #{task.type}"
      puts "Jira ID: #{task.jira_key} (#{task.jira_live_url})." if task.jira_key.present?
      puts "Pivotal ID: #{task.pivotal_id} (#{task.pivotal_url})" if task.pivotal_id.present?
      puts "Time spent at the moment: #{task.time_spent_so_far}"
      puts "Branch with the task: #{task.branch}" if task.branch.present?
      puts "Pull request will be sent to this branch: #{task.parent_branch}" if task.parent_branch.present?
      puts "Project location: #{task.project.folder_path}"
    end
  end
end

Public Instance Methods

abort(comment) click to toggle source
# File lib/caperoma/models/task.rb, line 88
def abort(comment)
  # finish without commit or push
  update_attribute(:finished_at, Time.now)
  close_issue_on_jira
  log_work_to_jira(comment) if should_log_work?
  finish_on_pivotal if finish_on_pivotal?
  puts time_spent
end
abort_without_time(_comment) click to toggle source
# File lib/caperoma/models/task.rb, line 97
def abort_without_time(_comment)
  # finish without commit or push
  update_attribute(:finished_at, Time.now)
  close_issue_on_jira
  # the task closes on Jira, but is still running in Pivotal
  puts time_spent
end
finish(comment) click to toggle source
# File lib/caperoma/models/task.rb, line 70
def finish(comment)
  # full pull request
  update_attribute(:finished_at, Time.now)
  close_issue_on_jira
  log_work_to_jira(comment) if should_log_work?
  finish_on_pivotal if finish_on_pivotal?
  puts time_spent
end
jira_live_url() click to toggle source
# File lib/caperoma/models/task.rb, line 131
def jira_live_url
  "#{project.jira_url}browse/#{jira_key}" if jira_key.present?
end
pause(comment = 'Done') click to toggle source
# File lib/caperoma/models/task.rb, line 79
def pause(comment = 'Done')
  # finish with commit & push but without pull request
  update_attribute(:finished_at, Time.now)
  close_issue_on_jira
  log_work_to_jira(comment) if should_log_work?
  finish_on_pivotal if finish_on_pivotal?
  puts time_spent
end
pivotal_url() click to toggle source
# File lib/caperoma/models/task.rb, line 127
def pivotal_url
  "https://www.pivotaltracker.com/story/show/#{pivotal_id}" if pivotal_id.present?
end
should_log_work?() click to toggle source
# File lib/caperoma/models/task.rb, line 105
def should_log_work?
  time_spent_so_far != '0h 0m' && Account.jira.present?
end
time_spent() click to toggle source
# File lib/caperoma/models/task.rb, line 118
def time_spent
  result = TimeDifference.between(started_at, finished_at).in_minutes

  hours = (result / 60).to_i
  minutes = (result - hours * 60).to_i

  "#{hours}h #{minutes}m"
end
time_spent_in_minutes() click to toggle source
# File lib/caperoma/models/task.rb, line 135
def time_spent_in_minutes
  TimeDifference.between(started_at, finished_at).in_minutes # TODO: test
end
time_spent_so_far() click to toggle source
# File lib/caperoma/models/task.rb, line 109
def time_spent_so_far
  result = TimeDifference.between(started_at, Time.now).in_minutes

  hours = (result / 60).to_i
  minutes = (result - hours * 60).to_i

  "#{hours}h #{minutes}m"
end

Private Instance Methods

close_issue_on_jira() click to toggle source
# File lib/caperoma/models/task.rb, line 310
def close_issue_on_jira
  if not_test?
    puts 'Closing the issue in Jira'

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

    response = conn.post do |request|
      request.url "rest/api/3/issue/#{jira_key}/transitions"
      request.body = close_issue_on_jira_data
      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 'Closed the issue in Jira'
    when 401, 403
      puts "No access to the task #{jira_key} in Jira. Maybe login or api_key are incorrect."
    when 404
      puts "A task with ID #{jira_key} is not found in Jira."
    else
      puts 'Could not close the issue in Jira.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Jira.'
end
close_issue_on_jira_data() click to toggle source
# File lib/caperoma/models/task.rb, line 304
def close_issue_on_jira_data
  Jbuilder.encode do |j|
    j.transition { j.id project.jira_transition_id_done }
  end
end
create_issue_on_jira() click to toggle source
# File lib/caperoma/models/task.rb, line 477
def create_issue_on_jira
  if not_test?
    puts 'Creating an issue in Jira'

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

    response = conn.post do |request|
      request.url 'rest/api/3/issue.json'
      request.body = create_issue_on_jira_data
      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 'Created the issue in Jira'

      result = JSON.parse response.body

      update_attributes(
        jira_id: result['id'],
        jira_key: result['key'],
        jira_url: result['self']
      )
    when 401, 403
      puts "Forbidden access to the resource in Jira. Maybe login, api_key or Jira project id #{project.jira_project_id} are incorrect."
    when 404
      puts "Not found the resource in Jira. Maybe the Jira Project ID #{project.jira_project_id} is incorrect."
    else
      puts 'Could not create the issue in Jira.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Jira.'
end
create_issue_on_jira_data() click to toggle source
# File lib/caperoma/models/task.rb, line 440
def create_issue_on_jira_data
  hash = {
    fields: {
      summary: title.to_s,
      issuetype: {
        id: issue_type
      },
      project: {
        id: project.jira_project_id.to_s
      },
      assignee: {
        name: Account.jira.username
      }
    }
  }

  description_hash = {
    type: 'doc',
    version: 1,
    content: [
      {
        type: 'paragraph',
        content: [
          {
            text: description,
            type: 'text'
          }
        ]
      }
    ]
  }

  hash[:fields][:description] = description_hash if description.present?

  hash.to_json
end
create_issue_on_pivotal() click to toggle source
# File lib/caperoma/models/task.rb, line 402
def create_issue_on_pivotal
  if not_test?
    puts 'Creating a task in Pivotal'

    conn = Faraday.new(url: 'https://www.pivotaltracker.com/') do |c|
      c.adapter Faraday.default_adapter
    end

    response = conn.post do |request|
      request.url "services/v5/projects/#{project.pivotal_tracker_project_id}/stories"
      request.body = create_issue_on_pivotal_data
      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
      puts 'Created the task in Pivotal'
      result = JSON.parse response.body

      update_attributes(
        pivotal_id: result['id']
      )
    when 401, 403
      puts "No access to the server. Maybe login, api_key or Pivotal Project ID ##{project.pivotal_tracker_project_id} is incorrect."
    when 404
      puts "Resource not found. Maybe Pivotal Project ID ##{project.pivotal_tracker_project_id} is incorrect."
    else
      puts 'Could not create the task in Pivotal.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Pivotal.'
end
create_issue_on_pivotal_data() click to toggle source
# File lib/caperoma/models/task.rb, line 392
def create_issue_on_pivotal_data
  Jbuilder.encode do |j|
    j.current_state 'unstarted'
    j.estimate pivotal_estimate == 0 ? 1 : pivotal_estimate
    j.name title.to_s
    j.description description
    j.story_type story_type
  end
end
create_on_jira?() click to toggle source
# File lib/caperoma/models/task.rb, line 145
def create_on_jira?
  Account.jira.present? && not_test?
end
create_on_pivotal?() click to toggle source
# File lib/caperoma/models/task.rb, line 153
def create_on_pivotal?
  pivotal_id.blank? && this_is_a_type_a_user_wants_to_create? && Account.pivotal.present? && not_test?
end
current_time() click to toggle source
# File lib/caperoma/models/task.rb, line 384
def current_time
  Time.now.in_time_zone('UTC').strftime('%Y-%m-%dT%H:%M:00.000+0000')
end
enable_git?() click to toggle source
# File lib/caperoma/models/task.rb, line 522
def enable_git?
  ENV['CAPEROMA_TEST'].blank? && ENV['CAPEROMA_INTEGRATION_TEST'].blank?
end
finish_on_pivotal() click to toggle source
# File lib/caperoma/models/task.rb, line 232
def finish_on_pivotal
  if not_test?
    puts 'Finishing the task in Pivotal'

    conn = Faraday.new(url: 'https://www.pivotaltracker.com/') do |c|
      c.adapter Faraday.default_adapter
    end

    response = conn.put do |request|
      request.url "services/v5/stories/#{pivotal_id}"
      request.body = finish_on_pivotal_data
      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
      puts 'Finished the task in Pivotal'
    when 401, 403
      puts "No access to the task ##{pivotal_id} in Pivotal. Maybe login or api_key are incorrect."
    when 404
      puts "A task with ID ##{pivotal_id} is not found in Pivotal."
    else
      puts 'Could not finish the task in Pivotal.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Pivotal.'
end
finish_on_pivotal?() click to toggle source
# File lib/caperoma/models/task.rb, line 161
def finish_on_pivotal?
  pivotal_id.present? && Account.pivotal.present? && not_test?
end
finish_on_pivotal_data() click to toggle source
# File lib/caperoma/models/task.rb, line 226
def finish_on_pivotal_data
  Jbuilder.encode do |j|
    j.current_state 'finished'
  end
end
generate_uuid() click to toggle source
# File lib/caperoma/models/task.rb, line 169
def generate_uuid
  self.uuid = SecureRandom.uuid
end
issue_type() click to toggle source
# File lib/caperoma/models/task.rb, line 388
def issue_type
  project.feature_jira_task_id
end
log_work_to_jira(comment = 'Done') click to toggle source
# File lib/caperoma/models/task.rb, line 351
def log_work_to_jira(comment = 'Done')
  if not_test?
    puts 'Logging work to Jira'

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

    response = conn.post do |request|
      request.url "rest/api/3/issue/#{jira_key}/worklog"
      request.body = log_work_to_jira_data(comment)
      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 'Work logged to Jira'
    when 401, 403
      puts "No access to the task #{jira_key} in Jira. Maybe login or api_key are incorrect."
    when 404
      puts "A task with ID #{jira_key} is not found in Jira."
    else
      puts 'Could not log work to Jira.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Jira.'
end
log_work_to_jira_data(comment = 'Done') click to toggle source
# File lib/caperoma/models/task.rb, line 343
def log_work_to_jira_data(comment = 'Done')
  Jbuilder.encode do |j|
    j.comment comment
    j.started current_time
    j.timeSpent time_spent
  end
end
not_test?() click to toggle source
# File lib/caperoma/models/task.rb, line 518
def not_test?
  ENV['CAPEROMA_INTEGRATION_TEST'].blank?
end
output_jira_key() click to toggle source
# File lib/caperoma/models/task.rb, line 179
def output_jira_key
  puts jira_key
end
output_jira_key?() click to toggle source
# File lib/caperoma/models/task.rb, line 183
def output_jira_key?
  jira_key.present? && not_test?
end
set_start_time() click to toggle source
# File lib/caperoma/models/task.rb, line 173
def set_start_time
  time = Time.now
  time -= additional_time.to_i.minutes if additional_time.present?
  self.started_at = time
end
start_issue_on_jira() click to toggle source
# File lib/caperoma/models/task.rb, line 271
def start_issue_on_jira
  if not_test?
    puts 'Starting the issue in Jira'

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

    response = conn.post do |request|
      request.url "rest/api/3/issue/#{jira_key}/transitions"
      request.body = start_issue_on_jira_data
      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 'Started the issue in Jira'
    when 401, 403
      puts "No access to the task #{jira_key} in Jira. Maybe login or api_key are incorrect."
    when 404
      puts "A task with ID #{jira_key} is not found in Jira."
    else
      puts 'Could not start the issue in Jira.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Jira.'
end
start_issue_on_jira_data() click to toggle source
# File lib/caperoma/models/task.rb, line 265
def start_issue_on_jira_data
  Jbuilder.encode do |j|
    j.transition { j.id project.jira_transition_id_in_progress }
  end
end
start_issue_on_pivotal() click to toggle source
# File lib/caperoma/models/task.rb, line 193
def start_issue_on_pivotal
  if not_test?
    puts 'Starting the task in Pivotal'

    conn = Faraday.new(url: 'https://www.pivotaltracker.com/') do |c|
      c.adapter Faraday.default_adapter
    end

    response = conn.put do |request|
      request.url "services/v5/stories/#{pivotal_id}"
      request.body = start_issue_on_pivotal_data
      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
      puts 'Started the task in Pivotal'
    when 401, 403
      puts "No access to the task ##{pivotal_id} in Pivotal. Maybe login or api_key are incorrect."
    when 404
      puts "A task with ID ##{pivotal_id} is not found in Pivotal."
    else
      puts 'Could not start the task in Pivotal.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Pivotal.'
end
start_issue_on_pivotal_data() click to toggle source
# File lib/caperoma/models/task.rb, line 187
def start_issue_on_pivotal_data
  Jbuilder.encode do |j|
    j.current_state 'started'
  end
end
start_on_jira?() click to toggle source
# File lib/caperoma/models/task.rb, line 149
def start_on_jira?
  jira_key.present? && Account.jira.present? && not_test?
end
start_on_pivotal?() click to toggle source
# File lib/caperoma/models/task.rb, line 157
def start_on_pivotal?
  pivotal_id.present? && Account.pivotal.present? && not_test?
end
story_type() click to toggle source
# File lib/caperoma/models/task.rb, line 141
def story_type
  'chore' # default is chore, it's never used directly
end
this_is_a_type_a_user_wants_to_create?() click to toggle source
# File lib/caperoma/models/task.rb, line 165
def this_is_a_type_a_user_wants_to_create?
  false
end