Class: MultiRepo::Service::Artifactory

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dry_run: false, cache: true) ⇒ Artifactory

Returns a new instance of Artifactory.



25
26
27
28
29
30
31
# File 'lib/multi_repo/service/artifactory.rb', line 25

def initialize(dry_run: false, cache: true)
  require "rest-client"
  require "json"

  @dry_run = dry_run
  @cache   = cache
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



23
24
25
# File 'lib/multi_repo/service/artifactory.rb', line 23

def cache
  @cache
end

#dry_runObject

Returns the value of attribute dry_run.



23
24
25
# File 'lib/multi_repo/service/artifactory.rb', line 23

def dry_run
  @dry_run
end

Class Method Details

.api_endpointObject



11
12
13
# File 'lib/multi_repo/service/artifactory.rb', line 11

def self.api_endpoint
  @api_endpoint ||= ENV.fetch("ARTIFACTORY_API_ENDPOINT")
end

.api_endpoint=(endpoint) ⇒ Object



15
16
17
# File 'lib/multi_repo/service/artifactory.rb', line 15

def self.api_endpoint=(endpoint)
  @api_endpoint = endpoint
end

.api_tokenObject



3
4
5
# File 'lib/multi_repo/service/artifactory.rb', line 3

def self.api_token
  @api_token ||= ENV.fetch("ARTIFACTORY_API_TOKEN")
end

.api_token=(token) ⇒ Object



7
8
9
# File 'lib/multi_repo/service/artifactory.rb', line 7

def self.api_token=(token)
  @api_token = token
end

.auth_headerObject



19
20
21
# File 'lib/multi_repo/service/artifactory.rb', line 19

def self.auth_header
  {"X-JFrog-Art-Api" => api_token}
end

Instance Method Details

#clear_cacheObject



33
34
35
# File 'lib/multi_repo/service/artifactory.rb', line 33

def clear_cache
  FileUtils.rm_f(Dir.glob("/tmp/artifactory-*"))
end

#copy(file, to, **kwargs) ⇒ Object



84
85
86
87
88
# File 'lib/multi_repo/service/artifactory.rb', line 84

def copy(file, to, **kwargs)
  file = file.to_s
  to = to.to_s
  request(:post, File.join("/api/copy", "#{strip_api_prefix(file)}?to=/#{strip_api_prefix(to)}"), **kwargs)
end

#delete(file, **kwargs) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/multi_repo/service/artifactory.rb', line 70

def delete(file, **kwargs)
  file = file.to_s
  request(:delete, strip_api_prefix(file), **kwargs)
rescue RestClient::NotFound => err
  # Ignore deletes on a 404 because it's already deleted
  raise unless err.http_code == 404
end

#get(path, **kwargs) ⇒ Object



38
39
40
41
# File 'lib/multi_repo/service/artifactory.rb', line 38

def get(path, **kwargs)
  path = path.to_s
  request(:get, path, **kwargs)
end

#list(folder, cache: @cache, **kwargs) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/multi_repo/service/artifactory.rb', line 43

def list(folder, cache: @cache, **kwargs)
  folder = folder.to_s
  cache_file = "/tmp/artifactory-#{folder.tr("/", "_")}-#{Date.today}.txt"
  if cache && File.exist?(cache_file)
    File.readlines(cache_file, :chomp => true)
  else
    data = raw_list(folder, cache: cache, **kwargs)
    uri  = data["uri"]

    data["files"].map { |d| File.join(uri, d["uri"]) }.tap do |d|
      File.write(cache_file, d.join("\n")) if cache
    end
  end
end

#move(file, to, **kwargs) ⇒ Object



78
79
80
81
82
# File 'lib/multi_repo/service/artifactory.rb', line 78

def move(file, to, **kwargs)
  file = file.to_s
  to = to.to_s
  request(:post, File.join("/api/move", "#{strip_api_prefix(file)}?to=/#{strip_api_prefix(to)}"), **kwargs)
end

#raw_list(folder, cache: @cache, **kwargs) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/multi_repo/service/artifactory.rb', line 58

def raw_list(folder, cache: @cache, **kwargs)
  folder = folder.to_s
  cache_file = "/tmp/artifactory-#{folder.tr("/", "_")}-raw-#{Date.today}.json"
  if cache && File.exist?(cache_file)
    JSON.parse(File.read(cache_file))
  else
    get("/api/storage/#{folder}?list&deep=1", **kwargs).tap do |d|
      File.write(cache_file, JSON.pretty_generate(d)) if cache
    end
  end
end