class Work

Constants

ACTIONS

Public Instance Methods

run(args) click to toggle source
# File lib/renuo/cli/app/work.rb, line 10
def run(args)
  ActiveResource::Base.logger = Logger.new($stdout)
  @action, @project_name, @ticket_number = args
  validate_action
  validate_project_name
  validate_ticket_number
  start_feature_branch
  update_redmine_ticket
  start_toggl
end

Private Instance Methods

create_toggl_time_entry() click to toggle source
# File lib/renuo/cli/app/work.rb, line 87
def create_toggl_time_entry
  Toggl::TimeEntry.start(time_entry: { description: @ticket_number.to_s,
                                       tags: [@project_name.to_s],
                                       created_with: "curl" })
end
existing_toggl(current_time_entry) click to toggle source
# File lib/renuo/cli/app/work.rb, line 66
def existing_toggl(current_time_entry)
  say("A timer '#{current_time_entry.description}' was already running.")
  if current_time_entry.description.to_i == @ticket_number.to_i
    say("I will keep using it")
  else
    say("I stopped it and started a new time entry.")
    stop_toggl_time_entry(current_time_entry.id)
    create_toggl_time_entry
  end
end
start_feature_branch() click to toggle source
# File lib/renuo/cli/app/work.rb, line 42
def start_feature_branch
  project_folder = `autojump #{@project_name}`.strip
  system("cd #{project_folder} && git stash && git checkout develop " \
         "&& git pull && git flow feature start #{@ticket_number}")
end
start_toggl() click to toggle source
# File lib/renuo/cli/app/work.rb, line 55
def start_toggl
  current_time_entry = Toggl::TimeEntry.current
  if current_time_entry.nil?
    create_toggl_time_entry
  elsif current_time_entry.description.nil?
    update_toggl_time_entry(current_time_entry.id)
  else
    existing_toggl(current_time_entry)
  end
end
stop_toggl_time_entry(time_entry_id) click to toggle source
# File lib/renuo/cli/app/work.rb, line 93
def stop_toggl_time_entry(time_entry_id)
  Toggl::TimeEntry.find(time_entry_id).stop
end
update_redmine_ticket() click to toggle source
# File lib/renuo/cli/app/work.rb, line 48
def update_redmine_ticket
  issue = Redmine::Issue.find(@ticket_number)
  issue.status_id = Redmine::Issue::STATUSES[:in_progress]
  issue.save
  system("open #{issue.html_url}")
end
update_toggl_time_entry(time_entry_id) click to toggle source
# File lib/renuo/cli/app/work.rb, line 77
def update_toggl_time_entry(time_entry_id)
  say("A timer was already running but without a project assigned. I updated the current time entry.")

  time_entry = Toggl::TimeEntry.find(time_entry_id)
  time_entry.description = @ticket_number.to_s
  time_entry.tags = [@project_name.to_s]
  time_entry.created_with = "curl"
  time_entry.save
end
validate_action() click to toggle source

TODO: I want to implement also the stop action.

# File lib/renuo/cli/app/work.rb, line 24
def validate_action
  abort(">> No action given. It must be start") unless ACTIONS.include? @action
end
validate_project_name() click to toggle source
# File lib/renuo/cli/app/work.rb, line 28
def validate_project_name
  abort(">> No project name given.") unless @project_name
end
validate_ticket_number() click to toggle source
# File lib/renuo/cli/app/work.rb, line 32
def validate_ticket_number
  abort(">> No ticket number given.") unless @ticket_number
  issue = Redmine::Issue.find(@ticket_number)
  open_statuses = Redmine::Issue::STATUSES.values_at(:to_start, :planned, :in_progress, :qa)
  return if open_statuses.include?(issue.status.id)

  system("open #{issue.html_url}")
  abort(">> Ticket should be in an open status")
end