Class: Githuh::CLI::Commands::Issue::Export

Inherits:
Base
  • Object
show all
Defined in:
lib/githuh/cli/commands/issue/export.rb

Direct Known Subclasses

ExportPaginated

Constant Summary collapse

FORMATS =
{
  json: "json",
  csv:  "csv",
}.freeze
DEFAULT_FORMAT =
:csv
DEFAULT_OUTPUT_FORMAT =
"<username>.<repo>.issues.<format>"
CSV_MAP =
{
  "Labels" => ->(_client, issue) { issue_labels(issue).reject { |l| LabelEstimates.key?(l) }.join(",").downcase },
  "Type" => ->(*) { "feature" },
  "Estimate" => ->(_client, issue) do
    el = issue_labels(issue).find { |l| LabelEstimates.key?(l) }
    el ? LabelEstimates[el] : nil
  end,
  "Current State" => ->(*) { "unstarted" },
  "Requested By" => ->(client, issue) do
    find_user(client, issue.user.)
  end,
  "Owned By" => ->(client, issue) do
    find_user(client, issue.user.)
  end,
  "Description" => ->(_client, issue) {
    issue.body
  },
  "Created at" => ->(_client, issue) { issue.created_at },
}.freeze
CSV_HEADER =
%w(Id Title Labels Type Estimate) +
                       ["Current State", "Created at", "Accepted at", "Deadline", "Requested By",
"Owned By", "Description", "Comment", "Comment", "Comment", "Comment"].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#box, #context, #info, #per_page, #token, #verbose

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#client, inherited

Instance Attribute Details

#fileObject

Returns the value of attribute file.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def file
  @file
end

#filenameObject

Returns the value of attribute filename.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def filename
  @filename
end

#formatObject

Returns the value of attribute format.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def format
  @format
end

#issuesObject

Returns the value of attribute issues.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def issues
  @issues
end

#mappingObject

Returns the value of attribute mapping.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def mapping
  @mapping
end

#outputObject

Returns the value of attribute output.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def output
  @output
end

#record_countObject

Returns the value of attribute record_count.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def record_count
  @record_count
end

#repoObject

Returns the value of attribute repo.



28
29
30
# File 'lib/githuh/cli/commands/issue/export.rb', line 28

def repo
  @repo
end

Class Method Details

.find_user(client, username) ⇒ Object



101
102
103
104
# File 'lib/githuh/cli/commands/issue/export.rb', line 101

def self.find_user(client, username)
  @user_cache ||= {}
  @user_cache[username] ||= client.user(username).name
end

.issue_labels(issue) ⇒ Object



97
98
99
# File 'lib/githuh/cli/commands/issue/export.rb', line 97

def self.issue_labels(issue)
  issue.labels.map(&:name)
end

Instance Method Details

#bar_sizeObject



89
90
91
# File 'lib/githuh/cli/commands/issue/export.rb', line 89

def bar_size
  record_count + 1
end

#call(repo: nil, file: nil, format: nil, mapping: nil) ⇒ Object



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
# File 'lib/githuh/cli/commands/issue/export.rb', line 38

def call(repo: nil, file: nil, format: nil, mapping: nil, **)
  super(**)

  self.record_count = 0
  self.repo = repo

  raise ArgumentError, "argument <repo> is required" unless repo
  raise ArgumentError, "argument <repo> is not a repository, expected eg 'rails/rails'" unless repo =~ %r{/}

  self.mapping = {}
  if mapping && ::File.exist?(mapping)
    self.mapping = ::YAML.safe_load_file(mapping)['label-to-estimates'] || {}
  end

  Export.send(:remove_const, :LabelEstimates) if Export.const_defined?(:LabelEstimates)
  Export.const_set(:LabelEstimates, self.mapping)

  self.issues = []
  self.output = StringIO.new
  self.format = (format || DEFAULT_FORMAT).to_sym

  self.filename = file || file_name(repo)
  self.file = File.open(filename, "w")

  print_summary

  raise ArgumentError, "Format is not provided" unless FORMATS.key?(format&.to_sym)

  # —————————— actually get all issues ———————————————
  self.file.write send("render_as_#{format}", fetch_issues)
  # ————————————————————————————————————————————————————————

  print_conclusion
ensure
  file.close if file.respond_to?(:close) && !file.closed?
end

#default_optionsObject



93
94
95
# File 'lib/githuh/cli/commands/issue/export.rb', line 93

def default_options
  { state: "open" }
end

#fetch_issuesObject



75
76
77
78
79
80
81
# File 'lib/githuh/cli/commands/issue/export.rb', line 75

def fetch_issues
  client.auto_paginate = true
  self.issues = filter_issues(client.issues(repo, query: default_options)).tap do |issue_list|
    self.record_count = issue_list.size
    bar("Issues")&.advance
  end
end

#filter_issues(issues_list) ⇒ Object



83
84
85
86
87
# File 'lib/githuh/cli/commands/issue/export.rb', line 83

def filter_issues(issues_list)
  issues_list.reject do |issue|
    issue.html_url =~ /pull/
  end
end

#render_as_csv(issue_list) ⇒ Object

Id,Title,Labels,Type,Estimate,Current State,Created at,Accepted at,Deadline,Requested By,Owned By,Description,Comment,Comment 100, existing started story,“label one,label two”,feature,1,started,“Nov 22, 2007”,,,user1,user2,this will update story 100,, ,new story,label one,feature,-1,unscheduled,,,,user1,,this will create a new story in the icebox,comment1,comment2



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/githuh/cli/commands/issue/export.rb', line 133

def render_as_csv(issue_list)
  # puts "rendering issues as CVS:"
  # pp issue_list
  ::CSV.generate do |csv|
    csv << CSV_HEADER
    issue_list.each do |issue|
      row = []
      CSV_HEADER.each do |column|
        method = column.downcase.underscore.to_sym
        value = if CSV_MAP[column]
                  CSV_MAP[column][client, issue]
                else
                  begin
                    issue.to_h[method]
                  rescue StandardError
                    nil
                  end
                end
        value = value.strip if value.is_a?(String)
        row << value
      end
      csv << row
      bar&.advance
    end
    bar.finish
  end
end

#render_as_json(issue_list) ⇒ Object



161
162
163
# File 'lib/githuh/cli/commands/issue/export.rb', line 161

def render_as_json(issue_list)
  JSON.pretty_generate(issue_list.map(&:to_h))
end