Class: Ditz::Operator

Inherits:
Object show all
Defined in:
lib/ditz/operator.rb,
lib/ditz/plugins/git.rb,
lib/ditz/plugins/git-sync.rb,
lib/ditz/plugins/icalendar.rb,
lib/ditz/plugins/issue-claiming.rb,
lib/ditz/plugins/issue-labeling.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

COL_ID =
"ID"
COL_NAME =
"NAME"
COL_RELEASE =
"RELEASE"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_args(project, method, args) ⇒ Object

parse the specs, and the commandline arguments, and resolve them. does typechecking but currently doesn’t check for open_issues actually being open, unstarted_issues being unstarted, etc. probably will check for this in the future.

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
79
80
81
# File 'lib/ditz/operator.rb', line 32

def build_args project, method, args
  specs = @operations[method][:args_spec]
  command = "command '#{method_to_op method}'"

  if specs.empty? && args == ["<options>"]
    die_with_completions project, method, nil
  end

  built_args = specs.map do |spec|
    optional = spec.to_s =~ /^maybe_/
    spec = spec.to_s.gsub(/^maybe_/, "").intern # :(
    val = args.shift

    case val
    when nil
      next if optional
      specname = spec.to_s.gsub("_", " ")
      article = specname =~ /^[aeiou]/ ? "an" : "a"
      raise Error, "#{command} requires #{article} #{specname}"
    when "<options>"
      die_with_completions project, method, spec
    end

    case spec
    when :issue, :open_issue, :unstarted_issue, :started_issue, :assigned_issue
      ## issue completion sticks the title on there, so this will strip it off
      valr = val.sub(/\A(\w+-\d+)_.*$/,'\1')
      issues = project.issues_for valr
      case issues.size
      when 0; raise Error, "no issue with name #{val.inspect}"
      when 1; issues.first
      else
        raise Error, "multiple issues matching name #{val.inspect}"
      end
    when :release, :unreleased_release
      if val == "unassigned"
        :unassigned
      else
        project.release_for(val) or raise Error, "no release with name #{val}"
      end
    when :component
      project.component_for(val) or raise Error, "no component with name #{val}" if val
    else
      val # no translation for other types
    end
  end

  raise Error, "too many arguments for #{command}" unless args.empty?
  built_args
end

.build_opts(method, args) ⇒ Object



23
24
25
26
# File 'lib/ditz/operator.rb', line 23

def build_opts method, args
  options_blk = @operations[method][:options_blk]
  options_blk and Trollop.options args, &options_blk or nil
end

.die_with_completions(project, method, spec) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ditz/operator.rb', line 83

def die_with_completions project, method, spec
  puts(case spec
  when :issue, :open_issue, :unstarted_issue, :started_issue, :assigned_issue
    m = { :issue => nil,
          :open_issue => :open?,
          :unstarted_issue => :unstarted?,
          :started_issue => :in_progress?,
          :assigned_issue => :assigned?,
        }[spec]
    project.issues.select { |i| m.nil? || i.send(m) }.sort_by { |i| i.creation_time }.reverse.map { |i| "#{i.name}_#{i.title.gsub(/\W+/, '-')}" }
  when :release
    project.releases.map { |r| r.name } + ["unassigned"]
  when :unreleased_release
    project.releases.select { |r| r.unreleased? }.map { |r| r.name }
  when :component
    project.components.map { |c| c.name }
  when :command
    operations.map { |name, _| name }
  else
    ""
  end)
  exit 0
end

.has_operation?(op) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.method_to_op(meth) ⇒ Object



9
# File 'lib/ditz/operator.rb', line 9

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

.op_to_method(op) ⇒ Object



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

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

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



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

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

.operationsObject



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

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

Instance Method Details

#__issue_claiming_closeObject



93
94
95
96
97
98
# File 'lib/ditz/plugins/issue-claiming.rb', line 93

def close project, config, opts, issue
  puts "Closing issue #{issue.name}: #{issue.title}."
  disp = ask_for_selection Issue::DISPOSITIONS, "disposition", lambda { |x| Issue::DISPOSITION_STRINGS[x] || x.to_s }
  issue.close disp, config.user, get_comment(opts)
  puts "Closed issue #{issue.name} with disposition #{issue.disposition_string}."
end

#__issue_claiming_startObject



75
76
77
78
79
# File 'lib/ditz/plugins/issue-claiming.rb', line 75

def start project, config, opts, issue
  puts "Starting work on issue #{issue.name}: #{issue.title}."
  issue.start_work config.user, get_comment(opts)
  puts "Recorded start of work for #{issue.name}."
end

#__issue_claiming_stopObject



84
85
86
87
88
# File 'lib/ditz/plugins/issue-claiming.rb', line 84

def stop project, config, opts, issue
  puts "Stopping work on issue #{issue.name}: #{issue.title}."
  issue.stop_work config.user, get_comment(opts)
  puts "Recorded work stop for #{issue.name}."
end

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



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/ditz/operator.rb', line 359

def actually_do_todo project, config, releases, full
  run_pager config
  releases ||= project.unreleased_releases + [:unassigned]
  releases = [*releases]
  releases.each do |r|
    puts r == :unassigned ? "Unassigned:" : "#{r.name} (#{r.status}):"
    issues = project.issues_for_release r
    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, opts) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ditz/operator.rb', line 200

def add project, config, opts
  component = opts[:component] ||  project.components.first.name
  release = opts[:release] || (project.releases.first.name rescue nil)
  with = if opts[:quick]
    {:title => opts[:quick], :desc => '', :type => :task, :component => component,
      :reporter => config.user, :release => release}
  end
  issue = Issue.create_interactively(:args => [config, project], :with => with)
  issue or return
  comment = if opts[:comment]
    opts[:comment]
  elsif opts[:ask_for_comment]
    ask_multiline_or_editor "Comments"
  end
  issue.log "created", config.user, comment
  project.add_issue issue
  project.assign_issue_names!
  puts "Added issue #{issue.name} (#{issue.id})."
end

#add_component(project, config) ⇒ Object



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

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, opts, issue) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/ditz/operator.rb', line 255

def add_reference project, config, opts, issue
  puts "Adding a reference to #{issue.name}: #{issue.title}."
  reference = ask "Reference"
  issue.add_reference reference
  issue.log "added reference #{issue.references.size}", config.user, get_comment(opts)
  puts "Added reference to #{issue.name}."
end

#add_release(project, config, opts, maybe_name) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/ditz/operator.rb', line 236

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

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



615
616
617
618
619
620
621
622
623
624
625
# File 'lib/ditz/operator.rb', line 615

def archive project, config, opts, release
  dir = opts[: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
  project.drop_release release
  puts "Archived to #{dir}. Note that issue names may have changed."
end

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



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/ditz/operator.rb', line 412

def assign project, config, opts, 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

  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
  issue.assign_to_release release, config.user, get_comment(opts)
  puts "Assigned #{issue.name} to #{release.name}."
end

#changelog(project, config, r) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/ditz/operator.rb', line 512

def changelog project, config, r
  run_pager config
  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

#claim(project, config, opts, issue, dev = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ditz/plugins/issue-claiming.rb', line 105

def claim project, config, opts, issue, dev = nil
  if dev
    dev_full_email = project.devs ? project.devs[dev.to_sym] : nil
    raise Error, "no nickname :#{dev} has been defined in project.yaml" unless dev_full_email
  end
  dev_full_email ||= config.user
  puts "Claiming issue #{issue.name}: #{issue.title} for #{dev_full_email}."
  comment = ask_multiline_or_editor "Comments" unless $opts[:no_comment]
  issue.claim dev_full_email, comment, opts[:force]
  puts "Issue #{issue.name} marked as claimed by #{dev_full_email}"
end

#claimed(project, config, opts, releases) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ditz/plugins/issue-claiming.rb', line 149

def claimed project, config, opts, releases
  releases ||= project.unreleased_releases + [:unassigned]
  releases = [*releases]

  issues = project.issues.inject({}) do |h, i|
    r = project.release_for(i.release) || :unassigned
    if i.claimed? && (opts[:all] || i.open?) && releases.member?(r)
      (h[i.claimer] ||= []) << i
    end
    h
  end

  if issues.empty?
    puts "No issues."
  else
    run_pager config
    issues.keys.sort.each do |c|
      puts "#{c}:"
      puts todo_list_for(issues[c], :show_release => true)
      puts
    end
  end
end

#close(project, config, opts, issue) ⇒ Object



401
402
403
404
405
406
# File 'lib/ditz/operator.rb', line 401

def close project, config, opts, issue
  puts "Closing issue #{issue.name}: #{issue.title}."
  disp = ask_for_selection Issue::DISPOSITIONS, "disposition", lambda { |x| Issue::DISPOSITION_STRINGS[x] || x.to_s }
  issue.close disp, config.user, get_comment(opts)
  puts "Closed issue #{issue.name} with disposition #{issue.disposition_string}."
end

#comment(project, config, opts, issue) ⇒ Object



481
482
483
484
485
486
487
488
489
490
# File 'lib/ditz/operator.rb', line 481

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

#commit(project, config, opts, issue) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ditz/plugins/git.rb', line 129

def commit project, config, opts, issue
  opts[:edit] = true if opts[:message].nil?

  args = {
    :verbose => "--verbose",
    :all => "--all",
    :edit => "--edit",
  }.map { |k, v| opts[k] ? v : "" }.join(" ")

  comment = "# #{issue.name}: #{issue.title}"
  tag = "Ditz-issue: #{issue.id}"
  message = if opts[:message] && !opts[:edit]
    "#{opts[:message]}\n\n#{tag}"
  elsif opts[:message] && opts[:edit]
    "#{opts[:message]}\n\n#{tag}\n\n#{comment}"
  else
    "#{comment}\n\n#{tag}"
  end

  message = message.gsub("\"", "\\\"")
  exec "git commit #{args} --message=\"#{message}\""
end

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



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ditz/operator.rb', line 109

def do op, project, config, args
  meth = self.class.op_to_method(op)

  # Parse options, removing them from args
  opts = self.class.build_opts meth, args
  built_args = self.class.build_args project, meth, args

  built_args.unshift opts if opts

  send meth, project, config, *built_args
end

#drop(project, config, issue) ⇒ Object



221
222
223
224
# File 'lib/ditz/operator.rb', line 221

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

#drop_release(project, config, release) ⇒ Object



227
228
229
230
# File 'lib/ditz/operator.rb', line 227

def drop_release project, config, release
  project.drop_release release
  puts "Dropped release #{release.name}."
end

#edit(project, config, opts, issue) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/ditz/operator.rb', line 632

def edit project, config, opts, 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

  begin
    edits = YAML.load_file fn
    comment = opts[:silent] ? nil : get_comment(opts)
    if issue.change edits, config.user, comment, opts[:silent]
      puts "Change recorded."
    else
      puts "No changes."
    end
  end
end

#grep(project, config, opts, match) ⇒ Object



567
568
569
570
571
572
573
574
575
# File 'lib/ditz/operator.rb', line 567

def grep project, config, opts, match
  run_pager config
  re = Regexp.new match, opts[:ignore_case]
  issues = project.issues.select do |i|
    i.title =~ re || i.desc =~ re ||
      i.log_events.map { |time, who, what, comments| comments }.join(" ") =~ re
  end
  puts(todo_list_for(issues) || "No matching issues.")
end

#help(project, config, opts, command) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ditz/operator.rb', line 133

def help project, config, opts, command
  if opts[:cow]
    puts "MOO!"
    puts "All is well with the world now. A bit more methane though."
    return
  end
  run_pager config
  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:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ditz/operator.rb', line 156

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 /^maybe_(.*)$/
      "[#{$1}]"
    else
      "<#{spec.to_s}>"
    end
  end.join(" ")

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

#html(project, config, dir) ⇒ Object



527
528
529
530
# File 'lib/ditz/operator.rb', line 527

def html project, config, dir
  dir ||= "html"
  HtmlView.new(project, config, dir).render_all
end

#initObject



126
127
128
# File 'lib/ditz/operator.rb', line 126

def init
  Project.create_interactively
end

#label(project, config, issue, label_names) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ditz/plugins/issue-labeling.rb', line 113

def label project, config, issue, label_names
  labels = project.labels_for label_names
  puts "Adding labels #{label_names} to issue #{issue.name}: #{issue.title}."
  comment = ask_multiline_or_editor "Comments" unless $opts[:no_comment]
  begin
    issue.apply_labels labels, config.user, comment
  rescue Ditz::ModelError => e
    puts "Error: Issue #{e}"
    return
  end
  show_labels issue
end

#labeled(project, config, opts, labels, releases) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ditz/plugins/issue-labeling.rb', line 152

def labeled project, config, opts, labels, releases
  releases ||= project.unreleased_releases + [:unassigned]
  releases = [*releases]
  labels = project.labels_for labels

  issues = project.issues.select do |i|
    r = project.release_for(i.release) || :unassigned
    labels.all? { |l| i.labeled? l } && (opts[:all] || i.open?) && releases.member?(r)
  end

  if issues.empty?
    puts "No issues."
  else
    print_todo_list_by_release_for project, issues
  end
end

#list(project, config) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/ditz/operator.rb', line 542

def list project, config
  issues = project.issues
  return if issues.empty?

  run_pager config
  name_len = issues.max_of { |i| i.name.length }
  name_len = COL_NAME.length if name_len < COL_NAME.length
  release_len = project.releases.max_of { |i| i.name.length }
  release_len = COL_RELEASE.length if !release_len || release_len < COL_RELEASE.length
  s = "  #{COL_ID.ljust(40)} #{COL_NAME.ljust(name_len)} #{COL_RELEASE.ljust(release_len)} TITLE\n"
  issues.map do |i|
    s += sprintf "%s %s %-#{name_len}s %-#{release_len}s %s\n", i.status_widget, i.id, i.name, i.release, i.title
  end.join
  puts s
end

#log(project, config) ⇒ Object



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

def log project, config
  run_pager 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}
  id: #{i.id}
 issue: [#{i.name}] #{i.title}

#{what}
#{comment.gsub(/^/, "  > ") unless comment =~ /^\A\s*\z/}
EOS
  puts unless comment.blank?
  end
end

#mine(project, config, opts, releases) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ditz/plugins/issue-claiming.rb', line 130

def mine project, config, opts, releases
  releases ||= project.unreleased_releases + [:unassigned]
  releases = [*releases]

  issues = project.issues.select do |i|
    r = project.release_for(i.release) || :unassigned
    releases.member?(r) && i.claimer == config.user && (opts[:all] || i.open?)
  end
  if issues.empty?
    puts "No issues."
  else
    run_pager config
    print_todo_list_by_release_for project, issues
  end
end

#new_label(project, config, maybe_label) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/ditz/plugins/issue-labeling.rb', line 101

def new_label project, config, maybe_label
  puts "Adding label #{maybe_label}." if maybe_label
  label = Label.create_interactively(:args => [project, config], :with => { :name => maybe_label }) or return
  begin
    project.add_label label
    puts "Added label #{label.name}."
  rescue Ditz::ModelError => e
    puts "Error: Project #{e}"
  end
end


336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/ditz/operator.rb', line 336

def print_todo_list_by_release_for project, issues
  by_release = issues.inject({}) do |h, i|
    r = project.release_for(i.release) || :unassigned
    h[r] ||= []
    h[r] << i
    h
  end

  (project.releases + [:unassigned]).each do |r|
    next unless by_release.member? r
    puts r == :unassigned ? "Unassigned:" : "#{r.name} (#{r.status}):"
    print todo_list_for(by_release[r])
    puts
  end
end

#rdf(project, config, dir) ⇒ Object



533
534
535
536
# File 'lib/ditz/operator.rb', line 533

def rdf project, config, dir
  dir ||= "baetle"
  BaetleView.new(project, config, dir).render_all
end

#reconfigure(project, config) ⇒ Object



187
188
189
190
191
# File 'lib/ditz/operator.rb', line 187

def reconfigure project, config
  new_config = Config.create_interactively :defaults_from => config
  new_config.save! $opts[:config_file]
  puts "Configuration written."
end

#release(project, config, opts, release) ⇒ Object



506
507
508
509
# File 'lib/ditz/operator.rb', line 506

def release project, config, opts, release
  release.release! project, config.user, get_comment(opts)
  puts "Release #{release.name} released!"
end

#releases(project, config) ⇒ Object



493
494
495
496
497
498
499
500
# File 'lib/ditz/operator.rb', line 493

def releases project, config
  run_pager 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_branch(project, config, issue, maybe_string) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ditz/plugins/git.rb', line 102

def set_branch project, config, issue, maybe_string
  puts "Issue #{issue.name} currently " + if issue.git_branch
    "assigned to git branch #{issue.git_branch.inspect}."
  else
    "not assigned to any git branch."
  end

  branch = maybe_string || ask("Git feature branch name:")
  return unless branch

  if branch == issue.git_branch
    raise Error, "issue #{issue.name} already assigned to branch #{issue.git_branch.inspect}"
  end

  puts "Assigning to branch #{branch.inspect}."
  issue.git_branch = branch
end

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



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/ditz/operator.rb', line 442

def set_component project, config, opts, 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
  issue.assign_to_component component, config.user, get_comment(opts)
  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



597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/ditz/operator.rb', line 597

def shortlog project, config
  run_pager 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



373
374
375
# File 'lib/ditz/operator.rb', line 373

def show project, config, issue
  ScreenView.new(project, config).render_issue issue
end

#start(project, config, opts, issue) ⇒ Object



381
382
383
384
385
# File 'lib/ditz/operator.rb', line 381

def start project, config, opts, issue
  puts "Starting work on issue #{issue.name}: #{issue.title}."
  issue.start_work config.user, get_comment(opts)
  puts "Recorded start of work for #{issue.name}."
end

#status(project, config, releases) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/ditz/operator.rb', line 264

def status project, config, releases
  run_pager config
  releases ||= project.unreleased_releases + [:unassigned]

  if releases.empty?
    puts "No releases."
    return
  end

  entries = releases.map do |r|
    title, issues = (r == :unassigned ? r.to_s : r.name), project.issues_for_release(r)

    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 == :unassigned
      ""
    elsif 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



319
320
321
322
323
324
# File 'lib/ditz/operator.rb', line 319

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, opts, issue) ⇒ Object



391
392
393
394
395
# File 'lib/ditz/operator.rb', line 391

def stop project, config, opts, issue
  puts "Stopping work on issue #{issue.name}: #{issue.title}."
  issue.stop_work config.user, get_comment(opts)
  puts "Recorded work stop for #{issue.name}."
end

#sync(project, config, opts) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ditz/plugins/git-sync.rb', line 55

def sync project, config, opts
  unless config.git_sync_local_branch
    $stderr.puts "Please run ditz reconfigure and set the local and remote branch names"
    return
  end

  Dir.chdir $project_root
  puts "[in #{$project_root}]"
  sh "git add *.yaml", :force => true, :fake => opts[:dry_run]
  sh "git commit -m 'issue updates'", :force => true, :fake => opts[:dry_run]
  sh "git pull", :fake => opts[:dry_run]
  sh "git push #{config.git_sync_remote_repo} #{config.git_sync_local_branch}:#{config.git_sync_remote_branch}", :fake => opts[:dry_run]
  puts
  puts "Ditz issue state synchronized."
end

#todo(project, config, opts, releases) ⇒ Object



355
356
357
# File 'lib/ditz/operator.rb', line 355

def todo project, config, opts, releases
  actually_do_todo project, config, releases, opts[:all]
end

#todo2vtodo(todo, parent) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ditz/plugins/icalendar.rb', line 48

def todo2vtodo todo, parent
  h = {"SUMMARY" => "#{todo.title}", "UID" => "#{todo.type}-#{todo.id}"}
  h["RELATED-TO"] = parent if parent
  h["PRIORITY"] = "3" if todo.type == :bugfix
  h["PERCENT-COMPLETE"] = case todo.status
                          when :closed
                            "100"
                          when :in_progress
                            "50"
                          else
                            "0"
                          end
  return Vpim::Icalendar::Vtodo.create(h)
end

#todo_ics(project, config, releases) ⇒ Object



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/ditz/plugins/icalendar.rb', line 24

def todo_ics project, config, releases
  cal = Vpim::Icalendar.create
  releases ||= project.releases + [:unassigned]
  releases = [*releases]
  releases.each do |r|
    issues = project.issues_for_release r
    done = 0
    done = (99 * (issues.select { |i| i.closed? }).length / issues.length).to_int if issues.length != 0
    if r != :unassigned
      done = 100 if r.released?
      parent = "release-#{r.hash}"
      title = "Release #{r.name} (#{r.status})"
    else
      parent = "release-unassigned"
      title = "Unassigned"
    end
    cal.push Vpim::Icalendar::Vtodo.create("SUMMARY" => title, "UID" => parent, "PERCENT-COMPLETE" => "#{done}")
    issues.each do |i|
      cal.push todo2vtodo(i, parent)
    end
  end
  puts cal.encode
end

#todo_list_for(issues, opts = {}) ⇒ Object



326
327
328
329
330
331
332
333
334
# File 'lib/ditz/operator.rb', line 326

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

#unassign(project, config, opts, issue) ⇒ Object



471
472
473
474
475
# File 'lib/ditz/operator.rb', line 471

def unassign project, config, opts, issue
  puts "Unassigning issue #{issue.name}: #{issue.title}."
  issue.unassign config.user, get_comment(opts)
  puts "Unassigned #{issue.name}."
end

#unclaim(project, config, opts, issue) ⇒ Object



120
121
122
123
124
125
# File 'lib/ditz/plugins/issue-claiming.rb', line 120

def unclaim project, config, opts, issue
  puts "Unclaiming issue #{issue.name}: #{issue.title}."
  comment = ask_multiline_or_editor "Comments" unless $opts[:no_comment]
  issue.unclaim config.user, comment, opts[:force]
  puts "Issue #{issue.name} marked as unclaimed."
end

#unclaimed(project, config, opts, releases) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ditz/plugins/issue-claiming.rb', line 176

def unclaimed project, config, opts, releases
  releases ||= project.unreleased_releases + [:unassigned]
  releases = [*releases]

  issues = project.issues.select do |i|
    r = project.release_for(i.release) || :unassigned
    releases.member?(r) && i.claimer.nil? && (opts[:all] || i.open?)
  end
  if issues.empty?
    puts "No issues."
  else
    run_pager config
    print_todo_list_by_release_for project, issues
  end
end

#unlabel(project, config, issue, label_names) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ditz/plugins/issue-labeling.rb', line 127

def unlabel project, config, issue, label_names
  labels = if label_names
             puts "Removing #{label_names} labels from issue #{issue.name}: #{issue.title}."
             project.labels_for label_names
           else
             puts "Removing labels from issue #{issue.name}: #{issue.title}."
            nil
           end
  comment = ask_multiline_or_editor "Comments" unless $opts[:no_comment]
  issue.remove_labels labels, config.user, comment
  show_labels issue
end

#validate(project, config) ⇒ Object



559
560
561
# File 'lib/ditz/operator.rb', line 559

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