Class: PT::UI

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

Constant Summary collapse

GLOBAL_CONFIG_PATH =
ENV['HOME'] + "/.pt"
LOCAL_CONFIG_PATH =
Dir.pwd + '/.pt'

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ UI

Returns a new instance of UI.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pt/ui.rb', line 12

def initialize(args)
  require 'pt/debugger' if ARGV.delete('--debug')
  @io = HighLine.new
  @global_config = load_global_config
  @client = Client.new(@global_config[:api_number])
  @local_config = load_local_config
  @project = @client.get_project(@local_config[:project_id])
  command = args[0].to_sym rescue :my_work
  @params = args[1..-1]
  commands.include?(command.to_sym) ? send(command.to_sym) : help
end

Instance Method Details

#acceptObject



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/pt/ui.rb', line 262

def accept
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i
    title("Accepting '#{task.name}'")
  else
    tasks = @client.get_my_tasks_to_accept(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to mark it as accepted", table)
  end
  @client.mark_task_as(@project, task, 'accepted')
  congrats("Task accepted, hooray!")
end

#assignObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/pt/ui.rb', line 181

def assign
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i
    owner = find_owner @params[1]
  else
    title("Tasks for #{user_s} in #{project_to_s}")
    tasks = @client.get_tasks_to_assign(@project)
    table = TasksTable.new(tasks)
    task = select("Please select a task to assign it an owner", table)
  end

  unless owner
    members = @client.get_members(@project)
    table = PersonsTable.new(members.map(&:person))
    owner = select("Please select a member to assign him the task", table)
  end
  @client.assign_task(@project, task, owner)

  congrats("Task assigned to #{owner.initials}, thanks!")
end

#commentObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pt/ui.rb', line 161

def comment
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i
    comment = @params[1]
    title("Adding a comment to #{task.name}")
  else
    tasks = @client.get_my_work(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to comment it", table)
    comment = ask("Write your comment")
  end
  if @client.comment_task(@project, task, comment)
    congrats("Comment sent, thanks!")
    save_recent_task( task.id )
  else
    error("Ummm, something went wrong.")
  end
end

#createObject



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/pt/ui.rb', line 94

def create
  if @params[0]
    name = @params[0]
    owner = @params[1] || @local_config[:user_name]
    requester = @local_config[:user_name]
    task_type = task_type_or_nil(@params[1]) || task_type_or_nil(@params[2]) || 'feature'
  else
    title("Let's create a new task:")
    name = ask("Name for the new task:")
  end

  owner = @client.find_member(@project, owner).person.id if owner.kind_of?(String)

  unless owner
    if ask('Do you want to assign it now? (y/n)').downcase == 'y'
      members = @client.get_members(@project)
      table = PersonsTable.new(members.map(&:person))
      owner = select("Please select a member to assign him the task.", table).id
    else
      owner = nil
    end
    requester = @local_config[:user_name]
    task_type = ask('Type? (c)hore, (b)ug, anything else for feature)')
  end

  task_type = case task_type
              when 'c', 'chore'
                'chore'
              when 'b', 'bug'
                'bug'
              else
                'feature'
              end
  result = nil

  owner_ids = [owner]
  # did you do a -m so you can add a description?
  if ARGV.include? "-m" or ARGV.include? "--m"
    editor = ENV.fetch('EDITOR') { 'vi' }
    temp_path = "/tmp/editor-#{ Process.pid }.txt"
    system "#{ editor } #{ temp_path }"

    description = File.read(temp_path)
    story = @client.create_task_with_description(@project, name, owner_ids, task_type, description)
  else
    story = @client.create_task(@project, name, owner_ids, task_type)
  end
  # TODO need result
  congrats("#{task_type} for #{owner} open #{story.url}")
end

#deliverObject



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/pt/ui.rb', line 248

def deliver
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i
    title("Delivering '#{task.name}'")
  else
    tasks = @client.get_my_tasks_to_deliver(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to mark it as delivered", table)
  end

  deliver_task task
end

#deliver_task(task) ⇒ Object



403
404
405
406
407
# File 'lib/pt/ui.rb', line 403

def deliver_task task
  return if task.story_type == 'chore'
  @client.mark_task_as(@project, task, 'delivered')
  congrats("Task delivered, congrats!")
end

#doneObject



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/pt/ui.rb', line 345

def done
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i

    #we need this for finding again later
    task_id = task.id

    if !@params[1] && task.estimate == -1
      error("You need to give an estimate for this task")
      return
    end

    if @params[1] && task.estimate == -1
      if [0,1,2,3].include? @params[1].to_i
        estimate_task(task, @params[1].to_i)
      end
      if @params[2]
        task = task_by_id_or_pt_id task_id
        @client.comment_task(@project, task, @params[2])
      end
    else
      @client.comment_task(@project, task, @params[1]) if @params[1]
    end

    task = task_by_id_or_pt_id task_id
    start_task task

    task = task_by_id_or_pt_id task_id
    finish_task task

    task = task_by_id_or_pt_id task_id
    deliver_task task
  end
end

#estimateObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/pt/ui.rb', line 202

def estimate
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i
    title("Estimating '#{task.name}'")

    if [0,1,2,3].include? @params[1].to_i
      estimation = @params[1]
    end
  else
    tasks = @client.get_my_tasks_to_estimate(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to estimate it", table)
  end

  estimation ||= ask("How many points you estimate for it? (#{@project.point_scale})")
  @client.estimate_task(@project, task, estimation)
  congrats("Task estimated, thanks!")
end

#estimate_task(task, difficulty) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/pt/ui.rb', line 380

def estimate_task task, difficulty
  result = @client.estimate_task(@project, task, difficulty)
  if result.errors.any?
    error(result.errors.errors)
  else
    congrats("Task estimated, thanks!")
  end
end

#findObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/pt/ui.rb', line 409

def find
  if (story_id = @params[0].to_i).nonzero?
    if task = task_by_id_or_pt_id(@params[0].to_i)
      return show_task(task)
    else
      message("Task not found by id (#{story_id}), falling back to text search")
    end
  end

  if @params[0]
    tasks = @client.search_for_story(@project, @params[0])
    tasks.each do |story_task|
      title("--- [#{(tasks.index story_task) + 1 }] -----------------")
      show_task(story_task)
    end
    message("No matches found for '#{@params[0]}'") if tasks.empty?
  else
    message("You need to provide a substring for a tasks title.")
  end
end

#finishObject



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/pt/ui.rb', line 235

def finish
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i
    title("Finishing '#{task.name}'")
  else
    tasks = @client.get_my_tasks_to_finish(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to mark it as finished", table)
  end
  finish_task task
end

#finish_task(task) ⇒ Object



394
395
396
397
398
399
400
401
# File 'lib/pt/ui.rb', line 394

def finish_task task
  if task.story_type == 'chore'
    @client.mark_task_as(@project, task, 'accepted')
  else
    @client.mark_task_as(@project, task, 'finished')
  end
  congrats("Another task bites the dust, yeah!")
end

#helpObject



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/pt/ui.rb', line 440

def help
  if ARGV[0] && ARGV[0] != 'help'
    message("Command #{ARGV[0]} not recognized. Showing help.")
  end

  title("Command line usage for pt #{VERSION}")
  help = <<-HELP
  pt                                                                      # show all available stories

  pt todo      <owner>                                                    # show all unscheduled stories

  pt (unscheduled,started,finished,delivered, accepted, rejected) <owner> # show all (unscheduled,started,finished,delivered, accepted, rejected) stories

  pt create    [title] <owner> <type> -m                                  # create a new story (and include description ala git commit)

  pt show      [id]                                                       # shows detailed info about a story

  pt tasks     [id]                                                       # manage tasks of story

  pt open      [id]                                                       # open a story in the browser

  pt assign    [id] <owner>                                               # assign owner

  pt comment   [id] [comment]                                             # add a comment

  pt label     [id] [label]                                               # add a label

  pt estimate  [id] [0-3]                                                 # estimate a story in points scale

  pt (start,finish,deliver,accept)     [id]                               # mark a story as started

  pt reject    [id] [reason]                                              # mark a story as rejected, explaining why

  pt done      [id]  <0-3> <comment>                                      # lazy mans finish story, opens, assigns to you, estimates, finish & delivers

  pt find      [query]                                                    # looks in your stories by title and presents it

  pt list      [owner]                                                    # list all stories for another pt user

  pt list      all                                                        # list all stories for all users

  pt updates                                                              # shows number recent activity from your current project

  pt recent                                                               # shows stories you've recently shown or commented on with pt

  All commands can be run entirely without arguments for a wizard based UI. Otherwise [required] <optional>.
  Anything that takes an id will also take the num (index) from the pt command.
  HELP
  puts(help)
end

#labelObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pt/ui.rb', line 76

def label

  task = get_task_from_params "Please select a story to show"
  unless task
    message("No matches found for '#{@params[0]}', please use a valid pivotal story Id")
    return
  end

  if @params[1]
    label = @params[1]
  else
    label = ask("Which label?")
  end

  @client.add_label( @project, task, label );
  show_task(task_by_id_or_pt_id(task.id))
end

#listObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pt/ui.rb', line 51

def list
  if @params[0]
    if @params[0] == "all"
      stories = @client.get_work(@project)
      TasksTable.new(stories).print @global_config
    else
      stories = @client.get_my_work(@project, @params[0])
      TasksTable.new(stories).print @global_config
    end
  else
    members = @client.get_members(@project)
    table = MembersTable.new(members)
    user = select("Please select a member to see his tasks.", table).name
    title("Work for #{user} in #{project_to_s}")
    stories = @client.get_my_work(@project, user)
    TasksTable.new(stories).print @global_config
  end
end

#my_workObject



24
25
26
27
28
# File 'lib/pt/ui.rb', line 24

def my_work
  title("My Work for #{user_s} in #{project_to_s}")
  stories = @client.get_my_work(@project, @local_config[:user_name])
  TasksTable.new(stories).print @global_config
end

#openObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/pt/ui.rb', line 145

def open
  if @params[0]
    if task = @client.get_task_by_id(@project, @params[0])
      congrats("Opening #{task.name}")
      `open #{task.url}`
    else
      message("Story ##{@params[0]} not found")
    end
  else
    tasks = @client.get_my_open_tasks(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to open it in the browser", table)
  end
end

#recentObject



70
71
72
73
74
# File 'lib/pt/ui.rb', line 70

def recent
  title("Your recent stories from #{project_to_s}")
  stories = @project.stories( ids: @local_config[:recent_tasks].join(',') )
  MultiUserTasksTable.new(stories).print @global_config
end

#rejectObject

TODO implement story notes and comment



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/pt/ui.rb', line 319

def reject
  title("Tasks for #{user_s} in #{project_to_s}")
  if @params[0]
    task = @client.get_story(@project, @params[0])
    title("Rejecting '#{task.name}'")
  else
    tasks = @client.get_my_tasks_to_reject(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to mark it as rejected", table)
  end

  if @params[1]
    comment = @params[1]
  else
    comment = ask("Please explain why are you rejecting the task.")
  end

  if @client.comment_task(@project, task, comment)
    result = @client.mark_task_as(@project, task, 'rejected')
    congrats("Task rejected, thanks!")
  else
    error("Ummm, something went wrong.")
  end
end

#showObject



276
277
278
279
280
281
282
283
284
# File 'lib/pt/ui.rb', line 276

def show
  title("Tasks for #{user_s} in #{project_to_s}")
  task = get_task_from_params "Please select a story to show"
  unless task
    message("No matches found for '#{@params[0]}', please use a valid pivotal story Id")
    return
  end
  show_task(task)
end

#show_condensedObject

takes a comma separated list of ids and prints the collection of tasks



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/pt/ui.rb', line 306

def show_condensed
  title("Tasks for #{user_s} in #{project_to_s}")
  tasks = []
  if @params[0]
    @params[0].each_line(',') do |line|
      tasks << @client.get_task_by_id(@project, line.to_i)
    end
    table = TasksTable.new(tasks)
    table.print
  end
end

#startObject



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/pt/ui.rb', line 222

def start
  if @params[0]
    task = task_by_id_or_pt_id @params[0].to_i
    title("Starting '#{task.name}'")
  else
    tasks = @client.get_my_tasks_to_start(@project, @local_config[:user_name])
    table = TasksTable.new(tasks)
    title("Tasks for #{user_s} in #{project_to_s}")
    task = select("Please select a story to mark it as started", table)
  end
  start_task task
end

#start_task(task) ⇒ Object



389
390
391
392
# File 'lib/pt/ui.rb', line 389

def start_task task
  @client.mark_task_as(@project, task, 'started')
  congrats("Task started, go for it!")
end

#tasksObject



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/pt/ui.rb', line 286

def tasks
  title("Open story tasks for #{user_s} in #{project_to_s}")

  unless story = get_task_from_params( "Please select a story to show pending tasks" )
    message("No matches found for '#{@params[0]}', please use a valid pivotal story Id")
    return
  end

  story_task = get_open_story_task_from_params(story)

  if story_task.position == -1
    description = ask('Title for new task')
    story.create_task(:description => description)
    congrats("New todo task added to \"#{story.name}\"")
  else
    edit_story_task story_task
  end
end

#todoObject



30
31
32
33
34
35
# File 'lib/pt/ui.rb', line 30

def todo
  title("My Work for #{user_s} in #{project_to_s}")
  stories = @client.get_my_work(@project, @local_config[:user_name])
  stories = stories.select { |story| story.current_state == "unscheduled" }
  TasksTable.new(stories).print @global_config
end

#updatesObject



430
431
432
433
434
435
436
437
# File 'lib/pt/ui.rb', line 430

def updates
  activities = @client.get_activities(@project, @params[0])
  tasks = @client.get_my_work(@project, @local_config[:user_name])
  title("Recent Activity on #{project_to_s}")
  activities.each do |activity|
    show_activity(activity, tasks)
  end
end