<commit_before>require 'excon'
require 'json'
require 'uri'

module Centurion; end

class Centurion::DockerRegistry
  OFFICIAL_URL = 'https://registry.hub.docker.com'

  def initialize(base_uri)
    @base_uri = base_uri
  end

  def digest_for_tag(repository, tag)
    path = "/v1/repositories/#{repository}/tags/#{tag}"
    uri = uri_for_repository_path(repository, path)
    $stderr.puts "GET: #{uri}"
    response = Excon.get(
      uri,
      :headers => { "Content-Type" => "application/json" }
    )
    raise response.inspect unless response.status == 200

    # This hack is stupid, and I hate it. But it works around the fact that
    # the Docker Registry will return a base JSON String, which the Ruby parser
    # refuses (possibly correctly) to handle
    JSON.load('[' + response.body + ']').first
  end

  def repository_tags(repository)
    path = "/v1/repositories/#{repository}/tags"
    uri = uri_for_repository_path(repository, path)
    $stderr.puts "GET: #{uri.inspect}"
    response = Excon.get(uri)
    raise response.inspect unless response.status == 200

    tags = JSON.load(response.body)

    # The Docker Registry API[1]  specifies a result in the format
    # { "[tag]" : "[image_id]" }. However, the official Docker registry returns a
    # result like [{ "layer": "[image_id]", "name": "[tag]" }].
    #
    # So, we need to normalize the response to what the Docker Registry API
    # specifies should be returned.
    #
    # [1]: https://docs.docker.com/v1.1/reference/api/registry_api/

    if is_official_registry?(repository)
      {}.tap do |hash|<commit_msg>Work around a bug in Docker Hub registry.<commit_after>require 'excon'
require 'json'
require 'uri'

module Centurion; end

class Centurion::DockerRegistry
  OFFICIAL_URL = 'https://registry.hub.docker.com'

  def initialize(base_uri)
    @base_uri = base_uri
  end

  def digest_for_tag(repository, tag)
    path = "/v1/repositories/#{repository}/tags/#{tag}"
    uri = uri_for_repository_path(repository, path)
    $stderr.puts "GET: #{uri}"
    response = Excon.get(
      uri,
      :headers => { "Content-Type" => "application/json" }
    )
    raise response.inspect unless response.status == 200

    # This hack is stupid, and I hate it. But it works around the fact that
    # the Docker Registry will return a base JSON String, which the Ruby parser
    # refuses (possibly correctly) to handle
    JSON.load('[' + response.body + ']').first
  end

  def repository_tags(repository)
    path = "/v1/repositories/#{repository}/tags"
    uri = uri_for_repository_path(repository, path)
    $stderr.puts "GET: #{uri.inspect}"
    # Need to workaround a bug in Docker Hub to now pass port in Host header
    response = Excon.get(uri, headers: { 'Host' => URI.parse(uri).host })
    raise response.inspect unless response.status == 200

    tags = JSON.load(response.body)

    # The Docker Registry API[1]  specifies a result in the format
    # { "[tag]" : "[image_id]" }. However, the official Docker registry returns a
    # result like [{ "layer": "[image_id]", "name": "[tag]" }].
    #
    # So, we need to normalize the response to what the Docker Registry API
    # specifies should be returned.
    #
    # [1]: https://docs.docker.com/v1.1/reference/api/registry_api/

    if is_official_registry?(repository)
      {}.tap do |hash|
{'fraction_non_alphanumeric': 0.12488795936659695, 'fraction_numerical': 0.00717060053779504, 'mean_word_length': 3.342412451361868, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6624068', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1021, 'n_words': 314}