Class: HarvestTimeOff::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/harvest_time_off.rb

Class Method Summary collapse

Class Method Details

.holiday_regions_from_environmentObject



106
107
108
# File 'lib/harvest_time_off.rb', line 106

def self.holiday_regions_from_environment
  ENV.fetch("HARVEST_HOLIDAY_REGIONS", "ca_yt").split(",").map { |region| region.strip.downcase }.reject(&:empty?)
end

.option_parser(options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/harvest_time_off.rb', line 70

def self.option_parser(options)
  OptionParser.new do |opts|
    opts.banner = <<~USAGE
      Usage: harvest-time-off FROM TO --project NAME --task NAME [options]
             harvest-time-off FROM TO --project-id ID --task-id ID [options]

      Creates one Harvest duration entry for each local business day from FROM through TO.
    USAGE
    opts.on("--project NAME", "Harvest project name") { |value| options[:project] = value }
    opts.on("--task NAME", "Harvest task name") { |value| options[:task] = value }
    opts.on("--project-id ID", Integer, "Harvest project ID") { |value| options[:project_id] = value }
    opts.on("--task-id ID", Integer, "Harvest task ID") { |value| options[:task_id] = value }
    opts.on("--hours HOURS", Float, "Hours per day (default: 7)") { |value| options[:hours] = value }
    opts.on("--notes NOTES", "Optional note on every entry") { |value| options[:notes] = value }
    opts.on("--holiday-region REGION", "Holidays region; repeat for each locality") { |value| options[:holiday_regions] << value.strip.downcase }
    opts.on("--dry-run", "Print entries without calling Harvest") { options[:dry_run] = true }
    opts.on("-h", "--help", "Show this help") do
      puts opts
      exit 0
    end
  end
end


125
126
127
128
129
130
131
132
# File 'lib/harvest_time_off.rb', line 125

def self.print_dry_run(output, dates, options)
  project = options[:project] || "project ##{options[:project_id]}"
  task = options[:task] || "task ##{options[:task_id]}"
  notes = options[:notes] ? "; #{options[:notes]}" : ""
  dates.each do |date|
    output.puts "Would create #{date.iso8601}: #{HarvestTimeOff.display_hours(options[:hours])}h on #{project} / #{task}#{notes}"
  end
end

.resolve_assignment(client, project_name, task_name) ⇒ Object

Raises:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/harvest_time_off.rb', line 110

def self.resolve_assignment(client, project_name, task_name)
  # ponytail: one 2,000-item page covers personal assignments; follow cursor pagination if that ceiling is exceeded.
  matches = client.active_personal_task_assignments.select do |assignment|
    assignment.dig("project", "name")&.casecmp?(project_name) &&
      assignment.dig("task", "name")&.casecmp?(task_name)
  end

  return [matches.first.dig("project", "id"), matches.first.dig("task", "id")] if matches.one?

  qualifier = "project #{project_name.inspect} and task #{task_name.inspect}"
  raise Error, "No active task assignment matches #{qualifier}. Pass --project-id and --task-id instead." if matches.empty?

  raise Error, "Multiple active task assignments match #{qualifier}. Pass --project-id and --task-id instead."
end

.run(arguments, output: $stdout, error: $stderr, client: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/harvest_time_off.rb', line 30

def self.run(arguments, output: $stdout, error: $stderr, client: nil)
  options = { hours: 7.0, dry_run: false, holiday_regions: holiday_regions_from_environment }
  parser = option_parser(options)
  dates = parser.parse(arguments)
  validate!(dates, options)

  from = Date.iso8601(dates[0])
  to = Date.iso8601(dates[1])
  workdays = HarvestTimeOff.dates_between(from, to, holiday_regions: options[:holiday_regions])
  raise Error, "date range contains no days to enter" if workdays.empty?

  if options[:dry_run]
    print_dry_run(output, workdays, options)
    return 0
  end

  client ||= Marlens::HarvestApiV2::Client.from_environment
  project_id, task_id = if options[:project_id]
                          [options[:project_id], options[:task_id]]
                        else
                          resolve_assignment(client, options[:project], options[:task])
                        end

  workdays.each do |date|
    entry = client.create_time_entry(
      project_id:,
      task_id:,
      spent_date: date,
      hours: options[:hours],
      notes: options[:notes]
    )
    output.puts "Created #{date.iso8601}: #{HarvestTimeOff.display_hours(options[:hours])}h (entry ##{entry.fetch("id")})"
  end
  0
rescue Error, Marlens::HarvestApiV2::Error, OptionParser::ParseError, Date::Error => e
  error.puts "Error: #{e.message}"
  error.puts parser if parser
  1
end

.validate!(dates, options) ⇒ Object

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/harvest_time_off.rb', line 93

def self.validate!(dates, options)
  raise Error, "FROM and TO are required" unless dates.length == 2
  raise Error, "--hours must be a positive finite number" unless options[:hours].positive? && options[:hours].finite?
  raise Error, "--holiday-region or HARVEST_HOLIDAY_REGIONS is required" if options[:holiday_regions].empty?

  names_provided = options[:project] || options[:task]
  ids_provided = options[:project_id] || options[:task_id]
  valid_names = options[:project] && options[:task]
  valid_ids = options[:project_id] && options[:task_id]
  raise Error, "supply --project and --task, or --project-id and --task-id" unless valid_names || valid_ids
  raise Error, "do not combine project/task names with IDs" if names_provided && ids_provided
end