Class: Resource::Artifact

Inherits:
Object
  • Object
show all
Defined in:
lib/packaging/artifactory.rb

Class Method Summary collapse

Class Method Details

.pattern_search(options = {}) ⇒ Array<Resource::Artifact>

Search for an artifact in a repo using an Ant-like pattern. Unlike many Artifactory searches, this one is restricted to a single repository.

Examples:

Search in a repository named 'foo_local' for an artifact in a directory containing

the word "recent", named "artifact[0-9].txt"
Artifact.pattern_search(pattern: '*recent*/artifact[0-9].txt',
                        repo: 'foo_local')

Parameters:

  • options (Hash) (defaults to: {}) —

    A hash of options, as follows:

Options Hash (options):

  • :client (Artifactory::Client) —

    the client object to make the request with

  • :pattern (String) —

    the Ant-like pattern to use for finding artifacts within the repos. Note that the Ant pattern '**' is barred in this case by JFrog.

  • :repo (String) —

    the repo to search

Returns:

  • (Array<Resource::Artifact>) —

    a list of artifacts that match the query



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/packaging/artifactory.rb', line 38

def self.pattern_search(options = {})
  client = extract_client!(options)
  params = Util.slice(options, :pattern, :repo)
  pattern_search_parameter = { :pattern => "#{params[:repo]}:#{params[:pattern]}" }
  response = client.get('/api/search/pattern', pattern_search_parameter)
  return [] if response['files'].nil? || response['files'].empty?

  # A typical response:
  # {
  #  "repoUri"=>"https:<artifactory endpoint>/<repo>",
  #  "sourcePattern"=>"<repo>:<provided search pattern>",
  #  "files"=>[<filename that matched pattern>, ...]
  # }
  #
  # Inserting '/api/storage' before the repo makes the 'from_url' call work correctly.
  #
  repo_uri = response['repoUri']
  unless repo_uri.include?('/api/storage/')
    # rubocop:disable Style/PercentLiteralDelimiters
    repo_uri.sub!(%r(/#{params[:repo]}$), "/api/storage/#{params[:repo]}")
  end
  response['files'].map do |file_path|
    from_url("#{repo_uri}/#{file_path}", client: client)
  end
end