<commit_before>require 'uri'
require 'net/https'
require 'json'

module Librarian
  module Puppet
    module Source
      class GitHubTarball
        class Repo

          attr_accessor :source, :name
          private :source=, :name=

          def initialize(source, name)
            self.source = source
            self.name = name
          end

          def versions
            data = api_call("/repos/#{source.uri}/tags")
            if data.nil?
              raise Error, "Unable to find module '#{source.uri}' on https://github.com"
            end

            all_versions = data.map { |r| r['name'] }.sort.reverse
            all_versions.reject { |version| version =~ /^v/ }.compact
          end

          def manifests
            versions.map do |version|
              Manifest.new(source, name, version)
            end
          end

          def install_version!(version, install_path)
            if environment.local? && !vendored?(source.uri, version)
              raise Error, "Could not find a local copy of #{source.uri} at #{version}."
            end

            vendor_cache(source.uri, version) unless vendored?(source.uri, version)

            cache_version_unpacked! version

            if install_path.exist?
              install_path.rmtree
            end

            unpacked_path = version_unpacked_cache_path(version).children.first
            FileUtils.cp_r(unpacked_path, install_path)
          end

          def environment
            source.environment
          end

          def cache_path<commit_msg>try to coerce semver tags into something we can work with<commit_after>require 'uri'
require 'net/https'
require 'json'

module Librarian
  module Puppet
    module Source
      class GitHubTarball
        class Repo

          attr_accessor :source, :name
          private :source=, :name=

          def initialize(source, name)
            self.source = source
            self.name = name
          end

          def versions
            data = api_call("/repos/#{source.uri}/tags")
            if data.nil?
              raise Error, "Unable to find module '#{source.uri}' on https://github.com"
            end

            all_versions = data.map { |r| r['name'] }.sort.reverse

            all_versions = all_versions.map do |version|
              version.gsub(/^v/, '')
            end

            all_versions.delete_if do |version|
              version !~ /\A\d\.\d(\.\d.*)?\z/
            end

            all_versions.compact
          end

          def manifests
            versions.map do |version|
              Manifest.new(source, name, version)
            end
          end

          def install_version!(version, install_path)
            if environment.local? && !vendored?(source.uri, version)
              raise Error, "Could not find a local copy of #{source.uri} at #{version}."
            end

            vendor_cache(source.uri, version) unless vendored?(source.uri, version)

            cache_version_unpacked! version

            if install_path.exist?
              install_path.rmtree
            end

            unpacked_path = version_unpacked_cache_path(version).children.first
            FileUtils.cp_r(unpacked_path, install_path)
          end

          def environment
            source.environment
          end

          def cache_path
{'fraction_non_alphanumeric': 0.08519511468573131, 'fraction_numerical': 0.0, 'mean_word_length': 1.61730319563523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4254109', 'n_tokens_mistral': 904, 'n_tokens_neox': 870, 'n_words': 235}