Class: HarvestWorklog::TimeOffCLI
- Inherits:
-
Object
- Object
- HarvestWorklog::TimeOffCLI
- Defined in:
- lib/harvest_worklog.rb
Class Method Summary collapse
- .holiday_regions_from_environment ⇒ Object
- .normalize_holiday_regions!(regions) ⇒ Object
- .option_parser(options) ⇒ Object
- .print_dry_run(output, dates, options) ⇒ Object
- .resolve_assignment(client, project_name, task_name) ⇒ Object
- .run(arguments, output: $stdout, error: $stderr, client: nil) ⇒ Object
- .validate!(dates, options) ⇒ Object
Class Method Details
.holiday_regions_from_environment ⇒ Object
158 159 160 |
# File 'lib/harvest_worklog.rb', line 158 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
162 163 164 165 166 167 |
# File 'lib/harvest_worklog.rb', line 162 def self.normalize_holiday_regions!(regions) regions.map! { |region| region.strip.downcase } regions.reject!(&:empty?) regions.uniq! regions end |
.option_parser(options) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/harvest_worklog.rb', line 121 def self.option_parser() OptionParser.new do |opts| opts. = <<~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| [:project] = value } opts.on("--task NAME", "Harvest task name") { |value| [:task] = value } opts.on("--project-id ID", Integer, "Harvest project ID") { |value| [:project_id] = value } opts.on("--task-id ID", Integer, "Harvest task ID") { |value| [:task_id] = value } opts.on("--hours HOURS", Float, "Hours per day (default: 7)") { |value| [:hours] = value } opts.on("--notes NOTES", "Optional note on every entry") { |value| [:notes] = value } opts.on("--holiday-region REGION", "Holidays region; repeat for each locality") { |value| [:holiday_regions] << value.strip.downcase } opts.on("--dry-run", "Print entries without calling Harvest") { [:dry_run] = true } opts.on("-h", "--help", "Show this help") do puts opts exit 0 end end end |
.print_dry_run(output, dates, options) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/harvest_worklog.rb', line 184 def self.print_dry_run(output, dates, ) project = [:project] || "project ##{[:project_id]}" task = [:task] || "task ##{[:task_id]}" notes = [:notes] ? "; #{[:notes]}" : "" dates.each do |date| output.puts "Would create #{date.iso8601}: #{HarvestWorklog.display_hours([:hours])}h on #{project} / #{task}#{notes}" end end |
.resolve_assignment(client, project_name, task_name) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/harvest_worklog.rb', line 169 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
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 111 112 113 114 115 116 117 118 119 |
# File 'lib/harvest_worklog.rb', line 80 def self.run(arguments, output: $stdout, error: $stderr, client: nil) = { hours: 7.0, dry_run: false, holiday_regions: holiday_regions_from_environment } parser = option_parser() dates = parser.parse(arguments) normalize_holiday_regions!([:holiday_regions]) validate!(dates, ) from = Date.iso8601(dates[0]) to = Date.iso8601(dates[1]) workdays = HarvestWorklog.dates_between(from, to, holiday_regions: [:holiday_regions]) raise Error, "date range contains no days to enter" if workdays.empty? if [:dry_run] print_dry_run(output, workdays, ) return 0 end client ||= Marlens::HarvestApiV2::Client.from_environment project_id, task_id = if [:project_id] [[:project_id], [:task_id]] else resolve_assignment(client, [:project], [:task]) end workdays.each do |date| entry = client.create_time_entry( project_id:, task_id:, spent_date: date, hours: [:hours], notes: [:notes] ) output.puts "Created #{date.iso8601}: #{HarvestWorklog.display_hours([:hours])}h (entry ##{entry.fetch("id")})" end 0 rescue Error, Marlens::HarvestApiV2::Error, OptionParser::ParseError, Date::Error => e error.puts "Error: #{e.}" error.puts parser if parser 1 end |
.validate!(dates, options) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/harvest_worklog.rb', line 144 def self.validate!(dates, ) raise Error, "FROM and TO are required" unless dates.length == 2 raise Error, "--hours must be a positive finite number" unless [:hours].positive? && [:hours].finite? raise Error, "--holiday-region or HARVEST_HOLIDAY_REGIONS is required" if [:holiday_regions].empty? raise Error, "--notes must not be blank" if [:notes]&.strip&.empty? names_provided = [:project] || [:task] ids_provided = [:project_id] || [:task_id] valid_names = [:project] && ![:project].strip.empty? && [:task] && ![:task].strip.empty? valid_ids = [:project_id]&.positive? && [: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 |