Class: HarvestWorklog::TimeOffCLI

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

Class Method Summary collapse

Class Method Details

.holiday_regions_from_environmentObject



149
150
151
# File 'lib/harvest_worklog.rb', line 149

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

.normalize_holiday_regions!(regions) ⇒ Object



153
154
155
156
157
158
# File 'lib/harvest_worklog.rb', line 153

def self.normalize_holiday_regions!(regions)
  regions.map! { |region| region.strip.downcase }
  regions.reject!(&:empty?)
  regions.uniq!
  regions
end

.option_parser(options) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/harvest_worklog.rb', line 112

def self.option_parser(options)
  OptionParser.new do |opts|
    opts.banner = <<~USAGE
      Usage: harvest-worklog time-off FROM TO --project NAME --task NAME [options]
             harvest-worklog 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


175
176
177
178
179
180
181
182
# File 'lib/harvest_worklog.rb', line 175

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}: #{HarvestWorklog.display_hours(options[:hours])}h on #{project} / #{task}#{notes}"
  end
end

.resolve_assignment(client, project_name, task_name) ⇒ Object

Raises:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/harvest_worklog.rb', line 160

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/harvest_worklog.rb', line 71

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)
  normalize_holiday_regions!(options[:holiday_regions])
  validate!(dates, options)

  from = Date.iso8601(dates[0])
  to = Date.iso8601(dates[1])
  workdays = HarvestWorklog.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}: #{HarvestWorklog.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:



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/harvest_worklog.rb', line 135

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?
  raise Error, "--notes must not be blank" if options[:notes]&.strip&.empty?

  names_provided = options[:project] || options[:task]
  ids_provided = options[:project_id] || options[:task_id]
  valid_names = options[:project] && !options[:project].strip.empty? && options[:task] && !options[:task].strip.empty?
  valid_ids = options[:project_id]&.positive? && options[:task_id]&.positive?
  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