Class: Lhj::Pgyer

Inherits:
Object
  • Object
show all
Defined in:
lib/lhj/helper/pgyer_helper.rb

Overview

pgyer upload

Constant Summary collapse

API_HOST =
'https://api.pgyer.com/apiv2/app'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = :uat) ⇒ Pgyer

Returns a new instance of Pgyer.



18
19
20
21
22
23
24
# File 'lib/lhj/helper/pgyer_helper.rb', line 18

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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def api_key
  @api_key
end

#passwordObject (readonly)

Returns the value of attribute password.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def password
  @password
end

#resultObject (readonly)

Returns the value of attribute result.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def result
  @result
end

#user_keyObject (readonly)

Returns the value of attribute user_key.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def user_key
  @user_key
end

Instance Method Details

#app_download_urlObject



154
155
156
# File 'lib/lhj/helper/pgyer_helper.rb', line 154

def app_download_url
  "https://api.pgyer.com/#{@result['data']['appKey']}"
end

#check_publish_status(client, api_host, api_key, build_key) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lhj/helper/pgyer_helper.rb', line 94

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

#http_clientFaraday::Connection

Returns:

  • (Faraday::Connection)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lhj/helper/pgyer_helper.rb', line 27

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

#last_commit_idObject



113
114
115
# File 'lib/lhj/helper/pgyer_helper.rb', line 113

def last_commit_id
  Lhj::Actions.last_git_commit_hash(true)
end

#render_with_template(template_name) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/lhj/helper/pgyer_helper.rb', line 145

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

#upload_with_path(path, file, des) ⇒ Object

Parameters:

  • path (current path)
  • file (upload file)
  • des (desc)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lhj/helper/pgyer_helper.rb', line 49

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

#upload_with_type(path, file) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/lhj/helper/pgyer_helper.rb', line 117

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