require 'fileutils' require 'renuo/cli/app/redmine/issue' require 'renuo/cli/app/toggl/time_entry' class Work ACTIONS = %w[start].freeze 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 # TODO: I want to implement also the stop action. def validate_action abort('>> No action given. It must be start') unless ACTIONS.include? @action end def validate_project_name abort('>> No project name given.') unless @project_name end 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 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 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 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 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 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 def create_toggl_time_entry Toggl::TimeEntry.start(time_entry: { description: @ticket_number.to_s, tags: [@project_name.to_s], created_with: 'curl' }) end def stop_toggl_time_entry(time_entry_id) Toggl::TimeEntry.find(time_entry_id).stop end end