Class: Githuh::CLI::Commands::Repo::List

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

Constant Summary collapse

FORMATS =
{
  markdown: 'md',
  json:     'json'
}.freeze
DEFAULT_FORMAT =
:markdown
DEFAULT_OUTPUT_FORMAT =
"<username>.repositories.<format>"
FORK_OPTIONS =
%w(exclude include only).freeze

Instance Attribute Summary collapse

Attributes inherited from Base

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

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/repo/list.rb', line 28

def file
  @file
end

#filenameObject

Returns the value of attribute filename.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def filename
  @filename
end

#forksObject

Returns the value of attribute forks.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def forks
  @forks
end

#formatObject

Returns the value of attribute format.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def format
  @format
end

#llm_adapterObject

Returns the value of attribute llm_adapter.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def llm_adapter
  @llm_adapter
end

#outputObject

Returns the value of attribute output.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def output
  @output
end

#privateObject

Returns the value of attribute private.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def private
  @private
end

#record_countObject

Returns the value of attribute record_count.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def record_count
  @record_count
end

#reposObject

Returns the value of attribute repos.



28
29
30
# File 'lib/githuh/cli/commands/repo/list.rb', line 28

def repos
  @repos
end

Instance Method Details

#announce_llm(adapter) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/githuh/cli/commands/repo/list.rb', line 223

def announce_llm(adapter)
  provider = adapter.class.name.split('::').last
  model    = adapter.class.const_defined?(:MODEL) ? adapter.class::MODEL : 'n/a'

  puts
  puts TTY::Box.info(
    "LLM summaries: ENABLED\n" \
    "Provider     : #{provider}\n" \
    "Model        : #{model}\n" \
    "\n" \
    "For each repository, the README will be fetched and summarized\n" \
    "into a 5-6 sentence description before writing to the output file.",
    width: ui_width, padding: 1
  )
  puts
end

#bar_sizeObject



127
128
129
130
131
# File 'lib/githuh/cli/commands/repo/list.rb', line 127

def bar_size
  return 1 if client&.last_response.nil?

  client&.last_response&.rels&.[](:last)&.href&.match(/page=(\d+).*$/)&.[](1)&.to_i # rubocop:disable Style/SafeNavigationChainLength
end

#build_llm_adapterObject

Raises:



215
216
217
218
219
220
221
# File 'lib/githuh/cli/commands/repo/list.rb', line 215

def build_llm_adapter
  adapter = Githuh::LLM.build
  raise Githuh::LLM::Error, '--llm was specified but neither ANTHROPIC_API_KEY nor OPENAI_API_KEY is set' unless adapter

  announce_llm(adapter) if info
  adapter
end

#build_llm_progress_bar(total) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/githuh/cli/commands/repo/list.rb', line 151

def build_llm_progress_bar(total)
  return unless info || verbose

  color = llm_adapter.class.const_defined?(:BAR_COLOR) ? llm_adapter.class::BAR_COLOR : :cyan
  provider = llm_adapter.class.name.split('::').last

  puts
  puts " • Summarizing #{total} READMEs with #{provider}".send(color)
  TTY::ProgressBar.new("[:bar]",
                       title:    'LLM Summaries',
                       total:    total,
                       width:    ui_width - 2,
                       head:     '',
                       complete: ''.send(color))
end

#call(file: nil, format: nil, forks: nil, private: nil, llm: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/githuh/cli/commands/repo/list.rb', line 62

def call(file: nil, format: nil, forks: nil, private: nil, llm: false, **)
  super(**)

  self.record_count = 0
  self.forks        = forks
  self.private      = private
  self.repos        = []
  self.output       = StringIO.new
  self.format       = (format || DEFAULT_FORMAT).to_sym
  self.llm_adapter  = build_llm_adapter if llm

  self.filename = file || "#{.}.repositories.#{FORMATS[self.format]}"
  self.file     = File.open(filename, 'w')

  puts
  puts TTY::Box.info("Format : #{self.format}\n" \
                     "File   : #{filename}\n" \
                     "Forks  : #{self.forks}\n",
                     width:   ui_width,
                     padding: 1)
  puts
  # —————————— actually get all repositories ———————————————
  self.file.write send("render_as_#{format}", repositories)
  # ————————————————————————————————————————————————————————

  puts
  puts TTY::Box.info("Success: written a total of #{record_count} records to #{filename}",
                     width: ui_width, padding: 1)
  puts
ensure
  file.close if file.respond_to?(:close) && !file.closed?
end

#describe(repo) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/githuh/cli/commands/repo/list.rb', line 186

def describe(repo)
  return repo.description unless llm_adapter

  readme = fetch_readme(repo)
  return repo.description if readme.nil? || readme.empty?

  llm_adapter.summarize(readme)
rescue StandardError => e
  warn "LLM summary failed for #{repo_full_name(repo)}: #{e.message}" if verbose
  repo.description
end

#fetch_readme(repo) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/githuh/cli/commands/repo/list.rb', line 198

def fetch_readme(repo)
  readme = client.readme(repo_full_name(repo))
  return nil unless readme

  encoded = readme.respond_to?(:content) ? readme.content : readme[:content]
  return nil if encoded.nil? || encoded.to_s.empty?

  Base64.decode64(encoded).force_encoding('UTF-8')
rescue StandardError => e
  warn "README fetch failed for #{repo_full_name(repo)}: #{e.message}" if verbose
  nil
end

#render_as_json(repositories) ⇒ Object



167
168
169
# File 'lib/githuh/cli/commands/repo/list.rb', line 167

def render_as_json(repositories)
  JSON.pretty_generate(repositories.map(&:to_hash))
end

#render_as_markdown(repositories) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/githuh/cli/commands/repo/list.rb', line 133

def render_as_markdown(repositories)
  output.puts "### #{client.user.name}'s Repos\n"

  llm_bar = build_llm_progress_bar(repositories.size) if llm_adapter

  repositories.each_with_index do |repo, index|
    output.puts repo_as_markdown(index, repo)
    llm_bar&.advance
  end

  if llm_bar
    llm_bar.finish
    puts
  end

  output.string
end

#repo_as_markdown(index, repo) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/githuh/cli/commands/repo/list.rb', line 171

def repo_as_markdown(index, repo)
  description = describe(repo)

  <<~REPO

    ### #{index + 1}. [#{repo.name}](#{repo.url}) (#{repo.stargazers_count} ★)

    #{"**#{repo.language}**. " if repo.language}
    #{"Distributed under the **#{repo.license.name}** license." if repo.license}

    #{description}

  REPO
end

#repo_full_name(repo) ⇒ Object



211
212
213
# File 'lib/githuh/cli/commands/repo/list.rb', line 211

def repo_full_name(repo)
  repo.respond_to?(:full_name) && repo.full_name ? repo.full_name : repo.name
end

#repositoriesObject



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
# File 'lib/githuh/cli/commands/repo/list.rb', line 95

def repositories
  page = 0
  bar  = nil

  [].tap do |repo_list|
    loop do
      options = {
        page:     page,
        per_page: per_page,
        type:     :owner,
      }

      result = client.repos({}, query: options)
      bar('Repositories')&.advance

      filter_result!(result)

      break if result.empty?

      result.each { |repo| printf "%s\n", repo.name } if verbose

      repo_list << result

      page += 1

      self.record_count += result.size
    end

    bar&.finish; puts
  end.flatten.sort_by(&:stargazers_count).reverse.uniq(&:name)
end