|
| 1 | +require 'dry/cli' |
| 2 | +require 'json' |
| 3 | +require 'uri' |
| 4 | +require 'async' |
| 5 | +require 'async/barrier' |
| 6 | +require 'async/semaphore' |
| 7 | +require 'async/http/internet/instance' |
| 8 | + |
| 9 | +module EmergeCLI |
| 10 | + module Commands |
| 11 | + module Upload |
| 12 | + class Build < EmergeCLI::Commands::GlobalOptions |
| 13 | + desc 'Upload a build to Emerge' |
| 14 | + |
| 15 | + option :path, type: :string, required: true, desc: 'Path to the build artifact' |
| 16 | + |
| 17 | + # Optional options |
| 18 | + option :api_token, type: :string, required: false, |
| 19 | + desc: 'API token for authentication, defaults to ENV[EMERGE_API_TOKEN]' |
| 20 | + option :sha, type: :string, required: false, desc: 'SHA of the commit' |
| 21 | + option :branch, type: :string, required: false, desc: 'Branch name' |
| 22 | + option :repo_name, type: :string, required: false, desc: 'Repository name' |
| 23 | + option :base_sha, type: :string, required: false, desc: 'Base SHA' |
| 24 | + option :previous_sha, type: :string, required: false, desc: 'Previous SHA' |
| 25 | + option :pr_number, type: :string, required: false, desc: 'PR number' |
| 26 | + |
| 27 | + def initialize(network: nil, git_info_provider: nil) |
| 28 | + @network = network |
| 29 | + @git_info_provider = git_info_provider |
| 30 | + end |
| 31 | + |
| 32 | + def call(**options) |
| 33 | + @options = options |
| 34 | + @profiler = EmergeCLI::Profiler.new(enabled: options[:profile]) |
| 35 | + before(options) |
| 36 | + |
| 37 | + start_time = Time.now |
| 38 | + |
| 39 | + file_path = options[:path] |
| 40 | + file_exists = File.exist?(file_path) |
| 41 | + raise "File not found at path: #{file_path}" unless file_exists |
| 42 | + |
| 43 | + file_extension = File.extname(file_path) |
| 44 | + raise "Unsupported file type: #{file_extension}" unless ['.ipa', '.apk', '.aab', |
| 45 | + '.zip'].include?(file_extension) |
| 46 | + |
| 47 | + api_token = @options[:api_token] || ENV.fetch('EMERGE_API_TOKEN', nil) |
| 48 | + raise 'API token is required and cannot be blank' if api_token.nil? || api_token.strip.empty? |
| 49 | + |
| 50 | + @network ||= EmergeCLI::Network.new(api_token:) |
| 51 | + @git_info_provider ||= GitInfoProvider.new |
| 52 | + |
| 53 | + Sync do |
| 54 | + upload_url, upload_id = fetch_upload_url |
| 55 | + |
| 56 | + file_size = File.size(file_path) |
| 57 | + Logger.info("Uploading file... (#{file_size} bytes)") |
| 58 | + |
| 59 | + File.open(file_path, 'rb') do |file| |
| 60 | + headers = { |
| 61 | + 'Content-Type' => 'application/zip', |
| 62 | + 'Content-Length' => file_size.to_s |
| 63 | + } |
| 64 | + |
| 65 | + response = @network.put( |
| 66 | + path: upload_url, |
| 67 | + body: file.read, |
| 68 | + headers: headers |
| 69 | + ) |
| 70 | + |
| 71 | + unless response.status == 200 |
| 72 | + Logger.error("Upload failed with status #{response.status}") |
| 73 | + Logger.error("Response body: #{response.body}") |
| 74 | + raise "Uploading file failed with status #{response.status}" |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + Logger.info('Upload complete successfully!') |
| 79 | + Logger.info "Time taken: #{(Time.now - start_time).round(2)} seconds" |
| 80 | + Logger.info("✅ You can view the build analysis at https://emergetools.com/build/#{upload_id}") |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + def fetch_upload_url |
| 87 | + git_result = @git_info_provider.fetch_git_info |
| 88 | + sha = @options[:sha] || git_result.sha |
| 89 | + branch = @options[:branch] || git_result.branch |
| 90 | + base_sha = @options[:base_sha] || git_result.base_sha |
| 91 | + previous_sha = @options[:previous_sha] || git_result.previous_sha |
| 92 | + pr_number = @options[:pr_number] || git_result.pr_number |
| 93 | + |
| 94 | + # TODO: Make optional |
| 95 | + raise 'SHA is required' unless sha |
| 96 | + raise 'Branch is required' unless branch |
| 97 | + |
| 98 | + payload = { |
| 99 | + sha:, |
| 100 | + branch:, |
| 101 | + repo_name: @options[:repo_name], |
| 102 | + # Optional |
| 103 | + base_sha:, |
| 104 | + previous_sha:, |
| 105 | + pr_number: pr_number&.to_s |
| 106 | + }.compact |
| 107 | + |
| 108 | + upload_response = @network.post( |
| 109 | + path: '/upload', |
| 110 | + body: payload, |
| 111 | + headers: { 'Content-Type' => 'application/json' } |
| 112 | + ) |
| 113 | + upload_json = parse_response(upload_response) |
| 114 | + upload_id = upload_json.fetch('upload_id') |
| 115 | + upload_url = upload_json.fetch('uploadURL') |
| 116 | + Logger.debug("Got upload ID: #{upload_id}") |
| 117 | + |
| 118 | + warning = upload_json['warning'] |
| 119 | + Logger.warn(warning) if warning |
| 120 | + |
| 121 | + [upload_url, upload_id] |
| 122 | + end |
| 123 | + |
| 124 | + def parse_response(response) |
| 125 | + case response.status |
| 126 | + when 200 |
| 127 | + JSON.parse(response.read) |
| 128 | + when 400 |
| 129 | + error_message = JSON.parse(response.read)['errorMessage'] |
| 130 | + raise "Invalid parameters: #{error_message}" |
| 131 | + when 401, 403 |
| 132 | + raise 'Invalid API token' |
| 133 | + else |
| 134 | + raise "Creating upload failed with status #{response.status}" |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | +end |
0 commit comments