Class: Commenter::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/commenter/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


328
329
330
# File 'lib/commenter/cli.rb', line 328

def self.exit_on_failure?
  true
end

Instance Method Details

#diff(before_yaml, after_yaml) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/commenter/cli.rb', line 71

def diff(before_yaml, after_yaml)
  before_sheet = CommentSheet.from_yaml(File.read(before_yaml))
  after_sheet = CommentSheet.from_yaml(File.read(after_yaml))

  report = if options[:format] == "yaml"
             BallotDiff.diff(before_sheet, after_sheet).to_yaml
           else
             BallotDiff.to_markdown(before_sheet, after_sheet)
           end

  if options[:output]
    File.write(options[:output], report)
    puts "Wrote comparison to #{options[:output]}"
  else
    puts report
  end
end

#export(input_yaml) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/commenter/cli.rb', line 119

def export(input_yaml)
  retriever = GitHubIssueRetriever.new(options[:config])
  retriever.retrieve_observations_from_yaml(input_yaml, include_open: options[:include_open])

  comment_sheet = CommentSheet.from_yaml(File.read(input_yaml))
  raise "No comments found in YAML file" if comment_sheet.comments.empty?

  template_path = options[:template] || File.join(__dir__, "../../data/iso_comment_template_2012-03.docx")
  fill_options = options.merge(
    date: comment_sheet.date, document: comment_sheet.document, project: comment_sheet.project
  ).compact
  Filler.new.fill(template_path, options[:output], comment_sheet.comments, fill_options)
  puts "Retrieved observations and wrote disposition sheet to #{options[:output]}"
end

#fill(input_yaml) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/commenter/cli.rb', line 93

def fill(input_yaml)
  output_docx = options[:output]

  comment_sheet = CommentSheet.from_yaml(File.read(input_yaml))
  comments = comment_sheet.comments
  raise "No comments found in YAML file" if comments.empty?

  # Use default template if none specified
  template_path = options[:template] || File.join(__dir__, "../../data/iso_comment_template_2012-03.docx")

  # Fill the template, including the sheet metadata in the page header
  fill_options = options.merge(
    date: comment_sheet.date,
    document: comment_sheet.document,
    project: comment_sheet.project
  ).compact
  Filler.new.fill(template_path, output_docx, comments, fill_options)
  puts "Filled template to #{output_docx}"
end

#github_create(input_yaml) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/commenter/cli.rb', line 143

def github_create(input_yaml)
  creator = GitHubIssueCreator.new(
    options[:config],
    options[:title_template],
    options[:body_template]
  )

  github_options = {
    stage: options[:stage],
    milestone: options[:milestone],
    assignee: options[:assignee],
    dry_run: options[:dry_run],
    output: options[:output]
  }.compact

  results = creator.create_issues_from_yaml(input_yaml, github_options)

  if options[:dry_run]
    puts "DRY RUN - Preview of issues to be created:"
    puts "=" * 50
    results.each do |result|
      puts "\nComment ID: #{result[:comment_id]}"
      puts "Title: #{result[:title]}"
      puts "Labels: #{result[:labels].join(", ")}" if result[:labels]&.any?
      puts "Assignees: #{result[:assignees].join(", ")}" if result[:assignees]&.any?
      puts "Milestone: #{result[:milestone]}" if result[:milestone]
      puts "\nBody preview (first 200 chars):"
      puts result[:body][0...200] + (result[:body].length > 200 ? "..." : "")
      puts "-" * 30
    end
  else
    puts "GitHub issue creation results:"
    puts "=" * 40

    created_count = 0
    skipped_count = 0
    error_count = 0

    results.each do |result|
      case result[:status]
      when :created
        created_count += 1
        puts "#{result[:comment_id]}: Created issue ##{result[:issue_number]}"
        puts "  URL: #{result[:issue_url]}"
      when :skipped
        skipped_count += 1
        puts "- #{result[:comment_id]}: Skipped (#{result[:message]})"
        puts "  URL: #{result[:issue_url]}" if result[:issue_url]
      when :error
        error_count += 1
        puts "#{result[:comment_id]}: Error - #{result[:message]}"
      end
    end

    puts "\nSummary:"
    puts "Created: #{created_count}, Skipped: #{skipped_count}, Errors: #{error_count}"
  end
rescue StandardError => e
  puts "Error: #{e.message}"
  exit 1
end

#github_retrieve(input_yaml) ⇒ Object



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
318
319
320
321
322
323
324
325
326
# File 'lib/commenter/cli.rb', line 270

def github_retrieve(input_yaml)
  retriever = GitHubIssueRetriever.new(options[:config])

  retrieve_options = {
    output: options[:output],
    include_open: options[:include_open],
    dry_run: options[:dry_run]
  }.compact

  results = retriever.retrieve_observations_from_yaml(input_yaml, retrieve_options)

  if options[:dry_run]
    puts "DRY RUN - Preview of observations to be retrieved:"
    puts "=" * 50
    results.each do |result|
      puts "\nComment ID: #{result[:comment_id]}"
      puts "Issue ##{result[:issue_number]}: #{result[:status]}"
      if result[:observation]
        puts "Observation preview (first 200 chars):"
        puts result[:observation][0...200] + (result[:observation].length > 200 ? "..." : "")
      else
        puts "No observation found"
      end
      puts "-" * 30
    end
  else
    puts "GitHub observation retrieval results:"
    puts "=" * 40

    retrieved_count = 0
    skipped_count = 0
    error_count = 0

    results.each do |result|
      case result[:status]
      when :retrieved
        retrieved_count += 1
        puts "#{result[:comment_id]}: Retrieved observation from issue ##{result[:issue_number]}"
      when :skipped
        skipped_count += 1
        puts "- #{result[:comment_id]}: Skipped (#{result[:message]})"
      when :error
        error_count += 1
        puts "#{result[:comment_id]}: Error - #{result[:message]}"
      end
    end

    puts "\nSummary:"
    puts "Retrieved: #{retrieved_count}, Skipped: #{skipped_count}, Errors: #{error_count}"

    output_file = options[:output] || input_yaml
    puts "Updated YAML file: #{output_file}"
  end
rescue StandardError => e
  puts "Error: #{e.message}"
  exit 1
end

#github_sync(input_yaml) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/commenter/cli.rb', line 220

def github_sync(input_yaml)
  sync = GitHubSync.new(options[:config], options[:title_template], options[:body_template])

  sync_options = {
    stage: options[:stage],
    milestone: options[:milestone],
    assignee: options[:assignee],
    conflict: options[:conflict],
    close_on_disposition: options[:close_on_disposition],
    dry_run: options[:dry_run],
    output: options[:output]
  }.compact
  results = sync.sync(input_yaml, sync_options)

  if options[:dry_run]
    puts "DRY RUN - Planned sync actions:"
    puts "=" * 50
    results.each do |result|
      issue = result[:issue_number] ? "issue ##{result[:issue_number]}" : "no issue yet"
      puts "#{result[:comment_id]} (#{issue}): #{result[:actions].join(", ")}"
    end
    puts "-" * 30
    puts "Based on the issue state last recorded in the YAML; run without --dry-run for a live pass."
  else
    puts "GitHub sync results:"
    puts "=" * 40
    results.each do |result|
      case result[:status]
      when :created then puts "\u2713 #{result[:comment_id]}: Created issue ##{result[:issue_number]} " \
                               "(#{result[:issue_url]})"
      when :updated then puts "\u21bb #{result[:comment_id]}: Updated issue ##{result[:issue_number]} " \
                               "(#{result[:actions].join(", ")})"
      when :closed then puts "\u2713 #{result[:comment_id]}: Closed issue ##{result[:issue_number]} (disposition recorded)"
      when :updated_and_closed then puts "\u21bb\u2713 #{result[:comment_id]}: Updated and closed issue ##{result[:issue_number]}"
      when :unchanged then puts "- #{result[:comment_id]}: Issue ##{result[:issue_number]} in sync"
      when :error then puts "\u2717 #{result[:comment_id]}: Error - #{result[:message]}"
      end
    end
    puts "Updated YAML file: #{options[:output] || input_yaml}"
  end
rescue StandardError => e
  puts "Error: #{e.message}"
  exit 1
end

#import(input_file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/commenter/cli.rb', line 23

def import(input_file)
  # Parse the input file
  comment_sheet = Parser.new.parse(input_file, options)

  schema_target = write_sheet(comment_sheet, options[:output], options[:schema_dir])

  puts "Converted #{input_file} to #{options[:output]}"
  puts "  Version: #{comment_sheet.version}"
  puts "  Comments: #{comment_sheet.comments.length}"
  puts "  Schema: #{schema_target}"
end

#merge(*input_files) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/commenter/cli.rb', line 38

def merge(*input_files)
  raise "At least one input file is required" if input_files.empty?

  sheets = input_files.map { |path| CommentSheet.from_yaml(File.read(path)) }
  comment_sheet = Ballot.new(sheets).merge
  write_sheet(comment_sheet, options[:output], options[:schema_dir])

  puts "Merged #{input_files.length} files (#{comment_sheet.comments.length} comments) to #{options[:output]}"
end

#stats(input_yaml) ⇒ Object



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

def stats(input_yaml)
  comment_sheet = CommentSheet.from_yaml(File.read(input_yaml))

  report = if options[:format] == "yaml"
             BallotReport.counts(comment_sheet).to_yaml
           else
             BallotReport.to_markdown(comment_sheet)
           end

  if options[:output]
    File.write(options[:output], report)
    puts "Wrote statistics to #{options[:output]}"
  else
    puts report
  end
end