lhj-tools 0.1.87 → 0.1.89
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.
- checksums.yaml +4 -4
- data/lib/lhj/command/yapi/yapi_init.rb +51 -36
- data/lib/lhj/command/yapi.rb +45 -11
- data/lib/lhj/tools/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 636e87e8f38a49680d589ea0899a05f644debe88926734c8f7a2b0bb707e02a1
|
|
4
|
+
data.tar.gz: 170851d7bc279275151e72f6098d992aa728f5738f76075ade79b657e7a9ef03
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1af599df60ad3ad1a91059d55d13971e52de3f081ed365c5fa66ea3a8f7e438a16cf7dd6ffd7724a29c8166dbc338a00e430e13eb53eb6213639a638034121f7
|
|
7
|
+
data.tar.gz: ea2ae987669a7a1a1a925c94e53e96db6bf40c553ade5970fcef9f74fba577840a627a76c2d0ae9ed01248612234c455a8e6d063b8f1391f9041279c11aa5902
|
|
@@ -7,50 +7,22 @@ module Lhj
|
|
|
7
7
|
self.summary = '更新yapi请求cookie'
|
|
8
8
|
self.description = '更新 ~/.lhj/yapi.yml 文件配置'
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
def interface_url
|
|
12
|
-
url = []
|
|
13
|
-
url << 'h'
|
|
14
|
-
url << 't'
|
|
15
|
-
url << 't'
|
|
16
|
-
url << 'p'
|
|
17
|
-
url << ':'
|
|
18
|
-
url << '/'
|
|
19
|
-
url << '/'
|
|
20
|
-
url << 'y'
|
|
21
|
-
url << 'a'
|
|
22
|
-
url << 'p'
|
|
23
|
-
url << 'i'
|
|
24
|
-
url << '.'
|
|
25
|
-
url << 'm'
|
|
26
|
-
url << 'i'
|
|
27
|
-
url << 'g'
|
|
28
|
-
url << 'u'
|
|
29
|
-
url << 'a'
|
|
30
|
-
url << 't'
|
|
31
|
-
url << 'e'
|
|
32
|
-
url << 'c'
|
|
33
|
-
url << 'h'
|
|
34
|
-
url << '.'
|
|
35
|
-
url << 'c'
|
|
36
|
-
url << 'o'
|
|
37
|
-
url << 'm'
|
|
38
|
-
url.join('')
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def request_cookie
|
|
10
|
+
def request_cookie(url)
|
|
42
11
|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[start-maximized])
|
|
43
12
|
driver = Selenium::WebDriver.for(:chrome, capabilities: options)
|
|
44
|
-
driver.get
|
|
13
|
+
driver.get url
|
|
45
14
|
manage = driver.manage
|
|
15
|
+
window = manage.window
|
|
46
16
|
manage.timeouts.implicit_wait = 10
|
|
47
17
|
manage.timeouts.page_load = 10
|
|
48
18
|
# 1.登录
|
|
49
19
|
all_btn = driver.all(tag_name: 'button')
|
|
50
20
|
login_btn = all_btn.find { |ele| ele.text =~ /钉钉登录/ } if all_btn
|
|
51
21
|
login_btn&.click
|
|
22
|
+
window.resize_to(200, 500)
|
|
52
23
|
# 2.获取cookies
|
|
53
|
-
wait = Selenium::WebDriver::Wait.new(timeout:
|
|
24
|
+
wait = Selenium::WebDriver::Wait.new(timeout: 90)
|
|
25
|
+
need_notify = true
|
|
54
26
|
wait.until do
|
|
55
27
|
res = {}
|
|
56
28
|
manage.all_cookies.each do |cookie|
|
|
@@ -58,21 +30,64 @@ module Lhj
|
|
|
58
30
|
res[:uid] = cookie[:value] if cookie[:name] == '_yapi_uid'
|
|
59
31
|
res[:token] = cookie[:value] if cookie[:name] == '_yapi_token'
|
|
60
32
|
end
|
|
33
|
+
if need_notify
|
|
34
|
+
file_name = 'qrcode.png'
|
|
35
|
+
file = File.join(Lhj::Config.instance.home_dir, file_name)
|
|
36
|
+
driver.save_screenshot(file)
|
|
37
|
+
Lhj::OSS::Helper.instance.upload(file_name, file)
|
|
38
|
+
notify_robot(driver.current_url)
|
|
39
|
+
need_notify = false
|
|
40
|
+
end
|
|
61
41
|
return res if res.values.count == 3
|
|
62
42
|
end
|
|
63
43
|
end
|
|
64
44
|
|
|
45
|
+
def robot_url
|
|
46
|
+
'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def notify_robot(url)
|
|
50
|
+
template = Lhj::ErbTemplateHelper.load('yapi_qr_code_notify')
|
|
51
|
+
output = Lhj::ErbTemplateHelper.render(template, { url: url }, '-')
|
|
52
|
+
Lhj::Dingtalk.post_message_robot(robot_url, 'yapi qrcode', output)
|
|
53
|
+
end
|
|
54
|
+
|
|
65
55
|
def config_file
|
|
66
56
|
File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
|
|
67
57
|
end
|
|
68
58
|
|
|
59
|
+
def user_info_file
|
|
60
|
+
File.join(Lhj::Config.instance.home_dir, 'yapi_user.yml')
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def load_config(file)
|
|
64
|
+
YAML.load_file(file)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def save_user(result)
|
|
68
|
+
user_info = load_config(user_info_file) if File.exist?(user_info_file)
|
|
69
|
+
user_info ||= []
|
|
70
|
+
current = user_info.find { |k| k['_yapi_uid'].eql?(result['_yapi_uid']) }
|
|
71
|
+
if current
|
|
72
|
+
current['__wpkreporterwid_'] = result['__wpkreporterwid_']
|
|
73
|
+
current['_yapi_token'] = result['_yapi_token']
|
|
74
|
+
else
|
|
75
|
+
user_info << result
|
|
76
|
+
end
|
|
77
|
+
file_to_save = File.open(user_info_file, 'w+')
|
|
78
|
+
YAML.dump(user_info, file_to_save)
|
|
79
|
+
file_to_save.close
|
|
80
|
+
end
|
|
81
|
+
|
|
69
82
|
def handle
|
|
70
|
-
|
|
71
|
-
|
|
83
|
+
config = load_config(config_file) if File.exist?(config_file)
|
|
84
|
+
# base_url: 'http://yapi.xx.com'
|
|
85
|
+
cookie = request_cookie(config['base_url'])
|
|
72
86
|
if cookie && config
|
|
73
87
|
config['__wpkreporterwid_'] = cookie[:wid]
|
|
74
88
|
config['_yapi_uid'] = cookie[:uid].to_i
|
|
75
89
|
config['_yapi_token'] = cookie[:token]
|
|
90
|
+
save_user({ '__wpkreporterwid_' => cookie[:wid], '_yapi_uid' => cookie[:uid].to_i, '_yapi_token' => cookie[:token] })
|
|
76
91
|
file_to_save = File.open(config_file, 'w+')
|
|
77
92
|
YAML.dump(config, file_to_save)
|
|
78
93
|
file_to_save.close
|
data/lib/lhj/command/yapi.rb
CHANGED
|
@@ -51,8 +51,20 @@ module Lhj
|
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
def process(config_file)
|
|
54
|
-
load_config(config_file)
|
|
55
|
-
|
|
54
|
+
config = load_config(config_file)
|
|
55
|
+
process_config(config)
|
|
56
|
+
res_body = get_interface_api_model(config)
|
|
57
|
+
unless res_body['errcode'].to_i.zero?
|
|
58
|
+
if File.exist?(user_yml_file)
|
|
59
|
+
user_infos = load_config(user_yml_file)
|
|
60
|
+
user_infos.each do |c|
|
|
61
|
+
res_body = get_interface_api_model(c)
|
|
62
|
+
config = c
|
|
63
|
+
break if res_body['errcode'].to_i.zero?
|
|
64
|
+
end
|
|
65
|
+
save_config(config) if res_body['errcode'].to_i.zero?
|
|
66
|
+
end
|
|
67
|
+
end
|
|
56
68
|
unless res_body['errcode'].to_i.zero?
|
|
57
69
|
puts '请执行 lhj yapi-init 初始化配置'.red
|
|
58
70
|
return
|
|
@@ -61,7 +73,7 @@ module Lhj
|
|
|
61
73
|
|
|
62
74
|
# get project info
|
|
63
75
|
project_id = res_body['data']['project_id']
|
|
64
|
-
project_info = get_project_info(project_id)
|
|
76
|
+
project_info = get_project_info(project_id, config)
|
|
65
77
|
# 1.print request result
|
|
66
78
|
res_models = print_res_body_model(res_body)
|
|
67
79
|
# 2.print request json body
|
|
@@ -176,11 +188,15 @@ module Lhj
|
|
|
176
188
|
File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
|
|
177
189
|
end
|
|
178
190
|
|
|
191
|
+
def user_yml_file
|
|
192
|
+
File.join(Lhj::Config.instance.home_dir, 'yapi_user.yml')
|
|
193
|
+
end
|
|
194
|
+
|
|
179
195
|
def load_config(file)
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
196
|
+
YAML.load_file(file)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def process_config(config)
|
|
184
200
|
@config_base_url = config['base_url']
|
|
185
201
|
@config_id = config['id']
|
|
186
202
|
@config_model_pre = config['model_pre']
|
|
@@ -191,6 +207,16 @@ module Lhj
|
|
|
191
207
|
@config_property_mapper = config['property_mapper']
|
|
192
208
|
end
|
|
193
209
|
|
|
210
|
+
def save_config(config)
|
|
211
|
+
yml = load_config(yml_file)
|
|
212
|
+
yml['__wpkreporterwid_'] = config['__wpkreporterwid_']
|
|
213
|
+
yml['_yapi_uid'] = config['_yapi_uid']
|
|
214
|
+
yml['_yapi_token'] = config['_yapi_token']
|
|
215
|
+
file_to_save = File.open(yml_file, 'w+')
|
|
216
|
+
YAML.dump(yml, file_to_save)
|
|
217
|
+
file_to_save.close
|
|
218
|
+
end
|
|
219
|
+
|
|
194
220
|
def api_id
|
|
195
221
|
@id || @config_id.to_s
|
|
196
222
|
end
|
|
@@ -219,10 +245,14 @@ module Lhj
|
|
|
219
245
|
@config_property_mapper || { 'id' => 'gid' }
|
|
220
246
|
end
|
|
221
247
|
|
|
222
|
-
def get_interface_api_model
|
|
248
|
+
def get_interface_api_model(config)
|
|
223
249
|
uri = URI.parse(interface_url_str)
|
|
224
250
|
req = Net::HTTP::Get.new(uri)
|
|
225
|
-
|
|
251
|
+
header_arr = []
|
|
252
|
+
config.each do |k, v|
|
|
253
|
+
header_arr << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
|
|
254
|
+
end
|
|
255
|
+
req['Cookie'] = header_arr.join('; ')
|
|
226
256
|
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
|
227
257
|
http.request(req)
|
|
228
258
|
end
|
|
@@ -232,11 +262,15 @@ module Lhj
|
|
|
232
262
|
res_json
|
|
233
263
|
end
|
|
234
264
|
|
|
235
|
-
def get_project_info(project_id)
|
|
265
|
+
def get_project_info(project_id, config)
|
|
236
266
|
url = project_url_str(project_id)
|
|
237
267
|
uri = URI.parse(url)
|
|
238
268
|
req = Net::HTTP::Get.new(uri)
|
|
239
|
-
|
|
269
|
+
header_arr = []
|
|
270
|
+
config.each do |k, v|
|
|
271
|
+
header_arr << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
|
|
272
|
+
end
|
|
273
|
+
req['Cookie'] = header_arr.join('; ')
|
|
240
274
|
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
|
241
275
|
http.request(req)
|
|
242
276
|
end
|
data/lib/lhj/tools/version.rb
CHANGED