module Gitlab::Artifacts::Cleaner

Constants

VERSION

Public Instance Methods

delete_artifact_api(job_id) click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 81
def delete_artifact_api(job_id)
  "#{@api_route}/jobs/#{job_id}/artifacts"
end
delete_job_artifact!(job_id) click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 63
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?() click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 37
def done?
  return false unless @total_page

  @current_page > @total_page
end
execute() click to toggle source
# 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 = safe_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']
      begin
        delete_job_artifact!(job_id)
        puts "job_id: #{job_id} message: deleted job artifacts"
        @total_count += 1
      rescue => e
        puts "job_id: #{job_id} message: #{e.message}"
      end
    end
  end

  puts "DONE. @total_count: #{@total_count}"
end
get_job_list!() click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 51
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
headers() click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 69
def headers
  { 'Private-token' => @priavte_token }
end
job_index_api() click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 77
def job_index_api
  "#{@api_route}/jobs"
end
query() click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 73
def query
  { page: @current_page, per_page: 100 }
end
safe_get_job_list() click to toggle source
# File lib/gitlab/artifacts/cleaner.rb, line 43
def safe_get_job_list
  retries ||= 0
  get_job_list!
rescue => e
  sleep 10
  retry if (retries += 1) < 10
end