Class: Gl::Global

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

Instance Method Summary collapse

Instance Method Details

#registryObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gl/cli/global.rb', line 6

def registry
  spinner = TTY::Spinner.new('[:spinner] Fetching projects ...')

  results = {}

  spinner.auto_spin
  projects = Gitlab.projects.auto_paginate
  spinner.stop("Found #{projects.count}")

  bar = TTY::ProgressBar.new(
    'processing projects [:bar] :current/:total processed (:percent, :eta remaining)',
    total: projects.size
  )

  projects.each do |project|
    bar.advance(1)
    registries = begin
      Gitlab.registry_repositories(project.id).auto_paginate
                 rescue Gitlab::Error::Forbidden
                   next
    end

    next if registries.empty?

    infos = registries.map do |registry|
      tags = Gitlab.registry_repository_tags(project.id, registry.id).auto_paginate

      next if tags.empty?

      tags = tags.map do |tag|
        Gitlab.registry_repository_tag(project.id, registry.id, tag.name)
      rescue Gitlab::Error::NotFound
        next
      end

      tags
    end

    sizes = infos.flatten.compact.map(&:total_size)

    project_size = sizes.compact.inject(0) { |sum, x| sum + x }
    results[project.path_with_namespace] = (project_size / 1024.0 / 1024.0).round(2)
  end

  bar.finish
  table = TTY::Table.new(['Project', 'Size in MB'], results.sort_by { |_key, value| -value })
  table << ['Total usage', results.values.inject(0) { |sum, x| sum + x }.round(2)]
  puts table.render(:ascii, alignments: %i[left right])
end