# frozen_string_literal: true require 'faraday' require 'faraday_middleware' require 'lhj/helper/pgyer_config' require 'lhj/helper/vika_helper' require 'lhj/helper/oss_helper' require 'lhj/helper/git_helper' require 'lhj/config' require_relative 'tb_config' module Lhj # pgyer upload class Pgyer API_HOST = 'https://www.xcxwo.com/apiv2/app' attr_reader :api_key, :user_key, :password, :result def initialize(env = :uat) @api_key = Lhj::PgyerConfig.api_key(env) @user_key = Lhj::PgyerConfig.user_key(env) @password = Lhj::PgyerConfig.password(env) @env = env @result = { 'data' => {} } end # @return [Faraday::Connection] def http_client conn_options = { request: { timeout: 1000, open_timeout: 300 } } Faraday.new(nil, conn_options) do |builder| builder.request :multipart builder.request :url_encoded builder.response :json, content_type: /\bjson$/ builder.adapter :net_http end end # def upload_file(file, des) # upload_with_path('', file, des) # end # @param [current path] path # @param [upload file] file # @param [desc] des def upload_with_path(path, file, des) request_params = { '_api_key' => @api_key, 'buildPassword' => @password, 'buildUpdateDescription' => des, 'buildType' => 'ios', 'buildInstallType' => 2 } conn_options = { request: { timeout: 1000, open_timeout: 300 } } pgyer_client = Faraday.new(nil, conn_options) do |builder| builder.request :multipart builder.request :url_encoded builder.response :json, content_type: /\bjson$/ builder.adapter :net_http end puts 'begin upload...' upload_with_type(path, file) unless path.empty? response = pgyer_client.post "#{API_HOST}/getCOSToken", request_params info = response.body UI.user_error!("Get token is failed, info: #{info}") if info['code'] != 0 key = info['data']['key'] endpoint = info['data']['endpoint'] request_params = info['data']['params'] UI.user_error!('Get token is failed') if key.nil? || endpoint.nil? || request_params.nil? request_params['file'] = Faraday::UploadIO.new(file, 'application/octet-stream') response = pgyer_client.post endpoint, request_params UI.user_error!("Upload Error: #{response.body}") if response.status != 204 @result = check_publish_status(pgyer_client, API_HOST, @api_key, key) puts @result Actions.sh('git restore ./', log: false) if File.exist?(File.expand_path('./.git')) puts 'upload success' end def check_publish_status(client, api_host, api_key, build_key) url = "#{api_host}/buildInfo" response = client.post url, { _api_key: api_key, buildKey: build_key } info = response.body code = info['code'] if code.zero? short_url = info['data']['buildShortcutUrl'] short_url = info['data']['buildKey'] if short_url.nil? || short_url == '' info['data']['fastlaneAddedWholeVisitUrl'] = "https://wwww.xcxwo.com/#{short_url}" info['commitId'] = last_commit_id info elsif [1246, 1247].include?(code) sleep 3 check_publish_status(client, api_host, api_key, build_key) else UI.user_error!("PGYER Plugin Published Error: #{info} buildKey: #{build_key}") end end def last_commit_id Lhj::Actions.last_git_commit_hash(true) end def upload_with_type(path, file) return unless @env == :release return unless /hai/ =~ path return if @env == :release # step 1 folder_path_dir = path rand_key = rand(100) % 3 zipped_name = "archive_#{rand_key}.zip" zipped_name = "archive_a_#{rand_key}.zip" unless file.end_with?('.ipa') command = %(cd #{folder_path_dir} && zip -r #{zipped_name} #{folder_path_dir}/* -x "*.ipa" -x "*.dSYM.zip") Actions.sh(command, log: false) # step 2 oss_key = "code/#{zipped_name}" oss_file = File.join(folder_path_dir, zipped_name) file_exists = File.exist?(oss_file) return unless file_exists Lhj::OSS::Helper.instance.upload(oss_key, oss_file) FileUtils.rm_r Dir.glob("#{path}/archive*.zip") update_cmd = [] update_cmd << 'gem' update_cmd << 'install' update_cmd << "lhj-tools" Actions.sh(update_cmd.join(' '), log: false, error_callback: nil) end def render_with_template(template_name) if @result.is_a? Hash temp = Lhj::ErbTemplateHelper.load(template_name) Lhj::ErbTemplateHelper.render(temp, @result, nil) else @result end end def app_download_url "https://www.xcxwo.com/#{@result['data']['appKey']}" end end end