File: artifactory_latest_url.rb

package info (click to toggle)
puppet-module-voxpupuli-archive 7.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 744 kB
  • sloc: ruby: 2,483; sh: 10; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

require 'json'
require_relative '../../../puppet_x/bodeco/util'

Puppet::Functions.create_function(:'archive::artifactory_latest_url') do
  dispatch :artifactory_latest_url do
    param 'Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]', :url
    param 'Hash', :maven_data
  end

  def artifactory_latest_url(url, maven_data)
    # Turn provided artifactory URL into the fileinfo API endpoint of the parent directory
    uri = URI(url.sub('/artifactory/', '/artifactory/api/storage/')[%r{^(.*)/.*$}, 1])

    response = PuppetX::Bodeco::Util.content(uri)
    content  = JSON.parse(response)

    uris = if maven_data['classifier']
             content['children'].select do |child|
               child['uri'] =~ %r{^/#{maven_data['module']}-#{maven_data['base_rev']}-(SNAPSHOT|(?:(?:[0-9]{8}.[0-9]{6})-(?:[0-9]+)))-#{maven_data['classifier']}\.#{maven_data['ext']}$} && !child['folder']
             end
           else
             content['children'].select do |child|
               child['uri'] =~ %r{^/#{maven_data['module']}-#{maven_data['base_rev']}-(SNAPSHOT|(?:(?:[0-9]{8}.[0-9]{6})-(?:[0-9]+)))\.#{maven_data['ext']}$} && !child['folder']
             end
           end

    raise("Couldn't find any Artifactory artifacts") if uris.empty?

    latest = uris.max_by { |x| x['uri'] }['uri']
    Puppet.debug("Latest artifact found for #{url} was #{latest}")

    # Now GET the fileinfo endpoint of the resolved latest version file
    uri = URI("#{content['uri']}#{latest}")
    response = PuppetX::Bodeco::Util.content(uri)
    content  = JSON.parse(response)

    url  = content['downloadUri']
    sha1 = content['checksums'] && content['checksums']['sha1']

    {
      'url' => url,
      'sha1' => sha1
    }
  end
end