Module: Gitlab::Artifacts::Cleaner

Extended by:
Cleaner
Included in:
Cleaner
Defined in:
lib/gitlab/artifacts/cleaner.rb,
lib/gitlab/artifacts/cleaner/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#delete_artifact_api(job_id) ⇒ Object



70
71
72
# File 'lib/gitlab/artifacts/cleaner.rb', line 70

def delete_artifact_api(job_id)
  "#{@api_route}/jobs/#{job_id}/artifacts"
end

#delete_job_artifact!(job_id) ⇒ Object



52
53
54
55
56
# File 'lib/gitlab/artifacts/cleaner.rb', line 52

def delete_job_artifact!(job_id)
  ::HTTParty.delete(delete_artifact_api(job_id), headers: headers).tap do |response|
    raise "Failed to delete job artifact: #{response}" unless response.code == 204
  end
end

#done?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/gitlab/artifacts/cleaner.rb', line 34

def done?
  return false unless @total_page

  @current_page > @total_page
end

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/artifacts/cleaner.rb', line 9

def execute
  @api_route, @priavte_token, @older_than = ARGV
  @current_page = 1
  @older_than = Time.parse(@older_than)
  @total_count = 0

  raise 'Insufficient arguments' unless @api_route && @priavte_token && @older_than

  while jobs = get_job_list!
    puts "@current_page: #{@current_page}"

    old_jobs = jobs.select { |job| Time.parse(job['created_at']) < @older_than }

    old_jobs.each do |job|
      job_id = job['id']
      puts "job_id: #{job_id} message: deleting job artifacts"
      delete_job_artifact!(job_id)
      puts "job_id: #{job_id} message: deleted job artifacts"
      @total_count += 1
    end
  end

  puts "DONE. @total_count: #{@total_count}"
end

#get_job_list!Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/artifacts/cleaner.rb', line 40

def get_job_list!
  response = ::HTTParty.get(job_index_api, headers: headers, query: query)

  raise "Failed to get job list: #{response}" unless response.code == 200

  return nil if response.empty?

  response
ensure
  @current_page += 1
end

#headersObject



58
59
60
# File 'lib/gitlab/artifacts/cleaner.rb', line 58

def headers
  { 'Private-token' => @priavte_token }
end

#job_index_apiObject



66
67
68
# File 'lib/gitlab/artifacts/cleaner.rb', line 66

def job_index_api
  "#{@api_route}/jobs"
end

#queryObject



62
63
64
# File 'lib/gitlab/artifacts/cleaner.rb', line 62

def query
  { page: @current_page, per_page: 100 }
end