Class: Artifactory::Resource::Artifact

Inherits:
Base
  • Object
show all
Defined in:
lib/artifactory/resources/artifact.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attribute, #attributes, attributes, #client, #client=, #client?, extract_client!, #extract_client!, format_repos!, #format_repos!, #initialize, #inspect, #set, #to_s, #url_safe, url_safe

Constructor Details

This class inherits a constructor from Artifactory::Resource::Base

Class Method Details

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

Search for an artifact by its checksum

Examples:

Search for all repositories with the given MD5 checksum

Artifact.checksum_search(
  md5: 'abcd1234...',
)

Search for all artifacts with the given SHA1 checksum in a repo

Artifact.checksum_search(
  sha1: 'abcdef123456....',
  repos: 'libs-release-local',
)

Parameters:

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

    the list of options to search with

Options Hash (options):

  • :client (Artifactory::Client)

    the client object to make the request with

  • :md5 (String)

    the MD5 checksum of the artifact to search for

  • :sha1 (String)

    the SHA1 checksum of the artifact to search for

  • :repos (String, Array<String>)

    the list of repos to search

Returns:



160
161
162
163
164
165
166
167
168
# File 'lib/artifactory/resources/artifact.rb', line 160

def checksum_search(options = {})
  client = extract_client!(options)
  params = Util.slice(options, :md5, :sha1, :repos)
  format_repos!(params)

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

.from_hash(hash, options = {}) ⇒ Resource::Artifact

Create a instance from the given Hash. This method extracts the “safe” information from the hash and adds them to the instance.

Examples:

Create a new resource from a hash

Artifact.from_hash('downloadUri' => '...', 'size' => '...')

Parameters:

  • client (Artifactory::Client)

    the client object to make the request with

  • hash (Hash)

    the hash to create the instance from

Returns:



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/artifactory/resources/artifact.rb', line 291

def from_hash(hash, options = {})
  client = extract_client!(options)

  new.tap do |instance|
    instance.api_path      = hash['uri']
    instance.client        = client
    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.md5           = hash['checksums']['md5']
    instance.mime_type     = hash['mimeType']
    instance.repo          = hash['repo']
    instance.sha1          = hash['checksums']['sha1']
    instance.size          = hash['size'].to_i
  end
end

.from_url(url, options = {}) ⇒ Resource::Artifact

Construct an artifact from the given URL.

Examples:

Create an artifact object from the given URL

Artifact.from_url('/path/to/some.deb') #=> #<Resource::Artifact>

Parameters:

  • client (Artifactory::Client)

    the client object to make the request with

  • url (String)

    the URL to find the artifact from

Returns:



272
273
274
275
# File 'lib/artifactory/resources/artifact.rb', line 272

def from_url(url, options = {})
  client = extract_client!(options)
  from_hash(client.get(url), client: client)
end

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

Search for an artifact by Maven coordinates: Group ID, Artifact ID, Version and Classifier.

Examples:

Search for all repositories with the given gavc

Artifact.gavc_search(
  group:      'org.acme',
  name:       'artifact',
  version:    '1.0',
  classifier: 'sources',
)

Search for all artifacts with the given gavc in a specific repo

Artifact.gavc_search(
  group:      'org.acme',
  name:       'artifact',
  version:    '1.0',
  classifier: 'sources',
  repos:      'libs-release-local',
)

Parameters:

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

    the list of options to search with

Options Hash (options):

  • :client (Artifactory::Client)

    the client object to make the request with

  • :group (String)

    the group id to search for

  • :name (String)

    the artifact id to search for

  • :version (String)

    the version of the artifact to search for

  • :classifier (String)

    the classifer to search for

  • :repos (String, Array<String>)

    the list of repos to search

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/artifactory/resources/artifact.rb', line 76

def gavc_search(options = {})
  client = extract_client!(options)
  options = Util.rename_keys(options,
    :group      => :g,
    :name       => :a,
    :version    => :v,
    :classifier => :c,
  )
  params = Util.slice(options, :g, :a, :v, :c, :repos)
  format_repos!(params)

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

.latest_version(options = {}) ⇒ String?

Get the latest version of an artifact.

Examples:

Find the latest version of an artifact

Artifact.latest_version(name: 'artifact')

Find the latest version of an artifact in a repo

Artifact.latest_version(
  name: 'artifact',
  repo: 'libs-release-local',
)

Find the latest snapshot version of an artifact

Artifact.latest_version(name: 'artifact', version: '1.0-SNAPSHOT')

Find the latest version of an artifact in a group

Artifact.latest_version(name: 'artifact', group: 'org.acme')

Parameters:

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

    the list of options to search with

Options Hash (options):

  • :client (Artifactory::Client)

    the client object to make the request with

  • :group (String)

    the group id to search for

  • :name (String)

    the artifact id to search for

  • :version (String)

    the version of the artifact to search for

  • :remote (Boolean)

    search remote repos (default: false)

  • :repos (String, Array<String>)

    the list of repos to search

Returns:

  • (String, nil)

    the latest version as a string (e.g. 1.0-201203131455-2), or nil if no artifact matches the given query



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/artifactory/resources/artifact.rb', line 240

def latest_version(options = {})
  client = extract_client!(options)
  options = Util.rename_keys(options,
    :group   => :g,
    :name    => :a,
    :version => :v,
  )
  params = Util.slice(options, :g, :a, :v, :repos, :remote)
  format_repos!(params)

  # For whatever reason, Artifactory won't accept "true" - they want a
  # literal "1"...
  params[:remote] = 1 if options[:remote]

  client.get('/api/search/latestVersion', params)
rescue Error::NotFound
  nil
end

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

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.

Examples:

Search for all repositories with the given properties

Artifact.property_search(
  branch: 'master',
  author: 'sethvargo',
)

Search for all artifacts with the given gavc in a specific repo

Artifact.property_search(
  branch: 'master',
  author: 'sethvargo',
  repos: 'libs-release-local',
)

Parameters:

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

    the free-form list of options to search with

Options Hash (options):

  • :client (Artifactory::Client)

    the client object to make the request with

  • :repos (String, Array<String>)

    the list of repos to search

Returns:



121
122
123
124
125
126
127
128
129
# File 'lib/artifactory/resources/artifact.rb', line 121

def property_search(options = {})
  client = extract_client!(options)
  params = options.dup
  format_repos!(params)

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

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

Search for an artifact by the full or partial filename.

Examples:

Search for all repositories with the name “artifact”

Artifact.search(name: 'artifact')

Search for all artifacts named “artifact” in a specific repo

Artifact.search(name: 'artifact', repos: 'libs-release-local')

Parameters:

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

    the list of options to search with

Options Hash (options):

  • :client (Artifactory::Client)

    the client object to make the request with

  • :name (String)

    the name of the artifact to search (it can be a regular expression)

  • :repos (String, Array<String>)

    the list of repos to search

Returns:



26
27
28
29
30
31
32
33
34
# File 'lib/artifactory/resources/artifact.rb', line 26

def search(options = {})
  client = extract_client!(options)
  params = Util.slice(options, :name, :repos)
  format_repos!(params)

  client.get('/api/search/artifact', params)['results'].map do |artifact|
    from_url(artifact['uri'], client: client)
  end
end

.versions(options = {}) ⇒ Object

Get all versions of an artifact.

Examples:

Get all versions of a given artifact

Artifact.versions(name: 'artifact')

Get all versions of a given artifact in a specific repo

Artifact.versions(name: 'artifact', repos: 'libs-release-local')

Parameters:

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

    the list of options to search with

Options Hash (options):

  • :client (Artifactory::Client)

    the client object to make the request with

  • :group (String)

    the

  • :sha1 (String)

    the SHA1 checksum of the artifact to search for

  • :repos (String, Array<String>)

    the list of repos to search



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/artifactory/resources/artifact.rb', line 190

def versions(options = {})
  client  = extract_client!(options)
  options = Util.rename_keys(options,
    :group   => :g,
    :name    => :a,
    :version => :v,
  )
  params = Util.slice(options, :g, :a, :v, :repos)
  format_repos!(params)

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

Instance Method Details

#api_pathObject

Return this object’s api_path

Returns:

  • (Object)


310
# File 'lib/artifactory/resources/artifact.rb', line 310

attribute :api_path, ->{ raise 'API path missing!' }

#api_path=(value) ⇒ Object

Set this object’s api_path

Parameters:

  • value (Object)

    the value to set for api_path

  • default (Object)

    the default value for this attribute



310
# File 'lib/artifactory/resources/artifact.rb', line 310

attribute :api_path, ->{ raise 'API path missing!' }

#api_path?Boolean

Determines if the api_path value exists and is truthy

Returns:

  • (Boolean)


310
# File 'lib/artifactory/resources/artifact.rb', line 310

attribute :api_path, ->{ raise 'API path missing!' }

#complianceHash<String, Array<Hash>>

Get compliance info for a given artifact path. The result includes license and vulnerabilities, if any.

**This requires the Black Duck addon to be enabled!**

Examples:

Get compliance info for an artifact

artifact.compliance #=> { 'licenses' => [{ 'name' => 'LGPL v3' }] }

Returns:

  • (Hash<String, Array<Hash>>)


373
374
375
# File 'lib/artifactory/resources/artifact.rb', line 373

def compliance
  @compliance ||= client.get(File.join('/api/compliance', relative_path))
end

#copy(destination, options = {}) ⇒ Object

See Also:

  • #copy_or_move


325
326
327
# File 'lib/artifactory/resources/artifact.rb', line 325

def copy(destination, options = {})
  copy_or_move(:copy, destination, options)
end

#createdObject

Return this object’s created

Returns:

  • (Object)


311
# File 'lib/artifactory/resources/artifact.rb', line 311

attribute :created

#created=(value) ⇒ Object

Set this object’s created

Parameters:

  • value (Object)

    the value to set for created

  • default (Object)

    the default value for this attribute



311
# File 'lib/artifactory/resources/artifact.rb', line 311

attribute :created

#created?Boolean

Determines if the created value exists and is truthy

Returns:

  • (Boolean)


311
# File 'lib/artifactory/resources/artifact.rb', line 311

attribute :created

#deleteBoolean

Delete this artifact from repository, suppressing any ResourceNotFound exceptions might occur.

Returns:

  • (Boolean)

    true if the object was deleted successfully, false otherwise



336
337
338
339
340
# File 'lib/artifactory/resources/artifact.rb', line 336

def delete
  !!client.delete(download_path)
rescue Error::NotFound
  false
end

#download(target = Dir.mktmpdir, options = {}) ⇒ String

Download the artifact onto the local disk.

Examples:

Download an artifact

artifact.download #=> /tmp/cache/000adad0-bac/artifact.deb

Download a remote artifact into a specific target

artifact.download('~/Desktop') #=> ~/Desktop/artifact.deb

Parameters:

  • target (String) (defaults to: Dir.mktmpdir)

    the target directory where the artifact should be downloaded to (defaults to a temporary directory). **It is the user’s responsibility to cleanup the temporary directory when finished!**

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

Options Hash (options):

  • filename (String)

    the name of the file when downloaded to disk (defaults to the basename of the file on the server)

Returns:

  • (String)

    the path where the file was downloaded on disk



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/artifactory/resources/artifact.rb', line 398

def download(target = Dir.mktmpdir, options = {})
  target = File.expand_path(target)

  # Make the directory if it doesn't yet exist
  FileUtils.mkdir_p(target) unless File.exists?(target)

  # Use the server artifact's filename if one wasn't given
  filename = options[:filename] || File.basename(download_path)

  # Construct the full path for the file
  destination = File.join(targer, filename)

  File.open(File.join(destination, filename), 'wb') do |file|
    file.write(_get(download_path))
  end

  destination
end

#download_pathObject

Return this object’s download_path

Returns:

  • (Object)


312
# File 'lib/artifactory/resources/artifact.rb', line 312

attribute :download_path, ->{ raise 'Download path missing!' }

#download_path=(value) ⇒ Object

Set this object’s download_path

Parameters:

  • value (Object)

    the value to set for download_path

  • default (Object)

    the default value for this attribute



312
# File 'lib/artifactory/resources/artifact.rb', line 312

attribute :download_path, ->{ raise 'Download path missing!' }

#download_path?Boolean

Determines if the download_path value exists and is truthy

Returns:

  • (Boolean)


312
# File 'lib/artifactory/resources/artifact.rb', line 312

attribute :download_path, ->{ raise 'Download path missing!' }

#last_modifiedObject

Return this object’s last_modified

Returns:

  • (Object)


313
# File 'lib/artifactory/resources/artifact.rb', line 313

attribute :last_modified

#last_modified=(value) ⇒ Object

Set this object’s last_modified

Parameters:

  • value (Object)

    the value to set for last_modified

  • default (Object)

    the default value for this attribute



313
# File 'lib/artifactory/resources/artifact.rb', line 313

attribute :last_modified

#last_modified?Boolean

Determines if the last_modified value exists and is truthy

Returns:

  • (Boolean)


313
# File 'lib/artifactory/resources/artifact.rb', line 313

attribute :last_modified

#last_updatedObject

Return this object’s last_updated

Returns:

  • (Object)


314
# File 'lib/artifactory/resources/artifact.rb', line 314

attribute :last_updated

#last_updated=(value) ⇒ Object

Set this object’s last_updated

Parameters:

  • value (Object)

    the value to set for last_updated

  • default (Object)

    the default value for this attribute



314
# File 'lib/artifactory/resources/artifact.rb', line 314

attribute :last_updated

#last_updated?Boolean

Determines if the last_updated value exists and is truthy

Returns:

  • (Boolean)


314
# File 'lib/artifactory/resources/artifact.rb', line 314

attribute :last_updated

#local_pathObject

Return this object’s local_path

Returns:

  • (Object)


315
# File 'lib/artifactory/resources/artifact.rb', line 315

attribute :local_path, ->{ raise 'Local destination missing!' }

#local_path=(value) ⇒ Object

Set this object’s local_path

Parameters:

  • value (Object)

    the value to set for local_path

  • default (Object)

    the default value for this attribute



315
# File 'lib/artifactory/resources/artifact.rb', line 315

attribute :local_path, ->{ raise 'Local destination missing!' }

#local_path?Boolean

Determines if the local_path value exists and is truthy

Returns:

  • (Boolean)


315
# File 'lib/artifactory/resources/artifact.rb', line 315

attribute :local_path, ->{ raise 'Local destination missing!' }

#md5Object

Return this object’s md5

Returns:

  • (Object)


317
# File 'lib/artifactory/resources/artifact.rb', line 317

attribute :md5

#md5=(value) ⇒ Object

Set this object’s md5

Parameters:

  • value (Object)

    the value to set for md5

  • default (Object)

    the default value for this attribute



317
# File 'lib/artifactory/resources/artifact.rb', line 317

attribute :md5

#md5?Boolean

Determines if the md5 value exists and is truthy

Returns:

  • (Boolean)


317
# File 'lib/artifactory/resources/artifact.rb', line 317

attribute :md5

#mime_typeObject

Return this object’s mime_type

Returns:

  • (Object)


316
# File 'lib/artifactory/resources/artifact.rb', line 316

attribute :mime_type

#mime_type=(value) ⇒ Object

Set this object’s mime_type

Parameters:

  • value (Object)

    the value to set for mime_type

  • default (Object)

    the default value for this attribute



316
# File 'lib/artifactory/resources/artifact.rb', line 316

attribute :mime_type

#mime_type?Boolean

Determines if the mime_type value exists and is truthy

Returns:

  • (Boolean)


316
# File 'lib/artifactory/resources/artifact.rb', line 316

attribute :mime_type

#move(destination, options = {}) ⇒ Object

See Also:

  • Artifactory::Resource::Artifact.{Artifact{Artifact#copy_or_move}


345
346
347
# File 'lib/artifactory/resources/artifact.rb', line 345

def move(destination, options = {})
  copy_or_move(:move, destination, options)
end

#propertiesHash<String, Object>

The list of properties for this object.

Examples:

List all properties for an artifact

artifact.properties #=> { 'artifactory.licenses'=>['Apache-2.0'] }

Returns:

  • (Hash<String, Object>)

    the list of properties



358
359
360
# File 'lib/artifactory/resources/artifact.rb', line 358

def properties
  @properties ||= client.get(api_path, properties: nil)['properties']
end

#repoObject

Return this object’s repo

Returns:

  • (Object)


318
# File 'lib/artifactory/resources/artifact.rb', line 318

attribute :repo

#repo=(value) ⇒ Object

Set this object’s repo

Parameters:

  • value (Object)

    the value to set for repo

  • default (Object)

    the default value for this attribute



318
# File 'lib/artifactory/resources/artifact.rb', line 318

attribute :repo

#repo?Boolean

Determines if the repo value exists and is truthy

Returns:

  • (Boolean)


318
# File 'lib/artifactory/resources/artifact.rb', line 318

attribute :repo

#sha1Object

Return this object’s sha1

Returns:

  • (Object)


319
# File 'lib/artifactory/resources/artifact.rb', line 319

attribute :sha1

#sha1=(value) ⇒ Object

Set this object’s sha1

Parameters:

  • value (Object)

    the value to set for sha1

  • default (Object)

    the default value for this attribute



319
# File 'lib/artifactory/resources/artifact.rb', line 319

attribute :sha1

#sha1?Boolean

Determines if the sha1 value exists and is truthy

Returns:

  • (Boolean)


319
# File 'lib/artifactory/resources/artifact.rb', line 319

attribute :sha1

#sizeObject

Return this object’s size

Returns:

  • (Object)


320
# File 'lib/artifactory/resources/artifact.rb', line 320

attribute :size

#size=(value) ⇒ Object

Set this object’s size

Parameters:

  • value (Object)

    the value to set for size

  • default (Object)

    the default value for this attribute



320
# File 'lib/artifactory/resources/artifact.rb', line 320

attribute :size

#size?Boolean

Determines if the size value exists and is truthy

Returns:

  • (Boolean)


320
# File 'lib/artifactory/resources/artifact.rb', line 320

attribute :size