class Sfctl::Commands::Time::Sync
Public Class Methods
new(options)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 15 def initialize(options) @options = options @pastel = Pastel.new(enabled: !@options['no-color']) @prompt = ::TTY::Prompt.new end
Public Instance Methods
execute(output: $stdout)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 21 def execute(output: $stdout) return if !config_present?(output) || !link_config_present?(output) if connections.length.zero? output.puts @pastel.red('Please add a connection before continue.') return end sync_assignments(output, assignments_to_sync) rescue ThreadError, JSON::ParserError output.puts output.puts @pastel.red('Something went wrong.') end
Private Instance Methods
assignment_items(time_entries, connection)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 150 def assignment_items(time_entries, connection) case connection['provider'] when TOGGL_PROVIDER Toggl::Sync.assignment_items(time_entries) when HARVEST_PROVIDER Harvest::Sync.assignment_items(time_entries, connection) end end
assignments_from_connections()
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 41 def assignments_from_connections connections.map do |con| id = con[0] asmnt = con[1] { 'id' => id, 'name' => asmnt['name'], 'service' => asmnt['service'] || '-' } end end
assignments_to_sync()
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 53 def assignments_to_sync assignments = assignments_from_connections return assignments if @options['all'] assignment_id = select_assignment(assignments) return assignments if assignment_id == 'all' assignments.select { |a| a['id'].to_s == assignment_id.to_s }.to_a end
connections()
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 37 def connections @connections ||= read_link_config.fetch('connections', []) end
load_time_entries(output, next_report, connection)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 104 def load_time_entries(output, next_report, connection) output.puts "Next Report: #{@pastel.cyan(report_name(next_report))}" next_report_interval = report_interval(next_report) case connection['provider'] when TOGGL_PROVIDER Toggl::Sync.load_data( output, connection, read_link_config['providers'][TOGGL_PROVIDER], @pastel, next_report_interval ) when HARVEST_PROVIDER Harvest::Sync.load_data( output, connection, read_link_config['providers'][HARVEST_PROVIDER], @pastel, next_report_interval ) end end
print_dry_run_enabled(output)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 136 def print_dry_run_enabled(output) output.puts @pastel.yellow('Dry run enabled. Skipping upload to starfish.team.') output.puts true end
print_no_next_reporting_segment(output)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 128 def print_no_next_reporting_segment(output) message = <<~HEREDOC No next reporting segment on Starfish that accepts time report data, the synchronization will be skipped. HEREDOC output.puts @pastel.red(message) true end
print_report_contains_data(output, next_report)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 142 def print_report_contains_data(output, next_report) output.puts @pastel.yellow( "Report #{report_name(next_report)} contains data. Skipping upload to starfish.team." ) output.puts true end
print_upload_results(output, success, spinner)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 169 def print_upload_results(output, success, spinner) if success spinner.success(@pastel.green('Done')) else spinner.error output.puts @pastel.red('Something went wrong. Unable to upload time entries to starfish.team') end output.puts output.puts true end
report_interval(record)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 98 def report_interval(record) start_date = Date.parse("#{record['year']}-#{record['month']}-01") end_date = start_date.next_month.prev_day [start_date, end_date] end
report_name(next_report)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 124 def report_name(next_report) "[#{next_report['year']}-#{next_report['month']}]" end
select_assignment(assignments)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 65 def select_assignment(assignments) @prompt.select('Which assignment do you want to sync?') do |menu| assignments.each do |asmnt| menu.choice name: "#{asmnt['name']} / #{asmnt['service']}", value: asmnt['id'].to_s end menu.choice name: 'All', value: 'all' end end
sync(output, assignment, connection)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 82 def sync(output, assignment, connection) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize output.puts "Synchronizing: #{@pastel.cyan("[#{assignment['name']} / #{assignment['service']}]")}" success, next_report = Starfish::Client.next_report(@options['starfish-host'], access_token, assignment['id']) print_no_next_reporting_segment(output) && return if !success || next_report.empty? time_entries = load_time_entries(output, next_report, connection) print_dry_run_enabled(output) && return if @options['dry_run'] print_report_contains_data(output, next_report) && return if touchy?(next_report) uploading_to_starfish(output, assignment, time_entries, connection) if time_entries.any? end
sync_assignments(output, list)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 74 def sync_assignments(output, list) list.each do |assignment| assignment_id = assignment['id'].to_s connection = connections.select { |c| c == assignment_id } sync(output, assignment, connection[assignment_id]) end end
touchy?(next_report)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 120 def touchy?(next_report) @options['touchy'] && next_report['data'] == 'present' end
uploading_to_starfish(output, assignment, time_entries, connection)
click to toggle source
# File lib/sfctl/commands/time/sync.rb, line 159 def uploading_to_starfish(output, assignment, time_entries, connection) spinner = TTY::Spinner.new('Uploading to starfish.team: [:spinner]', format: :dots) spinner.auto_spin success = Starfish::Client.update_next_report( @options['starfish-host'], access_token, assignment['id'], assignment_items(time_entries, connection) ) print_upload_results(output, success, spinner) end