<commit_before>module Artifactory
  class Resource::Artifact < Resource::Base
    class << self
      #
      # Search for an artifact by the full or partial filename.
      #
      # @example Search for all repositories with the name "artifact"
      #   Artifact.search(name: 'artifact')
      #
      # @example Search for all artifacts named "artifact" in a specific repo
      #   Artifact.search(name: 'artifact', repos: 'libs-release-local')
      #
      # @param [Hash] options
      #   the list of options to search with
      #
      # @option options [String] :name
      #   the name of the artifact to search (it can be a regular expression)
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def search(options = {})
        params = Util.slice(options, :name, :repos)
        format_repos!(params)

        _get('/api/search/artifact', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      #
      # Search for an artifact by Maven coordinates: +Group ID+, +Artifact ID+,
      # +Version+ and +Classifier+.
      #
      # @example Search for all repositories with the given gavc
      #   Artifact.gavc_search(
      #     g: 'org.acme',
      #     a: 'artifact',
      #     v: '1.0',
      #     c: 'sources',
      #   )
      #
      # @example Search for all artifacts with the given gavc in a specific repo
      #   Artifact.gavc_search(
      #     g: 'org.acme',
      #     a: 'artifact',
      #     v: '1.0',
      #     c: 'sources',
      #     repos: 'libs-release-local',
      #   )
      #
      # @param [Hash] options
      #   the list of options to search with
      #
      # @option options [String] :g
      #   the group id to search for
      # @option options [String] :a
      #   the artifact id to search for
      # @option options [String] :v
      #   the version of the artifact to search for
      # @option options [String] :c
      #   the classifer to search for
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def gavc_search(options = {})
        params = Util.slice(options, :g, :a, :v, :c, :repos)
        format_repos!(params)

        _get('/api/search/gavc', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      #
      # Search for an artifact by the given properties. These are arbitrary
      # properties defined by the user on artifact, so the search uses a free-
      # form schema.
      #
      # @example Search for all repositories with the given properties
      #   Artifact.property_search(
      #     branch: 'master',
      #     author: 'sethvargo',
      #   )
      #
      # @example Search for all artifacts with the given gavc in a specific repo
      #   Artifact.property_search(
      #     branch: 'master',
      #     author: 'sethvargo',
      #     repos: 'libs-release-local',
      #   )
      #
      # @param [Hash] options
      #   the free-form list of options to search with
      #
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def property_search(options = {})
        params = options.dup
        format_repos!(params)

        _get('/api/search/prop', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      #
      # Search for an artifact by its checksum
      #
      # @example Search for all repositories with the given MD5 checksum
      #   Artifact.checksum_search(
      #     md5: 'abcd1234...',
      #   )
      #
      # @example Search for all artifacts with the given SHA1 checksum in a repo
      #   Artifact.checksum_search(
      #     sha1: 'abcdef123456....',
      #     repos: 'libs-release-local',
      #   )
      #
      # @param [Hash] options
      #   the list of options to search with
      #
      # @option options [String] :md5
      #   the MD5 checksum of the artifact to search for
      # @option options [String] :sha1
      #   the SHA1 checksum of the artifact to search for
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def checksum_search(options = {})
        params = Util.slice(options, :md5, :sha1, :repos)
        format_repos!(params)

        _get('/api/search/checksum', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      def from_url(url)
        from_hash(_get(url).json)
      end

      def from_hash(hash)
        new.tap do |instance|
          instance.api_path      = hash['uri']
          instance.md5           = hash['checksums']['md5']
          instance.sha1          = hash['checksums']['sha1']
          instance.created       = Time.parse(hash['created'])
          instance.download_path = hash['downloadUri']
          instance.last_modified = Time.parse(hash['lastModified'])
          instance.last_updated  = Time.parse(hash['lastUpdated'])
          instance.mime_type     = hash['mimeType']
          instance.size          = hash['size']
        end
      end

      private

      #
      # Format the repos list from the given options. This method will modify
      # the given Hash parameter!
      #
      def format_repos!(options)
        return options if options[:repos].nil? || options[:repos].empty?
        options[:repos] = Array(options[:repos]).compact.join(',')
        options<commit_msg>Add the ability to search for all versions of an artifact<commit_after>module Artifactory
  class Resource::Artifact < Resource::Base
    class << self
      #
      # Search for an artifact by the full or partial filename.
      #
      # @example Search for all repositories with the name "artifact"
      #   Artifact.search(name: 'artifact')
      #
      # @example Search for all artifacts named "artifact" in a specific repo
      #   Artifact.search(name: 'artifact', repos: 'libs-release-local')
      #
      # @param [Hash] options
      #   the list of options to search with
      #
      # @option options [String] :name
      #   the name of the artifact to search (it can be a regular expression)
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def search(options = {})
        params = Util.slice(options, :name, :repos)
        format_repos!(params)

        _get('/api/search/artifact', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      #
      # Search for an artifact by Maven coordinates: +Group ID+, +Artifact ID+,
      # +Version+ and +Classifier+.
      #
      # @example Search for all repositories with the given gavc
      #   Artifact.gavc_search(
      #     g: 'org.acme',
      #     a: 'artifact',
      #     v: '1.0',
      #     c: 'sources',
      #   )
      #
      # @example Search for all artifacts with the given gavc in a specific repo
      #   Artifact.gavc_search(
      #     g: 'org.acme',
      #     a: 'artifact',
      #     v: '1.0',
      #     c: 'sources',
      #     repos: 'libs-release-local',
      #   )
      #
      # @param [Hash] options
      #   the list of options to search with
      #
      # @option options [String] :g
      #   the group id to search for
      # @option options [String] :a
      #   the artifact id to search for
      # @option options [String] :v
      #   the version of the artifact to search for
      # @option options [String] :c
      #   the classifer to search for
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def gavc_search(options = {})
        params = Util.slice(options, :g, :a, :v, :c, :repos)
        format_repos!(params)

        _get('/api/search/gavc', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      #
      # Search for an artifact by the given properties. These are arbitrary
      # properties defined by the user on artifact, so the search uses a free-
      # form schema.
      #
      # @example Search for all repositories with the given properties
      #   Artifact.property_search(
      #     branch: 'master',
      #     author: 'sethvargo',
      #   )
      #
      # @example Search for all artifacts with the given gavc in a specific repo
      #   Artifact.property_search(
      #     branch: 'master',
      #     author: 'sethvargo',
      #     repos: 'libs-release-local',
      #   )
      #
      # @param [Hash] options
      #   the free-form list of options to search with
      #
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def property_search(options = {})
        params = options.dup
        format_repos!(params)

        _get('/api/search/prop', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      #
      # Search for an artifact by its checksum
      #
      # @example Search for all repositories with the given MD5 checksum
      #   Artifact.checksum_search(
      #     md5: 'abcd1234...',
      #   )
      #
      # @example Search for all artifacts with the given SHA1 checksum in a repo
      #   Artifact.checksum_search(
      #     sha1: 'abcdef123456....',
      #     repos: 'libs-release-local',
      #   )
      #
      # @param [Hash] options
      #   the list of options to search with
      #
      # @option options [String] :md5
      #   the MD5 checksum of the artifact to search for
      # @option options [String] :sha1
      #   the SHA1 checksum of the artifact to search for
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      # @return [Array<Resource::Artifact>]
      #   a list of artifacts that match the query
      #
      def checksum_search(options = {})
        params = Util.slice(options, :md5, :sha1, :repos)
        format_repos!(params)

        _get('/api/search/checksum', params).json['results'].map do |artifact|
          from_url(artifact['uri'])
        end
      end

      #
      # @example Get all versions of a given artifact
      #   Artifact.versions(name: 'artifact')
      # @example Get all versions of a given artifact in a specific repo
      #   Artifact.versions(name: 'artifact', repos: 'libs-release-local')
      #
      # @param [Hash] options
      #   the list of options to search with
      #
      # @option options [String] :group
      #   the
      # @option options [String] :sha1
      #   the SHA1 checksum of the artifact to search for
      # @option options [String, Array<String>] :repos
      #   the list of repos to search
      #
      def versions(options = {})
        options = Util.rename_keys(options,
          :group      => :g,
          :name       => :a,
          :version    => :v,
        )
        params = Util.slice(options, :g, :a, :v, :repos)
        format_repos!(params)

        _get('/api/search/versions', params).json['results']
      rescue Error::NotFound
        []
      end

      def from_url(url)
        from_hash(_get(url).json)
      end

      def from_hash(hash)
        new.tap do |instance|
          instance.api_path      = hash['uri']
          instance.md5           = hash['checksums']['md5']
          instance.sha1          = hash['checksums']['sha1']
          instance.created       = Time.parse(hash['created'])
          instance.download_path = hash['downloadUri']
          instance.last_modified = Time.parse(hash['lastModified'])
          instance.last_updated  = Time.parse(hash['lastUpdated'])
          instance.mime_type     = hash['mimeType']
          instance.size          = hash['size']
        end
      end

      private

      #
      # Format the repos list from the given options. This method will modify
      # the given Hash parameter!
      #
      def format_repos!(options)
        return options if options[:repos].nil? || options[:repos].empty?
        options[:repos] = Array(options[:repos]).compact.join(',')
        options
{'fraction_non_alphanumeric': 0.10842814023682378, 'fraction_numerical': 0.004488816655057658, 'mean_word_length': 1.9374857922255058, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5547829', 'n_tokens_mistral': 3876, 'n_tokens_neox': 3661, 'n_words': 1136}