Class: GDK::PackageHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/gdk/package_helpers.rb

Constant Summary collapse

API_V4_URL =
'https://gitlab.com/api/v4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin_path:, package_path:, package_name:, package_version:, project_path:, project_id: ENV.fetch('CI_PROJECT_ID', '74823'), token: ENV.fetch('CI_JOB_TOKEN', '')) ⇒ PackageHelpers

Returns a new instance of PackageHelpers.



15
16
17
18
19
20
21
22
23
# File 'lib/gdk/package_helpers.rb', line 15

def initialize(bin_path:, package_path:, package_name:, package_version:, project_path:, project_id: ENV.fetch('CI_PROJECT_ID', '74823'), token: ENV.fetch('CI_JOB_TOKEN', ''))
  @bin_path = bin_path
  @package_path = package_path
  @package_name = package_name
  @package_version = package_version
  @project_path = project_path
  @project_id = project_id
  @token = token
end

Instance Attribute Details

#bin_pathObject (readonly)

Returns the value of attribute bin_path.



13
14
15
# File 'lib/gdk/package_helpers.rb', line 13

def bin_path
  @bin_path
end

#package_nameObject (readonly)

Returns the value of attribute package_name.



13
14
15
# File 'lib/gdk/package_helpers.rb', line 13

def package_name
  @package_name
end

#package_pathObject (readonly)

Returns the value of attribute package_path.



13
14
15
# File 'lib/gdk/package_helpers.rb', line 13

def package_path
  @package_path
end

#package_versionObject (readonly)

Returns the value of attribute package_version.



13
14
15
# File 'lib/gdk/package_helpers.rb', line 13

def package_version
  @package_version
end

#project_idObject (readonly)

Returns the value of attribute project_id.



13
14
15
# File 'lib/gdk/package_helpers.rb', line 13

def project_id
  @project_id
end

#project_pathObject (readonly)

Returns the value of attribute project_path.



13
14
15
# File 'lib/gdk/package_helpers.rb', line 13

def project_path
  @project_path
end

#tokenObject (readonly)

Returns the value of attribute token.



13
14
15
# File 'lib/gdk/package_helpers.rb', line 13

def token
  @token
end

Instance Method Details

#create_packageObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gdk/package_helpers.rb', line 25

def create_package
  File.open(package_path, 'wb') do |file|
    Zlib::GzipWriter.wrap(file) do |gzip|
      Gem::Package::TarWriter.new(gzip) do |tar|
        Dir.glob(File.join(bin_path, '**/*')).each do |file_path|
          add_file_to_tar(tar, file_path)
        end
      end
    end
  end

  GDK::Output.success("Package created at #{package_path}")

rescue StandardError => e
  raise "Package creation failed: #{e.message}"
end

#download_packageObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gdk/package_helpers.rb', line 57

def download_package
  return GDK::Output.success("No changes detected in #{project_path}, skipping package download and extraction.") if current_commit_sha == stored_commit_sha

  uri = URI.parse("#{API_V4_URL}/projects/#{project_id}/packages/generic/#{package_name}/#{package_version}/#{File.basename(package_path)}")

  GDK::Output.info("Downloading package from #{uri}")

  response = Net::HTTP.get_response(uri)

  raise "Download failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess)

  File.write(package_path, response.body)
  GDK::Output.success("Package downloaded successfully to #{package_path}")

  store_commit_sha(current_commit_sha)
end

#extract_packageObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gdk/package_helpers.rb', line 74

def extract_package
  FileUtils.mkdir_p(bin_path)

  File.open(package_path, 'rb') do |file|
    Zlib::GzipReader.wrap(file) do |gzip|
      Gem::Package::TarReader.new(gzip) do |tar|
        tar.each do |entry|
          extract_entry(entry)
        end
      end
    end
  end

  GDK::Output.success("Package extracted successfully to #{bin_path}")
  FileUtils.rm_f(package_path)
end

#upload_packageObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gdk/package_helpers.rb', line 42

def upload_package
  uri = URI.parse("#{API_V4_URL}/projects/#{project_id}/packages/generic/#{package_name}/#{package_version}/#{package_path}")
  request = Net::HTTP::Put.new(uri)
  request['JOB-TOKEN'] = token
  request.body = File.read(package_path)

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  raise "Upload failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess)

  GDK::Output.success("Package uploaded successfully to #{uri}")
end