Class: Ditz::Operator

Inherits:
Object show all
Defined in:
lib/operator.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_args(project, method, args) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/operator.rb', line 49

def build_args project, method, args
  command = "command '#{method_to_op method}'"
  built_args = @operations[method][:args_spec].map do |spec|
    val = args.shift
    generate_choices(project, method, spec) if val == '<options>'
    case spec
    when :issue
      raise Error, "#{command} requires an issue name" unless val
      valr = val.sub(/\A(\w+-\d+)_.*$/,'\1')
      project.issue_for(valr) or raise Error, "no issue with name #{val}"
    when :release
      raise Error, "#{command} requires a release name" unless val
      project.release_for(val) or raise Error, "no release with name #{val}"
    when :maybe_release
      project.release_for(val) or raise Error, "no release with name #{val}" if val
    when :maybe_component
      project.component_for(val) or raise Error, "no component with name #{val}" if val
    when :magic_release
      parse_releases_arg project, val
    when :string
      raise Error, "#{command} requires a string" unless val
      val
    else
      val # no translation for other types
    end
  end
  generate_choices(project, method, nil) if args.include? '<options>'
  raise Error, "too many arguments for #{command}" unless args.empty?
  built_args
end

.generate_choices(project, method, spec) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/operator.rb', line 80

def generate_choices project, method, spec
  case spec
  when :issue
    puts project.issues.map { |i| "#{i.name}_#{i.title.gsub(/\W+/, '-')}" }
  when :release, :maybe_release
    puts project.releases.map { |r| r.name }
  end
  exit 0
end

.has_operation?(op) ⇒ Boolean

Returns:

  • (Boolean)


21
# File 'lib/operator.rb', line 21

def has_operation? op; @operations.member? op_to_method(op) end

.method_to_op(meth) ⇒ Object



10
# File 'lib/operator.rb', line 10

def method_to_op meth; meth.to_s.gsub("_", "-") end

.op_to_method(op) ⇒ Object



11
# File 'lib/operator.rb', line 11

def op_to_method op; op.gsub("-", "_").intern end

.operation(method, desc, *args_spec) ⇒ Object



13
14
15
16
# File 'lib/operator.rb', line 13

def operation method, desc, *args_spec
  @operations ||= {}
  @operations[method] = { :desc => desc, :args_spec => args_spec }
end

.operationsObject



18
19
20
# File 'lib/operator.rb', line 18

def operations
  @operations.map { |k, v| [method_to_op(k), v] }.sort_by { |k, v| k }
end

.parse_releases_arg(project, releases_arg) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/operator.rb', line 23

def parse_releases_arg project, releases_arg
  ret = []

  releases, show_unassigned, force_show = case releases_arg
    when nil; [project.releases, true, false]
    when "unassigned"; [[], true, true]
    else
      release = project.release_for(releases_arg)
      raise Error, "no release with name #{releases_arg}" unless release
      [[release], false, true]
    end

  releases.each do |r|
    next if r.released? unless force_show
    groups = project.group_issues(project.issues_for_release(r))
    #next if groups.empty? unless force_show
    ret << [r, groups]
  end

  return ret unless show_unassigned
  groups = project.group_issues(project.unassigned_issues)
  return ret if groups.empty? unless force_show
  ret << [nil, groups]
end

Instance Method Details

#actually_do_todo(project, config, releases, full) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/operator.rb', line 270

def actually_do_todo project, config, releases, full
  releases.each do |r, groups|
    if r
      puts "Version #{r.name} (#{r.status}):"
    else
      puts "Unassigned:"
    end
    issues = groups.map { |_,g| g }.flatten
    issues = issues.select { |i| i.open? } unless full
    puts(todo_list_for(issues.sort_by { |i| i.sort_order }) || "No open issues.")
    puts
  end
end

#add(project, config) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/operator.rb', line 145

def add project, config
  issue = Issue.create_interactively(:args => [config, project]) or return
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.log "created", config.user, comment
  project.add_issue issue
  project.assign_issue_names!
  puts "Added issue #{issue.name}."
end

#add_component(project, config) ⇒ Object



171
172
173
174
175
# File 'lib/operator.rb', line 171

def add_component project, config
  component = Component.create_interactively(:args => [project, config]) or return
  project.add_component component
  puts "Added component #{component.name}."
end

#add_reference(project, config, issue) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/operator.rb', line 178

def add_reference project, config, issue
  puts "Adding a reference to #{issue.name}: #{issue.title}."
  reference = ask "Reference"
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.add_reference reference
  issue.log "added reference #{issue.references.size}", config.user, comment
  puts "Added reference to #{issue.name}."
end

#add_release(project, config, maybe_name) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/operator.rb', line 161

def add_release project, config, maybe_name
  puts "Adding release #{maybe_name}." if maybe_name
  release = Release.create_interactively(:args => [project, config], :with => { :name => maybe_name }) or return
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  release.log "created", config.user, comment
  project.add_release release
  puts "Added release #{release.name}."
end

#archive(project, config, release, dir) ⇒ Object



561
562
563
564
565
566
567
568
569
570
# File 'lib/operator.rb', line 561

def archive project, config, release, dir
  dir ||= "ditz-archive-#{release.name}"
  FileUtils.mkdir dir
  FileUtils.cp project.pathname, dir
  project.issues_for_release(release).each do |i|
    FileUtils.cp i.pathname, dir
    project.drop_issue i
  end
  puts "Archived to #{dir}."
end

#assign(project, config, issue, maybe_release) ⇒ Object



342
343
344
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
# File 'lib/operator.rb', line 342

def assign project, config, issue, maybe_release
  if maybe_release && maybe_release.name == issue.release
    raise Error, "issue #{issue.name} already assigned to release #{issue.release}"
  end

  puts "Issue #{issue.name} currently " + if issue.release
    "assigned to release #{issue.release}."
  else
    "not assigned to any release."
  end

  puts "Assigning to release #{maybe_release.name}." if maybe_release

  release = maybe_release || begin
    releases = project.releases.sort_by { |r| (r.release_time || 0).to_i }
    releases -= [releases.find { |r| r.name == issue.release }] if issue.release
    ask_for_selection(releases, "release") do |r|
      r.name + if r.released?
        " (released #{r.release_time.pretty_date})"
      else
        " (unreleased)"
      end
    end
  end
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.assign_to_release release, config.user, comment
  puts "Assigned #{issue.name} to #{release.name}."
end

#changelog(project, config, r) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/operator.rb', line 435

def changelog project, config, r
  puts "== #{r.name} / #{r.released? ? r.release_time.pretty_date : 'unreleased'}"
  project.group_issues(project.issues_for_release(r)).each do |type, issues|
    issues.select { |i| i.closed? }.each do |i|
      if type == :bugfix
        puts "* #{type}: #{i.title}"
      else
        puts "* #{i.title}"
      end
    end
  end
end

#close(project, config, issue) ⇒ Object



333
334
335
336
337
338
339
# File 'lib/operator.rb', line 333

def close project, config, issue
  puts "Closing issue #{issue.name}: #{issue.title}."
  disp = ask_for_selection Issue::DISPOSITIONS, "disposition", lambda { |x| Issue::DISPOSITION_STRINGS[x] || x.to_s }
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.close disp, config.user, comment
  puts "Closed issue #{issue.name} with disposition #{issue.disposition_string}."
end

#comment(project, config, issue) ⇒ Object



407
408
409
410
411
412
413
414
415
416
# File 'lib/operator.rb', line 407

def comment project, config, issue
  puts "Commenting on issue #{issue.name}: #{issue.title}."
  comment = ask_multiline "Comments"
  if comment.blank?
    puts "Empty comment, aborted."
  else
    issue.log "commented", config.user, comment
    puts "Comments recorded for #{issue.name}."
  end
end

#do(op, project, config, args) ⇒ Object



91
92
93
94
95
# File 'lib/operator.rb', line 91

def do op, project, config, args
  meth = self.class.op_to_method(op)
  built_args = self.class.build_args project, meth, args
  send meth, project, config, *built_args
end

#drop(project, config, issue) ⇒ Object



155
156
157
158
# File 'lib/operator.rb', line 155

def drop project, config, issue
  project.drop_issue issue
  puts "Dropped #{issue.name}. Note that other issue names may have changed."
end

#edit(project, config, issue) ⇒ Object



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/operator.rb', line 573

def edit project, config, issue
  data = { :title => issue.title, :description => issue.desc,
           :reporter => issue.reporter }

  fn = run_editor { |f| f.puts data.to_yaml }

  unless fn
    puts "Aborted."
    return
  end

  comment = ask_multiline "Comments" unless $opts[:no_comment]

  begin
    edits = YAML.load_file fn
    if issue.change edits, config.user, comment
      puts "Changed recorded."
    else
      puts "No changes."
    end
  end
end

#format_log_events(events) ⇒ Object



309
310
311
312
313
314
# File 'lib/operator.rb', line 309

def format_log_events events
  return "none" if events.empty?
  events.map do |time, who, what, comment|
    "- #{time.pretty} :: #{who}\n  #{what}#{comment.multiline "  > "}"
  end.join("\n")
end

#grep(project, config, match) ⇒ Object



522
523
524
525
526
# File 'lib/operator.rb', line 522

def grep project, config, match
  re = /#{match}/
  issues = project.issues.select { |i| i.title =~ re || i.desc =~ re }
  puts(todo_list_for(issues) || "No matching issues.")
end

#help(project, config, command) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/operator.rb', line 107

def help project, config, command
  return help_single(command) if command
  puts <<EOS
Ditz commands:

EOS
  ops = self.class.operations
  len = ops.map { |name, op| name.to_s.length }.max
  ops.each do |name, opts|
    printf "  %#{len}s: %s\n", name, opts[:desc]
  end
  puts <<EOS

Use 'ditz help <command>' for details.
EOS
end

#help_single(command) ⇒ Object

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/operator.rb', line 124

def help_single command
  name, opts = self.class.operations.find { |name, spec| name == command }
  raise Error, "no such ditz command '#{command}'" unless name
  args = opts[:args_spec].map do |spec|
    case spec.to_s
    when "magic_release"
      "[release]"
    when /^maybe_(.*)$/
      "[#{$1}]"
    else
      "<#{spec.to_s}>"
    end
  end.join(" ")

  puts <<EOS
#{opts[:desc]}.
Usage: ditz #{name} #{args}
EOS
end

#html(project, config, dir) ⇒ Object



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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/operator.rb', line 449

def html project, config, dir
  dir ||= "html"
  Dir.mkdir dir unless File.exists? dir

  ## find the ERB templates. this is my brilliant approach
  ## to the 'gem datadir' problem.
  template_dir = $:.find { |p| File.exist? File.expand_path(File.join(p, "index.rhtml")) }
  raise "can't find index.rhtml in any path" unless template_dir
  template_dir = File.expand_path template_dir

  FileUtils.cp File.join(template_dir, "style.css"), dir

  ## build up links
  links = {}
  project.releases.each { |r| links[r] = "release-#{r.name}.html" }
  project.issues.each { |i| links[i] = "issue-#{i.id}.html" }
  project.components.each { |c| links[c] = "component-#{c.name}.html" }
  links["unassigned"] = "unassigned.html" # special case: unassigned
  links["index"] = "index.html" # special case: index

  project.issues.each do |issue|
    fn = File.join dir, links[issue]
    puts "Generating #{fn}..."
    File.open(fn, "w") do |f|
      f.puts ErbHtml.new(template_dir, "issue", links, :issue => issue,
        :release => (issue.release ? project.release_for(issue.release) : nil),
        :component => project.component_for(issue.component),
        :project => project)
    end
  end

  project.releases.each do |r|
    fn = File.join dir, links[r]
    puts "Generating #{fn}..."
    File.open(fn, "w") do |f|
      f.puts ErbHtml.new(template_dir, "release", links, :release => r,
        :issues => project.issues_for_release(r), :project => project)
    end
  end

  project.components.each do |c|
    fn = File.join dir, links[c]
    puts "Generating #{fn}..."
    File.open(fn, "w") do |f|
      f.puts ErbHtml.new(template_dir, "component", links, :component => c,
        :issues => project.issues_for_component(c), :project => project)
    end
  end

  fn = File.join dir, links["unassigned"]
  puts "Generating #{fn}..."
  File.open(fn, "w") do |f|
    f.puts ErbHtml.new(template_dir, "unassigned", links,
      :issues => project.unassigned_issues, :project => project)
  end

  past_rels, upcoming_rels = project.releases.partition { |r| r.released? }
  fn = File.join dir, links["index"]
  puts "Generating #{fn}..."
  File.open(fn, "w") do |f|
    f.puts ErbHtml.new(template_dir, "index", links, :project => project,
      :past_releases => past_rels, :upcoming_releases => upcoming_rels,
      :components => project.components)
  end
  puts "Local generated URL: file://#{File.expand_path(fn)}"
end

#initObject



102
103
104
# File 'lib/operator.rb', line 102

def init
  Project.create_interactively
end

#log(project, config) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/operator.rb', line 529

def log project, config
  project.issues.map { |i| i.log_events.map { |e| [e, i] } }.
    flatten_one_level.sort_by { |e| e.first.first }.reverse.
    each do |(date, author, what, comment), i|
    puts <<EOS
date  : #{date.localtime} (#{date.ago} ago)
author: #{author}
 issue: [#{i.name}] #{i.title}

#{what}
#{comment.multiline "  > ", false}
EOS
  puts unless comment.blank?
  end
end

#release(project, config, release) ⇒ Object



428
429
430
431
432
# File 'lib/operator.rb', line 428

def release project, config, release
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  release.release! project, config.user, comment
  puts "Release #{release.name} released!"
end

#releases(project, config) ⇒ Object



419
420
421
422
423
424
425
# File 'lib/operator.rb', line 419

def releases project, config
  a, b = project.releases.partition { |r| r.released? }
  (b + a.sort_by { |r| r.release_time }).each do |r|
    status = r.released? ? "released #{r.release_time.pretty_date}" : r.status
    puts "#{r.name} (#{status})"
  end
end

#set_component(project, config, issue, maybe_component) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/operator.rb', line 372

def set_component project, config, issue, maybe_component
  puts "Changing the component of issue #{issue.name}: #{issue.title}."

  if project.components.size == 1
    raise Error, "this project does not use multiple components"
  end

  if maybe_component && maybe_component.name == issue.component
    raise Error, "issue #{issue.name} already assigned to component #{issue.component}"
  end

  component = maybe_component || begin
    components = project.components
    components -= [components.find { |r| r.name == issue.component }] if issue.component
    ask_for_selection(components, "component") { |r| r.name }
  end
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.assign_to_component component, config.user, comment
  oldname = issue.name
  project.assign_issue_names!
  puts <<EOS
Issue #{oldname} is now #{issue.name}. Note that the names of other issues may
have changed as well.
EOS
end

#shortlog(project, config) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/operator.rb', line 546

def shortlog project, config
  project.issues.map { |i| i.log_events.map { |e| [e, i] } }.
    flatten_one_level.sort_by { |e| e.first.first }.reverse.
    each do |(date, author, what, comment), i|
      shortauthor = if author =~ /<(.*?)@/
        $1
      else
        author
      end[0..15]
      printf "%13s|%13s|%13s|%s\n", date.ago, i.name, shortauthor,
        what
    end
end

#show(project, config, issue) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/operator.rb', line 285

def show project, config, issue
  status = case issue.status
  when :closed
    "#{issue.status_string}: #{issue.disposition_string}"
  else
    issue.status_string
  end
  puts <<EOS
#{"Issue #{issue.name}".underline}
    Title: #{issue.title}
Description: #{issue.desc.multiline "  "}
     Type: #{issue.type}
   Status: #{status}
  Creator: #{issue.reporter}
      Age: #{issue.creation_time.ago}
  Release: #{issue.release}
 References: #{issue.references.listify "  "}
 Identifier: #{issue.id}

Event log:
#{format_log_events issue.log_events}
EOS
end

#start(project, config, issue) ⇒ Object



317
318
319
320
321
322
# File 'lib/operator.rb', line 317

def start project, config, issue
  puts "Starting work on issue #{issue.name}: #{issue.title}."
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.start_work config.user, comment
  puts "Recorded start of work for #{issue.name}."
end

#status(project, config, releases) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/operator.rb', line 188

def status project, config, releases
  if releases.empty?
    puts "No releases."
    return
  end

  ## TODO: remove weird and deprecated :maybe_release semantics
  releases = releases.map { |r, groups| r }

  entries = releases.map do |r|
    title, issues = if r
      [r.name, project.issues_for_release(r)]
    else
      ["unassigned", project.unassigned_issues]
    end

    middle = Issue::TYPES.map do |type|
      type_issues = issues.select { |i| i.type == type }
      num = type_issues.size
      nc = type_issues.count_of { |i| i.closed? }
      pc = 100.0 * (type_issues.empty? ? 1.0 : nc.to_f / num)
      "%2d/%2d %s" % [nc, num, type.to_s.pluralize(num, false)]
    end

    bar = if r && r.released?
      "(released)"
    elsif issues.empty?
      "(no issues)"
    elsif issues.all? { |i| i.closed? }
      "(ready for release)"
    else
      status_bar_for(issues)
    end

    [title, middle, bar]
  end

  title_size = 0
  middle_sizes = []

  entries.each do |title, middle, bar|
    title_size = [title_size, title.length].max
    middle_sizes = middle.zip(middle_sizes).map do |e, s|
      [s || 0, e.length].max
    end
  end

  entries.each do |title, middle, bar|
    printf "%-#{title_size}s ", title
    middle.zip(middle_sizes).each_with_index do |(e, s), i|
      sep = i < middle.size - 1 ? "," : ""
      printf "%-#{s + sep.length}s ", e + sep
    end
    puts bar
  end
end

#status_bar_for(issues) ⇒ Object



245
246
247
248
249
250
# File 'lib/operator.rb', line 245

def status_bar_for issues
  Issue::STATUS_WIDGET.
    sort_by { |k, v| -Issue::STATUS_SORT_ORDER[k] }.
    map { |k, v| v * issues.count_of { |i| i.status == k } }.
    join
end

#stop(project, config, issue) ⇒ Object



325
326
327
328
329
330
# File 'lib/operator.rb', line 325

def stop project, config, issue
  puts "Stopping work on issue #{issue.name}: #{issue.title}."
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.stop_work config.user, comment
  puts "Recorded work stop for #{issue.name}."
end

#todo(project, config, releases) ⇒ Object



261
262
263
# File 'lib/operator.rb', line 261

def todo project, config, releases
  actually_do_todo project, config, releases, false
end

#todo_full(project, config, releases) ⇒ Object



266
267
268
# File 'lib/operator.rb', line 266

def todo_full project, config, releases
  actually_do_todo project, config, releases, true
end

#todo_list_for(issues) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/operator.rb', line 252

def todo_list_for issues
  return if issues.empty?
  name_len = issues.max_of { |i| i.name.length }
  issues.map do |i|
    sprintf "%s %#{name_len}s: %s\n", i.status_widget, i.name, i.title
  end.join
end

#unassign(project, config, issue) ⇒ Object



399
400
401
402
403
404
# File 'lib/operator.rb', line 399

def unassign project, config, issue
  puts "Unassigning issue #{issue.name}: #{issue.title}."
  comment = ask_multiline "Comments" unless $opts[:no_comment]
  issue.unassign config.user, comment
  puts "Unassigned #{issue.name}."
end

#validate(project, config) ⇒ Object



517
518
519
# File 'lib/operator.rb', line 517

def validate project, config
  ## a no-op
end