fastlane-plugin-fastci 0.0.30 → 0.0.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,35 +1,70 @@
1
1
  require 'fastlane_core/ui/ui'
2
- require 'fileutils'
2
+ require 'json'
3
+ require 'open3'
4
+ require 'uri'
3
5
 
4
6
  module Fastlane
5
7
  module Helper
6
8
  class DingdingHelper
7
9
 
8
- def self.sendMarkdown(text)
9
-
10
- # 检查钉钉 Token 是否存在
11
- unless CommonHelper.is_validate_string(Environment.dingdingToken)
12
- UI.message("*************| 跳过钉钉消息通知(未配置 Token)|*************")
13
- return
10
+ # 保留原调用入口;JSON 序列化避免更新说明中的引号、换行破坏消息。
11
+ def self.sendMarkdown(text, title: nil)
12
+ token = Environment.dingdingToken.to_s.strip
13
+ if token.empty?
14
+ UI.message('跳过钉钉通知:未配置 DINGDING_TOKEN')
15
+ return false
14
16
  end
15
-
16
- UI.message("*************| 开始钉钉消息通知 |*************")
17
-
18
- curl = %Q{
19
- curl 'https://oapi.dingtalk.com/robot/send?access_token=#{Environment.dingdingToken}' \
20
- -H 'Content-Type:application/json' \
21
- -d '{
22
- "msgtype":"markdown",
23
- "markdown":{
24
- "title":"#{Environment.scheme} 打包通知",
25
- "text":"#{text}"
26
- }
27
- }'
17
+
18
+ payload = {
19
+ msgtype: 'markdown',
20
+ markdown: { title: title || "#{Environment.scheme} 打包通知", text: text }
28
21
  }
29
- system curl
30
- UI.message("")
22
+ UI.message('开始发送钉钉通知')
23
+ response = post_message(token, payload)
24
+ return false unless response
25
+
26
+ unless response[:code].between?(200, 299)
27
+ UI.important("钉钉通知发送失败(HTTP #{response[:code]}),继续原流程")
28
+ return false
29
+ end
30
+
31
+ result = JSON.parse(response[:body])
32
+ if result.is_a?(Hash) && result['errcode'] == 0
33
+ UI.success('钉钉通知发送成功')
34
+ return true
35
+ end
36
+
37
+ # 不输出响应原文或异常消息,避免机器人 Token 随请求地址进入日志。
38
+ error_code = result.is_a?(Hash) && result['errcode'].is_a?(Integer) ? result['errcode'] : '未知'
39
+ UI.important("钉钉拒绝消息(errcode=#{error_code}),请检查机器人配置;继续原流程")
40
+ false
41
+ rescue StandardError => error
42
+ UI.important("钉钉通知发送失败(#{error.class}),请检查网络及机器人配置;继续原流程")
43
+ false
31
44
  end
32
45
 
46
+ # 使用系统 curl 的证书链校验,避开 Ruby OpenSSL 在本机的 CRL 校验错误。
47
+ # URL 和 JSON 通过 stdin 配置传递,不进入进程参数或 shell;保持 TLS 校验与超时。
48
+ def self.post_message(token, payload)
49
+ uri = URI('https://oapi.dingtalk.com/robot/send')
50
+ uri.query = URI.encode_www_form(access_token: token)
51
+ # 第二次 JSON 编码只为 curl 配置中的双引号和反斜杠转义,正文仍为原始 JSON。
52
+ configuration = "url = #{JSON.generate(uri.to_s)}\ndata-binary = #{JSON.generate(JSON.generate(payload))}\n"
53
+ output, _stderr, status = Open3.capture3(
54
+ '/usr/bin/curl', '--disable', '--silent', '--show-error',
55
+ '--connect-timeout', '5', '--max-time', '15',
56
+ '--header', 'Content-Type: application/json; charset=utf-8',
57
+ '--write-out', "\n%{http_code}", '--config', '-', stdin_data: configuration
58
+ )
59
+ unless status.success?
60
+ UI.important("钉钉通知网络请求失败(curl exit=#{status.exitstatus}),继续原流程")
61
+ return nil
62
+ end
63
+
64
+ body, _separator, code = output.rpartition("\n")
65
+ { code: Integer(code, 10), body: body }
66
+ end
67
+ private_class_method :post_message
33
68
  end
34
69
  end
35
70
  end
@@ -0,0 +1,70 @@
1
+ require 'base64'
2
+ require 'json'
3
+ require 'open3'
4
+ require 'tempfile'
5
+ require 'uri'
6
+ require 'fastlane_core/ui/ui'
7
+
8
+ module Fastlane
9
+ module Helper
10
+ # 二维码在构建机下载后内嵌到 Markdown,避免钉钉客户端再次访问分发服务器。
11
+ class DingdingImageHelper
12
+ # Base64 会增加约三分之一体积,为通知正文和更新说明留出空间。
13
+ MAX_IMAGE_BYTES = 8 * 1024
14
+
15
+ def self.base64_source(url)
16
+ uri = URI.parse(url.to_s)
17
+ return unless %w[http https].include?(uri.scheme) && uri.host && !uri.userinfo
18
+
19
+ data = download_image(uri)
20
+ return unless data
21
+
22
+ mime_type = image_mime_type(data)
23
+ unless mime_type
24
+ UI.important('二维码响应不是 PNG、JPEG 或 GIF 图片,通知保留图片链接')
25
+ return nil
26
+ end
27
+
28
+ "data:#{mime_type};base64,#{Base64.strict_encode64(data)}"
29
+ rescue StandardError => error
30
+ # 不输出 URL、响应正文或异常原文,避免签名参数进入打包日志。
31
+ UI.important("二维码内嵌失败(#{error.class}),通知保留图片链接")
32
+ nil
33
+ end
34
+
35
+ def self.download_image(uri)
36
+ Tempfile.create(['fastci-dingding-qr', '.image']) do |file|
37
+ # 不经过 shell;图片地址仅通过 stdin 传递,保留证书校验并限制下载量与耗时。
38
+ _output, _stderr, status = Open3.capture3(
39
+ '/usr/bin/curl', '--disable', '--silent', '--show-error', '--fail',
40
+ '--location', '--max-redirs', '3', '--proto', '=http,https', '--proto-redir', '=http,https',
41
+ '--connect-timeout', '5', '--max-time', '15', '--max-filesize', MAX_IMAGE_BYTES.to_s,
42
+ '--output', file.path, '--config', '-', stdin_data: "url = #{JSON.generate(uri.to_s)}\n"
43
+ )
44
+ unless status.success?
45
+ UI.important("二维码下载失败(curl exit=#{status.exitstatus}),通知保留图片链接")
46
+ return nil
47
+ end
48
+
49
+ # 额外检查实际字节数,兼容未返回 Content-Length 的图片接口。
50
+ data = File.binread(file.path, MAX_IMAGE_BYTES + 1)
51
+ if data.empty? || data.bytesize > MAX_IMAGE_BYTES
52
+ UI.important('二维码为空或超过 8 KB,通知保留图片链接')
53
+ return nil
54
+ end
55
+
56
+ data
57
+ end
58
+ end
59
+
60
+ # 按文件签名识别格式,避免把登录页或错误 HTML 当作图片编码发送。
61
+ def self.image_mime_type(data)
62
+ return 'image/png' if data.start_with?("\x89PNG\r\n\x1a\n".b)
63
+ return 'image/jpeg' if data.start_with?("\xff\xd8\xff".b)
64
+ return 'image/gif' if data.start_with?('GIF87a', 'GIF89a')
65
+ end
66
+
67
+ private_class_method :download_image, :image_mime_type
68
+ end
69
+ end
70
+ end
@@ -9,6 +9,19 @@ module Fastlane
9
9
  ENV['CONNECT_ISSUER_ID']
10
10
  end
11
11
 
12
+ # 商店认证支持 CI 提供的 API Key JSON 密钥文件。
13
+ def self.app_store_connect_api_key_path
14
+ ENV['APP_STORE_CONNECT_API_KEY_PATH'].to_s.strip
15
+ end
16
+
17
+ # 未指定独立 P8 路径时,保留证书目录下 AuthKey_<KEY_ID>.p8 的原有约定。
18
+ def self.app_store_connect_p8_path
19
+ configured_path = ENV['APP_STORE_CONNECT_P8_PATH'].to_s.strip
20
+ return configured_path unless configured_path.empty?
21
+
22
+ File.join(package_file_folder_name.to_s, "AuthKey_#{connect_key_id}.p8")
23
+ end
24
+
12
25
  # 项目配置
13
26
  def self.scheme
14
27
  ENV['SCHEME_NAME']
@@ -107,6 +120,19 @@ module Fastlane
107
120
  ENV['DINGDING_TOKEN']
108
121
  end
109
122
 
123
+ # 商店安装入口由项目配置;上传接口本身不提供 TestFlight 邀请或线上商店地址。
124
+ def self.dingding_testflight_url
125
+ ENV['DINGDING_TESTFLIGHT_URL']
126
+ end
127
+ def self.dingding_app_store_url
128
+ ENV['DINGDING_APP_STORE_URL']
129
+ end
130
+
131
+ # 默认兼容原有密码展示,配置 false 后不在群通知中附带安装密码。
132
+ def self.dingding_include_install_password
133
+ ENV.fetch('DINGDING_INCLUDE_INSTALL_PASSWORD', 'true').casecmp('true').zero?
134
+ end
135
+
110
136
  # Sentry 配置
111
137
  def self.sentry_auth_token
112
138
  ENV['SENTRY_AUTH_TOKEN']
@@ -0,0 +1,184 @@
1
+ require 'cgi'
2
+ require 'uri'
3
+ require 'fastlane/plugin/fastci/helper/dingding_helper'
4
+ require 'fastlane/plugin/fastci/helper/dingding_image_helper'
5
+
6
+ module Fastlane
7
+ module Helper
8
+ # 每次 package 独立保存通知上下文,避免多个 lane 之间串用版本或分发结果。
9
+ class PackageNotificationHelper
10
+ PLATFORM_NAMES = {
11
+ local: '本地构建', pgy: '蒲公英', fir: 'fir', zealot: 'Zealot',
12
+ testflight: 'TestFlight', app_store: 'App Store', checks: '代码检查', sentry: 'Sentry'
13
+ }.freeze
14
+
15
+ def initialize(params)
16
+ @enabled = params[:is_notice_dingding] != false
17
+ @scheme = params[:scheme] || Environment.scheme
18
+ @export_method = params[:export_method] || 'development'
19
+ @configuration = %w[app-store testFlight].include?(@export_method) ? 'Release' : (params[:configuration] || 'Debug')
20
+ @version, @build = params[:version], params[:build]
21
+ @changelog = params[:changelog]
22
+ @started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
23
+ stage(:local, '准备构建')
24
+ end
25
+
26
+ # 在构建前记录实际版本,失败通知也能定位到本次产物。
27
+ def build_info(version:, build:)
28
+ @version, @build = version, build
29
+ end
30
+
31
+ def stage(platform, name)
32
+ @platform, @stage = platform, name
33
+ end
34
+
35
+ # 各渠道只转换自己的返回字段,公共标题与正文始终使用同一模板。
36
+ def distribution_completed(platform, result = {}, store_result: {})
37
+ stage(platform, '上传分发包')
38
+ if !result.is_a?(Hash) || result.empty?
39
+ return notify('上传未完成', details: { '说明' => '上传接口未返回成功结果,请查看构建日志。' }, failed: true)
40
+ end
41
+
42
+ fields = distribution_fields(platform, result, store_result)
43
+ notify('上传完成', **fields)
44
+ end
45
+
46
+ def store_skipped(platform)
47
+ stage(platform, '上传分发包')
48
+ notify('未上传', details: { '说明' => 'IPA 已生成,未配置商店上传认证。' })
49
+ end
50
+
51
+ def checks_completed(names)
52
+ return if names.empty?
53
+
54
+ stage(:checks, '代码检查')
55
+ notify('检查执行完成', details: {
56
+ '已执行' => names.join('、'), '说明' => '检查已执行,发现的问题请查看本次生成的报告。'
57
+ })
58
+ end
59
+
60
+ # 失败消息只说明阶段,不把可能含凭据、个人路径的原始异常发送到群里。
61
+ def failure(store_result: {})
62
+ details = { '失败阶段' => @stage, '说明' => '本次流程已中断,请查看构建日志。' }
63
+ if [:testflight, :app_store].include?(@platform) && !store_result.empty?
64
+ details['失败阶段'] = store_result[:stage]
65
+ details['IPA'] = store_result[:ipa_uploaded] ? '已上传' : '尚未确认上传完成'
66
+ end
67
+ notify('执行失败', details: details, failed: true)
68
+ end
69
+
70
+ private
71
+
72
+ def distribution_fields(platform, result, store_result)
73
+ case platform
74
+ when :pgy
75
+ {
76
+ links: { '安装页面' => value(result, :fastlaneAddedWholeVisitUrl) },
77
+ qr_code: value(result, :buildQRCodeURL), password: Environment.pgy_password
78
+ }
79
+ when :fir
80
+ { links: { '安装页面' => value(result, :download_url) }, password: Environment.fir_password }
81
+ when :zealot
82
+ {
83
+ links: { '版本详情' => value(result, :release_url), '安装页面' => value(result, :install_url) },
84
+ qr_code: value(result, :qrcode_url),
85
+ details: { 'Release ID' => value(result, :release_id), 'dSYM' => value(result, :dsym_status) || '未开启' }
86
+ }
87
+ when :testflight
88
+ {
89
+ links: store_links.merge('TestFlight 测试邀请' => Environment.dingding_testflight_url),
90
+ details: { '说明' => 'IPA 已上传,尚未等待 Apple 处理完成;测试可用性请在 App Store Connect 确认。' }
91
+ }
92
+ when :app_store
93
+ { links: store_links.merge('App Store 线上页面' => Environment.dingding_app_store_url), details: store_details(store_result) }
94
+ else
95
+ {}
96
+ end
97
+ end
98
+
99
+ def store_links
100
+ { 'App Store Connect' => 'https://appstoreconnect.apple.com/apps' }
101
+ end
102
+
103
+ # 使用上传流程确认过的结果,不从配置推断资源已上传或审核已通过。
104
+ def store_details(result)
105
+ details = { '说明' => '上传完成不代表审核通过或已上线;线上页面可能仍为旧版本。' }
106
+ unless result[:resources_enabled]
107
+ return details.merge('商店资源' => '未开启,仅上传 IPA')
108
+ end
109
+
110
+ details.merge(
111
+ '元数据' => result[:metadata_uploaded] ? '已上传' : '未上传',
112
+ '截图' => result[:screenshots_uploaded] ? '已上传' : '未上传',
113
+ '构建选择' => result[:build_selected] ? '已选择本次 Build' : '未执行',
114
+ '提交审核' => result[:review_submitted] ? '已提交' : '未提交'
115
+ )
116
+ end
117
+
118
+ def value(result, key)
119
+ result[key] || result[key.to_s]
120
+ end
121
+
122
+ # 不写空链接、空密码;安装密码开关默认保留旧版的展示行为。
123
+ def notify(status, details: {}, links: {}, qr_code: nil, password: nil, failed: false)
124
+ return unless @enabled && !Environment.dingdingToken.to_s.strip.empty?
125
+
126
+ platform = PLATFORM_NAMES.fetch(@platform)
127
+ title = "#{@scheme} iOS · #{platform} · #{status}"
128
+ fields = {
129
+ '应用' => @scheme, 'Bundle ID' => Environment.bundleID, '分发平台' => platform,
130
+ '状态' => status, '版本' => @version, 'Build' => @build,
131
+ '打包方式' => @export_method, '构建配置' => @configuration
132
+ }.merge(details)
133
+ if Environment.dingding_include_install_password
134
+ fields['安装密码'] = password
135
+ end
136
+ fields['累计耗时'] = "#{(Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started_at).round} 秒"
137
+ fields['通知时间'] = Time.now.strftime('%Y-%m-%d %H:%M:%S %:z')
138
+ sections = ["### #{failed ? '❌' : '📦'} #{markdown(title)}"]
139
+ sections << fields.map { |key, val| "- **#{key}**:#{markdown(val)}" unless val.to_s.strip.empty? }.compact.join("\n")
140
+ sections.concat(link_sections(links, qr_code))
141
+ unless @changelog.to_s.strip.empty?
142
+ # 为长更新日志留出消息空间;截断的是展示文案,不影响平台上传内容。
143
+ notes = @changelog.to_s[0, 1000]
144
+ notes += '…(更新说明已截断)' if @changelog.to_s.length > 1000
145
+ # 普通换行会被 Markdown 合并成段落;两个行尾空格明确要求换行。
146
+ # 仅格式化通知展示,兼容 CRLF,平台上传继续使用原始 changelog。
147
+ formatted_notes = markdown(notes).gsub(/\r\n?/, "\n").gsub("\n", " \n")
148
+ sections << "**更新说明**\n\n#{formatted_notes}"
149
+ end
150
+ DingdingHelper.sendMarkdown(sections.join("\n\n"), title: title)
151
+ end
152
+
153
+ def link_sections(links, qr_code)
154
+ sections = links.map do |label, url|
155
+ safe = web_url(url)
156
+ "[#{label}](#{safe})" if safe
157
+ end.compact
158
+ qr_url = web_url(qr_code)
159
+ if qr_url
160
+ image_source = DingdingImageHelper.base64_source(qr_url)
161
+ sections << "**扫码安装**\n\n![安装二维码](#{image_source})" if image_source
162
+ sections << "[打开二维码](#{qr_url})"
163
+ end
164
+ sections
165
+ end
166
+
167
+ # 仅允许普通网页地址,避免返回值中的括号或凭据破坏 Markdown 链接。
168
+ def web_url(value)
169
+ url = value.to_s.strip
170
+ uri = URI.parse(url)
171
+ return unless %w[https http].include?(uri.scheme) && uri.host && !uri.userinfo
172
+ return if url.match?(/[\s<>]/)
173
+
174
+ url.gsub('(', '%28').gsub(')', '%29')
175
+ rescue URI::InvalidURIError
176
+ nil
177
+ end
178
+
179
+ def markdown(value)
180
+ CGI.escapeHTML(value.to_s).gsub(/[\\`*_\[\]#]/) { |char| "\\#{char}" }
181
+ end
182
+ end
183
+ end
184
+ end
@@ -1,5 +1,5 @@
1
1
  module Fastlane
2
2
  module Fastci
3
- VERSION = "0.0.30"
3
+ VERSION = "0.0.41"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastlane-plugin-fastci
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.30
4
+ version: 0.0.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - watermelon
@@ -79,7 +79,6 @@ dependencies:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
81
  version: 1.0.1
82
- email: watermelon_lp@163.com
83
82
  executables: []
84
83
  extensions: []
85
84
  extra_rdoc_files: []
@@ -88,6 +87,7 @@ files:
88
87
  - README.md
89
88
  - lib/fastlane/plugin/fastci.rb
90
89
  - lib/fastlane/plugin/fastci/actions/analyze_swiftlint_action.rb
90
+ - lib/fastlane/plugin/fastci/actions/app_store_resources_action.rb
91
91
  - lib/fastlane/plugin/fastci/actions/check_zealot_version_action.rb
92
92
  - lib/fastlane/plugin/fastci/actions/detect_duplicity_code_action.rb
93
93
  - lib/fastlane/plugin/fastci/actions/detect_unused_code_action.rb
@@ -103,12 +103,15 @@ files:
103
103
  - lib/fastlane/plugin/fastci/actions/upload_store_action.rb
104
104
  - lib/fastlane/plugin/fastci/actions/upload_zealot_action.rb
105
105
  - lib/fastlane/plugin/fastci/actions/upload_zealot_dsym_action.rb
106
+ - lib/fastlane/plugin/fastci/helper/app_store_resources_helper.rb
106
107
  - lib/fastlane/plugin/fastci/helper/common_helper.rb
107
108
  - lib/fastlane/plugin/fastci/helper/constants.rb
108
109
  - lib/fastlane/plugin/fastci/helper/dingding_helper.rb
110
+ - lib/fastlane/plugin/fastci/helper/dingding_image_helper.rb
109
111
  - lib/fastlane/plugin/fastci/helper/environment.rb
110
112
  - lib/fastlane/plugin/fastci/helper/fastci_zealot_helper.rb
111
113
  - lib/fastlane/plugin/fastci/helper/fir_helper.rb
114
+ - lib/fastlane/plugin/fastci/helper/package_notification_helper.rb
112
115
  - lib/fastlane/plugin/fastci/version.rb
113
116
  - lib/fastlane/plugin/python/generate_duplicity_code_html.py
114
117
  - lib/fastlane/plugin/python/generate_lint_html.py
@@ -119,6 +122,7 @@ licenses:
119
122
  - MIT
120
123
  metadata:
121
124
  rubygems_mfa_required: 'true'
125
+ bug_tracker_uri: https://github.com/watermelon520/fastlane-plugin-fastci/issues
122
126
  rdoc_options: []
123
127
  require_paths:
124
128
  - lib