Skip to content

Commit c213a04

Browse files
committed
Add import command for bulk timeslip creation
The `import` command will take an array of JSON timeslips from STDIN and batch create them using the API. If the -U, -P or -T flags are specified, it will override the user, project or task values in each timeslip to be the ones matched. This allows you to copy or move existing timeslips for to a new user or an alternative project. The timeslip objects must be contained in a single JSON array – which is not the output created by this tool (it creates a stream). Thus you need to pass the output from `timeslips list` through `jq -s` before piping it into `timeslips import`: fa timeslips list … | jq -s | fa timeslips import …
1 parent d3fa0da commit c213a04

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

exe/fa

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,37 @@ module Freeagent
158158
end
159159
end
160160

161+
desc 'import [-U USER] [-P PROJECT [-T TASK]]', 'Import timeslips'
162+
method_option :user, aliases: '-U', type: :string, default: nil, desc: 'Name or email of a user to override in input data'
163+
method_option :project, aliases: '-P', type: :string, default: nil, desc: 'Name of a project to override in input data'
164+
method_option :task, aliases: '-T', type: :string, default: nil, desc: 'Name of a task within the project to override in input data'
165+
method_option :format, type: :string, default: 'human', desc: 'Output format, one of human or json'
166+
def import
167+
raise ArgumentError, '--project must be specified if --task if used' if options[:project].nil? && !options[:task].nil?
168+
169+
user = options[:user].nil? ? nil : match(:user, api.users, options[:user], :first_name, :last_name, :email)
170+
project = options[:project].nil? ? nil : match(:project, api.projects, options[:project], :name)
171+
task = options[:task].nil? ? nil : match(:task, api.tasks(project), options[:task], :name)
172+
173+
users[user.url] = user unless user.nil?
174+
projects[project.url] = project unless project.nil?
175+
tasks[task.url] = task unless task.nil?
176+
177+
api.batch_create_timeslips(JSON.parse(STDIN.read, object_class: SnakyHash::SymbolKeyed).map do |timeslip|
178+
timeslip.user = user.url unless user.nil?
179+
timeslip.project = project.url unless project.nil?
180+
timeslip.task = task.url unless task.nil?
181+
182+
puts case options[:format].downcase
183+
when 'human'; human(timeslip)
184+
when 'json'; timeslip.to_json
185+
else raise ArgumentError, "unknown output format #{options[:format]}"
186+
end
187+
188+
timeslip
189+
end)
190+
end
191+
161192
desc 'delete USER PROJECT TASK [FROM [TO]]', 'Delete timeslips'
162193
method_option :format, type: :string, default: 'human', desc: 'Output format, one of human or json'
163194
def delete user, project, task, from = nil, to = nil

lib/freeagent.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def create_timeslip user, project, task, dated, hours
167167
end
168168

169169
def batch_create_timeslips timeslips
170-
post('timeslips', {'timeslips' => timeslips})
170+
post('timeslips', {'timeslips' => timeslips.to_a})
171171
end
172172

173173
def delete_timeslip timeslip

0 commit comments

Comments
 (0)