Class: Fastlane::Actions::PgyerJvtdAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



218
219
220
# File 'lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb', line 218

def self.authors
  ["chenlei"]
end

.available_optionsObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb', line 231

def self.available_options
  [
      FastlaneCore::ConfigItem.new(key: :api_key,
                                   env_name: "PGYER_API_KEY",
                                   description: "api_key in your pgyer account",
                                   optional: false,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :user_key,
                                   env_name: "PGYER_USER_KEY",
                                   description: "user_key in your pgyer account",
                                   optional: false,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :apk,
                                   env_name: "PGYER_APK",
                                   description: "Path to your APK file",
                                   default_value: Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH],
                                   optional: true,
                                   verify_block: proc do |value|
                                     UI.user_error!("Couldn't find apk file at path '#{value}'") unless File.exist?(value)
                                   end,
                                   conflicting_options: [:ipa],
                                   conflict_block: proc do |value|
                                     UI.user_error!("You can't use 'apk' and '#{value.key}' options in one run")
                                   end),
      FastlaneCore::ConfigItem.new(key: :ipa,
                                   env_name: "PGYER_IPA",
                                   description: "Path to your IPA file. Optional if you use the _gym_ or _xcodebuild_ action. For Mac zip the .app. For Android provide path to .apk file",
                                   default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH],
                                   optional: true,
                                   verify_block: proc do |value|
                                     UI.user_error!("Couldn't find ipa file at path '#{value}'") unless File.exist?(value)
                                   end,
                                   conflicting_options: [:apk],
                                   conflict_block: proc do |value|
                                     UI.user_error!("You can't use 'ipa' and '#{value.key}' options in one run")
                                   end),
      FastlaneCore::ConfigItem.new(key: :password,
                                   env_name: "PGYER_PASSWORD",
                                   description: "set password to protect app",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :update_description,
                                   env_name: "PGYER_UPDATE_DESCRIPTION",
                                   description: "set update description for app",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :install_type,
                                   env_name: "PGYER_INSTALL_TYPE",
                                   description: "set install type for app (1=public, 2=password, 3=invite). Please set as a string",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :channel_shortcut,
                                   env_name: "PGYER_CHANNEL",
                                   description: "Use channel short link. Please set as a string",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :dingtalk_access_token,
                                   env_name: "DINGTALK_ACCESS_TOKEN",
                                   description: "Set up webhook and push update messages. Please set as a string",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :is_at_all,
                                   env_name: "DINGTALK_IS_AT_ALL",
                                   description: "set is at all for dingtalk (0=not_all, 1=all). Please set as a string",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :environment,
                                   env_name: "ENVIRONMENT",
                                   description: "set environment (dev, pub). Please set as a string",
                                   optional: true,
                                   type: String)
  ]
end

.descriptionObject



214
215
216
# File 'lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb', line 214

def self.description
  "distribute app to pgyer beta testing service"
end

.detailsObject



226
227
228
229
# File 'lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb', line 226

def self.details
  # Optional:
  "distribute app to pgyer beta testing service"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


305
306
307
308
309
310
311
# File 'lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb', line 305

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
  #
  [:ios, :mac, :android].include?(platform)
  true
end

.return_valueObject



222
223
224
# File 'lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb', line 222

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/fastlane/plugin/pgyer_jvtd/actions/pgyer_jvtd_action.rb', line 9

def self.run(params)
  UI.message("The pgyer plugin is working.")

  api_host = "https://www.pgyer.com/apiv2/app/upload"
  api_key = params[:api_key]
  user_key = params[:user_key]
  access_token = params[:dingtalk_access_token]
  is_at_all = params[:is_at_all]
  environment = params[:environment]

  if is_at_all.nil?
    is_at_all = "0"
  end

  if environment.nil?
    environment = "dev"
  end

  info = nil

  if api_key != "test"
    build_file = [
        params[:ipa],
        params[:apk]
    ].detect { |e| !e.to_s.empty? }

    if build_file.nil?
      UI.user_error!("You have to provide a build file")
    end

    UI.message "build_file: #{build_file}"

    password = params[:password]
    if password.nil?
      password = ""
    end

    update_description = params[:update_description]
    if update_description.nil?
      update_description = ""
    end

    install_type = params[:install_type]
    if install_type.nil?
      install_type = "1"
    end

    channel_shortcut = params[:channel_shortcut]
    if channel_shortcut.nil?
      channel_shortcut = ""
    end

    # start upload
    conn_options = {
        request: {
            timeout: 1000,
            open_timeout: 300
        }
    }

    pgyer_client = Faraday.new(nil, conn_options) do |c|
      c.request :multipart
      c.request :url_encoded
      c.response :json, content_type: /\bjson$/
      c.adapter :net_http
    end

    params = {
        '_api_key' => api_key,
        'userKey' => user_key,
        'buildPassword' => password,
        'buildUpdateDescription' => update_description,
        'buildInstallType' => install_type,
        'buildInstallDate' => "2",
        'buildChannelShortcut' => channel_shortcut,
        'file' => Faraday::UploadIO.new(build_file, 'application/octet-stream')
    }

    UI.message "Start upload #{build_file} to pgyer..."

    response = pgyer_client.post api_host, params
    info = response.body

    if info['code'] != 0
      UI.user_error!("PGYER Plugin Error: #{info['message']}")
      return
    end

    UI.success "Upload success. Visit this URL to see: https://www.pgyer.com/#{info['data']['buildShortcutUrl']}"
  else
  end

  appType = "Android";
  appName = "测试应用"
  appVersion = "1.0.0"
  appBuildVersion = 1
  appIcon = ""
  appUrl = "https://www.pgyer.com/#{channel_shortcut}"
  appEnvironment = "测试环境"
  if environment == "pub"
    appEnvironment = "正式环境"
  end
  appQrCode = appIcon;
  if info != nil
    # 应用类型
    if info['data']['buildType'] == 1 || info['data']['buildType'] == "1"
      appType = "iOS"
    end
    # 应用名称
    appName = info['data']['buildName']
    # 应用版本信息
    appVersion = info['data']['buildVersion']
    # 应用蒲公英版本
    appBuildVersion = info['data']['buildBuildVersion']
    # 图标
    appIcon = "https://appicon.pgyer.com/image/view/app_icons/#{info['data']['buildIcon']}"
    # 地址
    if channel_shortcut.nil?
      appUrl = "https://www.pgyer.com/#{info['data']['buildShortcutUrl']}"
    end
    appQrCode = info['data']['buildQRCodeURL'];
  end

  unless access_token.nil?

    uriStr = "https://oapi.dingtalk.com/robot/send?access_token=#{access_token}";
    uri = URI.parse(uriStr)
    UI.success uriStr

    params = {
        "msgtype" => "text",
        "text" => {
            "content" => "#{appName}#{appType} App最新#{appEnvironment}版本已发布"
        },
        "at" => {
            "atMobiles" => [],
            "isAtAll" => is_at_all == "1"
        }
    }

    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.request_uri)
    req.body = params.to_json
    req.content_type = 'application/json'
    resp = https.request(req)
    info = JSON.parse(resp.body)

    if info['errcode'] != 0
      UI.error('ding talk error message: ' + info["errmsg"])
    end

    params = {
        "msgtype" => "link",
        "link" => {
            "title" => "#{appName}#{appType} App发布",
            "text" => "请下载最新版本#{appVersion}(#{appBuildVersion})\n此版本为#{appEnvironment}",
            "picUrl" => appIcon,
            "messageUrl" => appUrl
        }
    }

    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.request_uri)
    req.body = params.to_json
    req.content_type = 'application/json'
    resp = https.request(req)
    info = JSON.parse(resp.body)

    if info['errcode'] != 0
      UI.error('ding talk error message: ' + info["errmsg"])
    end

    params = {
        "msgtype" => "markdown",
        "markdown" => {
            "title" => "#{appName}#{appType} App最新#{appEnvironment}版本",
            "text" => "> 扫描二维码即可安装\n" +
                "> ![qrcode](#{appQrCode})\n"
        },
        "at" => {
            "atMobiles" => [],
            "isAtAll" => is_at_all == "1"
        }
    }

    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.request_uri)
    req.body = params.to_json
    req.content_type = 'application/json'
    resp = https.request(req)
    info = JSON.parse(resp.body)

    if info['errcode'] != 0
      UI.error('ding talk error message: ' + info["errmsg"])
    end

    UI.success("Successfully send qr code to ding talk!")
  else
  end

end