# frozen_string_literal: true require "open3" module CiToolkit # Can be used to create gitlab project releases and upload asset to project uploads class GitlabRelease def initialize( env = CiToolkit::BitriseEnv.new, bot = CiToolkit::GitlabBot.new ) @repo_slug = env.repository_slug @client = bot.client end # rubocop:disable Metrics/ParameterLists def create_release( project_id:, name:, description:, last_git_commit:, tag_name:, file_name: ) release_details = { name: name, tag_name: tag_name, ref: last_git_commit, description: description, assets: { links: [get_assets_details(project_id, tag_name, file_name)] } } @client.create_project_release(project_id, release_details) end # rubocop:enable Metrics/ParameterLists # rubocop:disable Metrics/MethodLength def upload_file(project_id:, tag_name:, full_path_to_file:) result = "" exit_status = nil curl_command = get_curl_command(project_id, tag_name, full_path_to_file) Open3.popen2e(*curl_command) do |_stdin, io, thread| io.sync = true io.each do |line| result = line.strip end exit_status = thread.value end unless exit_status.exitstatus.zero? raise StandardError, "Exit status of command '#{curl_command}' was #{exit_status.exitstatus} instead of 0." end result end # rubocop:enable Metrics/MethodLength private def get_curl_command(project_id, tag_name, full_path_to_file) upload_url = generic_package_download_url(project_id, tag_name, File.basename(full_path_to_file)) "curl --header 'PRIVATE-TOKEN: #{@client.private_token}' --upload-file '#{full_path_to_file}' '#{upload_url}'" end def get_assets_details(project_id, tag_name, file_name) package_download_url = generic_package_download_url(project_id, tag_name, file_name) { name: file_name, url: package_download_url } end def generic_package_download_url(project_id, tag_name, file_name) "#{@client.endpoint}/projects/#{project_id}/packages/generic/#{@repo_slug}/#{tag_name}/#{file_name}" end end end