lg_pod_plugin 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/command/command.rb +2 -0
- data/lib/command/init.rb +34 -0
- data/lib/lg_pod_plugin/database.rb +234 -104
- data/lib/lg_pod_plugin/downloader.rb +17 -53
- data/lib/lg_pod_plugin/gitlab_api.rb +151 -0
- data/lib/lg_pod_plugin/gitlab_archive.rb +287 -0
- data/lib/lg_pod_plugin/gitlab_download.rb +181 -0
- data/lib/lg_pod_plugin/install.rb +37 -20
- data/lib/lg_pod_plugin/l_cache.rb +5 -3
- data/lib/lg_pod_plugin/l_config.rb +83 -0
- data/lib/lg_pod_plugin/l_util.rb +117 -27
- data/lib/lg_pod_plugin/net-ping.rb +41 -0
- data/lib/lg_pod_plugin/podspec.rb +156 -0
- data/lib/lg_pod_plugin/request.rb +73 -75
- data/lib/lg_pod_plugin/string.rb +1 -1
- data/lib/lg_pod_plugin/version.rb +1 -1
- data/lib/lg_pod_plugin.rb +2 -1
- metadata +26 -6
- data/lib/lg_pod_plugin/git_util.rb +0 -331
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51b66b843efb74dad6c4bead07bedda8df047db87f23e69d00b1a6bb326ba7d7
|
4
|
+
data.tar.gz: 553ffdd0eeed2e9f7fd28d858c110a52cfa9a50f00e0cdee64d85fd68c966ede
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0ecf3adb2ac46859ca9b58e02306f279c3fd85084b08cb88e40da4d10aa4fdf2739aaf03c37ce7f64828682835352a3b00786aa4d6bc3cea2561b73052616b3
|
7
|
+
data.tar.gz: 1061f2907c3b2e3b345902277d2f45ca94fcd4a2aba7637f1674f43112441516f3882c40e1b4b67c35d661315fde8f30218492677d43e79f07286926a6b7e478
|
data/lib/command/command.rb
CHANGED
@@ -4,7 +4,9 @@ module LgPodPlugin
|
|
4
4
|
class Command < CLAide::Command
|
5
5
|
require_relative 'cache'
|
6
6
|
require_relative 'update'
|
7
|
+
require_relative 'init'
|
7
8
|
require_relative 'install'
|
9
|
+
require_relative '../lg_pod_plugin/gitlab_api.rb'
|
8
10
|
self.command = 'lg'
|
9
11
|
self.version = VERSION
|
10
12
|
self.abstract_command = true
|
data/lib/command/init.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'claide'
|
2
|
+
require_relative 'command'
|
3
|
+
|
4
|
+
module LgPodPlugin
|
5
|
+
class Command
|
6
|
+
class Init < Command
|
7
|
+
self.command = "init"
|
8
|
+
self.abstract_command = false
|
9
|
+
self.summary = '初始化gitlab projects 信息'
|
10
|
+
attr_accessor :username
|
11
|
+
attr_accessor :password
|
12
|
+
attr_accessor :host
|
13
|
+
self.description = <<-DESC
|
14
|
+
Manipulate the download cache for pods, like printing the cache content
|
15
|
+
or cleaning the pods cache.
|
16
|
+
DESC
|
17
|
+
|
18
|
+
def initialize(argv)
|
19
|
+
self.host = argv.option('host')
|
20
|
+
self.username = argv.option('username')
|
21
|
+
self.password = argv.option('password')
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def run
|
26
|
+
raise unless self.host
|
27
|
+
raise unless self.username
|
28
|
+
raise unless self.password
|
29
|
+
GitLabAPI.request_gitlab_access_token(self.host, self.username, self.password)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,104 +1,234 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
1
|
+
require "sqlite3"
|
2
|
+
require 'singleton'
|
3
|
+
require_relative 'l_cache'
|
4
|
+
|
5
|
+
module LgPodPlugin
|
6
|
+
|
7
|
+
class ProjectModel
|
8
|
+
REQUIRED_ATTRS ||= %i[id name path description ssh_url_to_repo http_url_to_repo web_url path_with_namespace name_with_namespace].freeze
|
9
|
+
attr_accessor(*REQUIRED_ATTRS)
|
10
|
+
|
11
|
+
def initialize(id = nil, name = nil, description = nil, path = nil, ssh_url_to_repo = nil, http_url_to_repo = nil, web_url = nil, name_with_namespace = nil, path_with_namespace = nil)
|
12
|
+
self.id = id
|
13
|
+
self.path = path
|
14
|
+
self.name = name
|
15
|
+
self.web_url = web_url
|
16
|
+
self.description = description
|
17
|
+
self.ssh_url_to_repo = ssh_url_to_repo
|
18
|
+
self.http_url_to_repo = http_url_to_repo
|
19
|
+
self.path_with_namespace = path_with_namespace
|
20
|
+
self.name_with_namespace = name_with_namespace
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class LPodLatestRefs
|
26
|
+
attr_accessor :id
|
27
|
+
attr_accessor :name
|
28
|
+
attr_accessor :tag
|
29
|
+
attr_accessor :git
|
30
|
+
attr_accessor :branch
|
31
|
+
attr_accessor :commit
|
32
|
+
|
33
|
+
def initialize(id, name, git, branch, tag, commit)
|
34
|
+
self.id = id
|
35
|
+
self.git = git
|
36
|
+
self.tag = tag
|
37
|
+
self.name = name
|
38
|
+
self.branch = branch
|
39
|
+
self.commit = commit
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.get_pod_id(name, git, branch, tag)
|
43
|
+
if git && branch
|
44
|
+
key = name + git + branch
|
45
|
+
return Digest::MD5.hexdigest(key)
|
46
|
+
elsif git && tag
|
47
|
+
key = name + git + tag
|
48
|
+
return Digest::MD5.hexdigest(key)
|
49
|
+
else
|
50
|
+
key = name + git
|
51
|
+
return Digest::MD5.hexdigest(key)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
class LUserAuthInfo
|
58
|
+
REQUIRED_ATTRS ||= %i[id username password host access_token refresh_token expires_in].freeze
|
59
|
+
attr_accessor(*REQUIRED_ATTRS)
|
60
|
+
|
61
|
+
def initialize(id = nil, name = nil, pwd = nil, host = nil, token = nil, refresh_token = nil, time = nil)
|
62
|
+
self.id = id
|
63
|
+
self.host = host
|
64
|
+
self.password = pwd
|
65
|
+
self.username = name
|
66
|
+
self.expires_in = time
|
67
|
+
self.access_token = token
|
68
|
+
self.refresh_token = refresh_token
|
69
|
+
end
|
70
|
+
|
71
|
+
# 创建一个userId
|
72
|
+
def self.get_user_id(host)
|
73
|
+
body = host + Dir.home
|
74
|
+
return Digest::MD5.hexdigest(body)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
class LSqliteDb
|
80
|
+
include Singleton
|
81
|
+
REQUIRED_ATTRS ||= %i[db ].freeze
|
82
|
+
attr_accessor(*REQUIRED_ATTRS)
|
83
|
+
K_USER_TABLE = "user_tab"
|
84
|
+
K_USER_PROJECTS = "user_projects"
|
85
|
+
K_POD_LATEST_REFS = "user_pod_latest_refs"
|
86
|
+
|
87
|
+
def self.shared
|
88
|
+
return LSqliteDb.instance
|
89
|
+
end
|
90
|
+
|
91
|
+
# 初始化 db
|
92
|
+
def initialize
|
93
|
+
root_path = LFileManager.download_director.join("database")
|
94
|
+
db_file_path = root_path.join("my.db")
|
95
|
+
if !root_path.exist? || !db_file_path.exist?
|
96
|
+
FileUtils.mkdir(root_path)
|
97
|
+
FileUtils.chdir(root_path)
|
98
|
+
FileUtils.touch("my.db")
|
99
|
+
end
|
100
|
+
# FileUtils.chdir(root_path)
|
101
|
+
self.db = SQLite3::Database.new(db_file_path.to_path)
|
102
|
+
#添加用户表
|
103
|
+
sql1 = "create table if not exists #{K_USER_TABLE}(
|
104
|
+
id varchar(100) primary key not null,
|
105
|
+
username varchar(100),
|
106
|
+
password varchar(100),
|
107
|
+
host varchar(100),
|
108
|
+
access_token varchar(100),
|
109
|
+
refresh_token varchar(100),
|
110
|
+
expires_in TimeStamp NOT NULL DEFAULT CURRENT_TIMESTAMP);"
|
111
|
+
self.db.execute(sql1)
|
112
|
+
|
113
|
+
#添加项目表
|
114
|
+
sql2 = "create table if not exists #{K_USER_PROJECTS}(
|
115
|
+
id varchar(100) primary key not null,
|
116
|
+
name varchar(100),
|
117
|
+
desc varchar(100),
|
118
|
+
path varchar(100),
|
119
|
+
ssh_url_to_repo varchar(100),
|
120
|
+
http_url_to_repo varchar(100),
|
121
|
+
web_url varchar(100),
|
122
|
+
name_with_namespace varchar(100),
|
123
|
+
path_with_namespace varchar(100)
|
124
|
+
);"
|
125
|
+
self.db.execute(sql2)
|
126
|
+
|
127
|
+
#添加项目表
|
128
|
+
sql3 = "create table if not exists #{K_POD_LATEST_REFS}(
|
129
|
+
id varchar(100) primary key not null,
|
130
|
+
name varchar(100),
|
131
|
+
git varchar(100),
|
132
|
+
branch varchar(100),
|
133
|
+
tag varchar(100),
|
134
|
+
sha varchar(100)
|
135
|
+
);"
|
136
|
+
self.db.execute(sql3)
|
137
|
+
|
138
|
+
super
|
139
|
+
end
|
140
|
+
|
141
|
+
public
|
142
|
+
|
143
|
+
def insert_user_info(user)
|
144
|
+
# pp "user.id => #{user.id}"
|
145
|
+
if self.query_user_info(user.id) != nil
|
146
|
+
self.db.execute_batch(
|
147
|
+
"UPDATE #{K_USER_TABLE} SET username = (:username), password = (:password), host = (:host), access_token = (:access_token), expires_in = (:expires_in), refresh_token = (:refresh_token) where (id = :id)", { "username" => user.username, "password" => user.password, "host" => user.host, "access_token" => user.access_token, :expires_in => user.expires_in, :id => user.id, :refresh_token => user.refresh_token }
|
148
|
+
)
|
149
|
+
else
|
150
|
+
self.db.execute("INSERT INTO #{K_USER_TABLE} (id, username, password, host, access_token,refresh_token, expires_in)
|
151
|
+
VALUES (?, ?, ?, ?,?,?,?)", [user.id, user.username, user.password, user.host, user.access_token, user.refresh_token, user.expires_in])
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
public
|
157
|
+
def query_user_info(user_id)
|
158
|
+
user_info = nil
|
159
|
+
self.db.execute("select * from #{K_USER_TABLE} where id = '#{user_id}';") do |row|
|
160
|
+
user_info = LUserAuthInfo.new
|
161
|
+
user_info.id = row[0]
|
162
|
+
user_info.username = row[1]
|
163
|
+
user_info.password = row[2]
|
164
|
+
user_info.host = row[3]
|
165
|
+
user_info.access_token = row[4]
|
166
|
+
user_info.refresh_token = row[5]
|
167
|
+
user_info.expires_in = row[6]
|
168
|
+
end
|
169
|
+
user_info
|
170
|
+
end
|
171
|
+
|
172
|
+
# 保存项目信息到数据库
|
173
|
+
def insert_project(project)
|
174
|
+
if self.query_project_info(project.name, project.http_url_to_repo) != nil
|
175
|
+
self.db.execute_batch(
|
176
|
+
"UPDATE #{K_USER_PROJECTS} SET name = (:name), desc = (:desc), path = (:path), ssh_url_to_repo = (:ssh_url_to_repo), http_url_to_repo = (:http_url_to_repo), web_url = (:web_url), name_with_namespace = (:name_with_namespace), path_with_namespace = (:path_with_namespace) where (id = :id)", { "name" => project.name, "desc" => project.description, "path" => project.path, "ssh_url_to_repo" => project.ssh_url_to_repo, :http_url_to_repo => project.http_url_to_repo, :web_url => project.web_url, :id => project.id , :path_with_namespace => project.path_with_namespace, :name_with_namespace => project.name_with_namespace}
|
177
|
+
)
|
178
|
+
else
|
179
|
+
self.db.execute("INSERT INTO #{K_USER_PROJECTS} (id, name, desc, path, ssh_url_to_repo, http_url_to_repo, web_url,name_with_namespace, path_with_namespace)
|
180
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [project.id, project.name, project.description, project.path, project.ssh_url_to_repo, project.http_url_to_repo, project.web_url, project.name_with_namespace, project.path_with_namespace])
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
# 通过名称查询项目信息
|
185
|
+
def query_project_info(name, git = nil)
|
186
|
+
project_info = nil
|
187
|
+
self.db.execute("select * from #{K_USER_PROJECTS} where name = '#{name}' or path = '#{name}' ;") do |row|
|
188
|
+
name_with_namespace = row[7]
|
189
|
+
path_with_namespace = row[8]
|
190
|
+
next unless git.include?(name_with_namespace) || git.include?(path_with_namespace)
|
191
|
+
id = row[0]
|
192
|
+
path = row[3]
|
193
|
+
name = row[1]
|
194
|
+
web_url = row[6]
|
195
|
+
description = row[2]
|
196
|
+
ssh_url_to_repo = row[4]
|
197
|
+
http_url_to_repo = row[5]
|
198
|
+
project_info = ProjectModel.new(id, name, description, path, ssh_url_to_repo, http_url_to_repo, web_url, name_with_namespace, path_with_namespace)
|
199
|
+
return project_info
|
200
|
+
end
|
201
|
+
return project_info
|
202
|
+
end
|
203
|
+
|
204
|
+
def insert_pod_refs(name, git, branch, tag , commit)
|
205
|
+
id = LPodLatestRefs.get_pod_id(name, git, branch, tag)
|
206
|
+
pod = LPodLatestRefs.new(id, name, git, branch, tag, commit)
|
207
|
+
if self.query_pod_refs(id) != nil
|
208
|
+
self.db.execute_batch(
|
209
|
+
"UPDATE #{K_POD_LATEST_REFS} SET name = (:name), git = (:git), branch = (:branch), tag = (:tag), sha = (:sha) where (id = :id)", { "name" => pod.name, "git" => pod.git, "sha" => pod.commit, "branch" => pod.branch, :tag => pod.tag, :id => pod.id}
|
210
|
+
)
|
211
|
+
else
|
212
|
+
self.db.execute("INSERT INTO #{K_POD_LATEST_REFS} (id, name, git, branch, tag, sha)
|
213
|
+
VALUES (?, ?, ?, ?, ?, ?)", [pod.id, pod.name, pod.git, pod.branch, pod.tag, pod.commit])
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
def query_pod_refs(id)
|
219
|
+
pod_info = nil
|
220
|
+
self.db.execute("select * from #{K_POD_LATEST_REFS} where id = '#{id}';") do |row|
|
221
|
+
id = row[0]
|
222
|
+
name = row[1]
|
223
|
+
git = row[2]
|
224
|
+
branch = row[3]
|
225
|
+
tag = row[4]
|
226
|
+
commit = row[5]
|
227
|
+
pod_info = LPodLatestRefs.new(id, name, git, branch, tag, commit)
|
228
|
+
end
|
229
|
+
return pod_info
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
@@ -6,7 +6,7 @@ module LgPodPlugin
|
|
6
6
|
|
7
7
|
class LDownloader
|
8
8
|
|
9
|
-
REQUIRED_ATTRS ||= %i[git name commit branch tag options].freeze
|
9
|
+
REQUIRED_ATTRS ||= %i[git real_name name commit branch tag options].freeze
|
10
10
|
attr_accessor(*REQUIRED_ATTRS)
|
11
11
|
|
12
12
|
def initialize(name, options = {})
|
@@ -25,67 +25,31 @@ module LgPodPlugin
|
|
25
25
|
else
|
26
26
|
LgPodPlugin.log_green "Using `#{name}`"
|
27
27
|
end
|
28
|
+
hash_map = LRequest.shared.get_cache_key_params
|
28
29
|
# 发现本地有缓存, 不需要更新缓存
|
29
|
-
|
30
|
+
if LRequest.shared.single_git
|
31
|
+
need_download = LRequest.shared.cache.find_pod_cache(name, hash_map)
|
32
|
+
unless need_download
|
33
|
+
hash_map.delete(:commit)
|
34
|
+
need_download = LRequest.shared.cache.find_pod_cache(name, hash_map)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
need_download = LRequest.shared.cache.find_pod_cache(name, hash_map)
|
38
|
+
end
|
30
39
|
if need_download
|
31
40
|
LgPodPlugin.log_green "find the new commit of `#{name}`, Git downloading now."
|
32
41
|
# 本地 git 下载 pod 目录
|
33
42
|
LRequest.shared.git_util.pre_download_git_repository
|
34
43
|
else
|
35
|
-
LRequest.shared.
|
44
|
+
is_delete = LRequest.shared.request_params["is_delete"] ||= false
|
45
|
+
if self.real_name == self.name
|
46
|
+
LRequest.shared.libs.delete(self.name) if is_delete
|
47
|
+
else
|
48
|
+
LRequest.shared.libs.delete(self.real_name) if is_delete
|
49
|
+
end
|
36
50
|
LgPodPlugin.log_green "find the cache of `#{name}`, you can use it now."
|
37
51
|
end
|
38
52
|
|
39
|
-
# 本地clone代码失败跳出去
|
40
|
-
# unless real_pod_path.exist?
|
41
|
-
# return
|
42
|
-
# end
|
43
|
-
# # 切换到本地git仓库目录下
|
44
|
-
# FileUtils.chdir(real_pod_path)
|
45
|
-
# unless real_pod_path.glob("*.git")
|
46
|
-
# return
|
47
|
-
# end
|
48
|
-
# # 使用branch克隆代码
|
49
|
-
# git = Git.open(Pathname("./"))
|
50
|
-
# current_branch = git.current_branch
|
51
|
-
# if current_branch == branch # 要 clone 的分支正好等于当前分支
|
52
|
-
# current_commit = git.log(1).to_s
|
53
|
-
# if new_commit != current_commit && is_update
|
54
|
-
# #删除旧的pod 缓存
|
55
|
-
# self.cache.clean_old_cache(name, git_url, current_commit)
|
56
|
-
# LgPodPlugin.log_green "git pull #{name} origin/#{current_branch}"
|
57
|
-
# self.git_util.should_pull(git, current_branch, new_commit)
|
58
|
-
# current_commit = new_commit
|
59
|
-
# end
|
60
|
-
# hash_map = { :git => git_url }
|
61
|
-
# if current_commit
|
62
|
-
# hash_map[:commit] = current_commit
|
63
|
-
# end
|
64
|
-
# LSqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
|
65
|
-
# LgPodPlugin::LCache.cache_pod(name, real_pod_path, is_update, hash_map)
|
66
|
-
# else
|
67
|
-
# branch_exist = git.branches.local.find { |e| e.to_s == branch }
|
68
|
-
# if branch_exist
|
69
|
-
# LgPodPlugin.log_green "git switch #{name} #{git_url} -b #{branch}"
|
70
|
-
# self.git_util.git_switch(branch)
|
71
|
-
# else
|
72
|
-
# LgPodPlugin.log_green "git checkout #{name} #{git_url} -b #{branch}"
|
73
|
-
# self.git_util.git_checkout(branch)
|
74
|
-
# end
|
75
|
-
# current_commit = git.log(1).to_s
|
76
|
-
# if current_commit != new_commit
|
77
|
-
# LgPodPlugin.log_green "git pull #{name} #{git_url} -b #{branch}"
|
78
|
-
# self.git_util.should_pull(git, current_branch, new_commit)
|
79
|
-
# current_commit = new_commit
|
80
|
-
# end
|
81
|
-
# hash_map = { :git => git_url }
|
82
|
-
# if current_commit
|
83
|
-
# hash_map[:commit] = current_commit
|
84
|
-
# end
|
85
|
-
# LSqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
|
86
|
-
# LgPodPlugin::LCache.cache_pod(name, real_pod_path, is_update, hash_map)
|
87
|
-
# end
|
88
|
-
|
89
53
|
end
|
90
54
|
|
91
55
|
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
require_relative 'database'
|
4
|
+
require_relative 'file_path'
|
5
|
+
|
6
|
+
module LgPodPlugin
|
7
|
+
|
8
|
+
class GitLabAPI
|
9
|
+
|
10
|
+
public
|
11
|
+
# 获取 GitLab access_token
|
12
|
+
def self.request_gitlab_access_token(host, username, password)
|
13
|
+
begin
|
14
|
+
uri = URI("#{host}/oauth/token")
|
15
|
+
hash_map = { "grant_type" => "password", "username" => username, "password" => password }
|
16
|
+
LgPodPlugin.log_green "开始请求 access_token, url => #{uri.to_s} "
|
17
|
+
req = Net::HTTP::Post.new(uri)
|
18
|
+
req.set_form_data(hash_map)
|
19
|
+
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
20
|
+
http.open_timeout = 15
|
21
|
+
http.read_timeout = 15
|
22
|
+
http.request(req)
|
23
|
+
end
|
24
|
+
case res
|
25
|
+
when Net::HTTPSuccess, Net::HTTPRedirection
|
26
|
+
json = JSON.parse(res.body)
|
27
|
+
else
|
28
|
+
LgPodPlugin.log_red "获取 `access_token` 失败, 请检查用户名和密码是否有误!"
|
29
|
+
return
|
30
|
+
end
|
31
|
+
if (json["error"] != nil)
|
32
|
+
raise json["error_description"]
|
33
|
+
end
|
34
|
+
access_token = json["access_token"]
|
35
|
+
refresh_token = json["refresh_token"]
|
36
|
+
expires_in = json["expires_in"] ||= 7200
|
37
|
+
created_at = json["created_at"] ||= Time.now.to_i
|
38
|
+
user_id = LUserAuthInfo.get_user_id(host)
|
39
|
+
user_model = LUserAuthInfo.new(user_id, username, password, host, access_token, refresh_token, (created_at + expires_in))
|
40
|
+
LSqliteDb.shared.insert_user_info(user_model)
|
41
|
+
GitLabAPI.get_user_projects(access_token, host, 1)
|
42
|
+
LgPodPlugin.log_green "请求成功: `access_token` => #{access_token}, expires_in => #{expires_in}"
|
43
|
+
rescue => exception
|
44
|
+
LgPodPlugin.log_red "获取 `access_token` 失败, error => #{exception.to_s}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
#获取用户所有项目
|
49
|
+
def self.get_user_projects(access_token, host, page)
|
50
|
+
begin
|
51
|
+
hash_map = Hash.new
|
52
|
+
hash_map["page"] = page
|
53
|
+
hash_map["per_page"] = 100
|
54
|
+
hash_map["access_token"] = access_token
|
55
|
+
uri = URI("#{host}/api/v4/projects")
|
56
|
+
uri.query = URI.encode_www_form(hash_map)
|
57
|
+
res = Net::HTTP.get_response(uri)
|
58
|
+
array = JSON.parse(res.body) if res.body
|
59
|
+
unless array.is_a?(Array)
|
60
|
+
return
|
61
|
+
end
|
62
|
+
# pp array
|
63
|
+
array.each do |json|
|
64
|
+
id = json["id"]
|
65
|
+
name = json["name"]
|
66
|
+
path = json["path"]
|
67
|
+
web_url = json["web_url"]
|
68
|
+
description = json["description"]
|
69
|
+
ssh_url_to_repo = json["ssh_url_to_repo"]
|
70
|
+
http_url_to_repo = json["http_url_to_repo"]
|
71
|
+
path_with_namespace = json["path_with_namespace"] ||= ""
|
72
|
+
name_with_namespace = (json["name_with_namespace"] ||= "").gsub(/[ ]/, '')
|
73
|
+
project = ProjectModel.new(id, name, description, path, ssh_url_to_repo, http_url_to_repo, web_url, name_with_namespace, path_with_namespace)
|
74
|
+
LSqliteDb.shared.insert_project(project)
|
75
|
+
end
|
76
|
+
if array.count >= 100
|
77
|
+
GitLabAPI.get_user_projects(access_token, host, page + 1)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# 刷新gitlab_token
|
84
|
+
def self.refresh_gitlab_access_token(host, refresh_token)
|
85
|
+
begin
|
86
|
+
hash_map = Hash.new
|
87
|
+
hash_map["scope"] = "api"
|
88
|
+
hash_map["grant_type"] = "refresh_token"
|
89
|
+
hash_map["refresh_token"] = refresh_token
|
90
|
+
uri = URI("#{host}/oauth/token")
|
91
|
+
res = Net::HTTP.post_form(uri, hash_map)
|
92
|
+
json = JSON.parse(res.body) if res.body
|
93
|
+
return nil unless json.is_a?(Hash)
|
94
|
+
error = json["error"]
|
95
|
+
if error != nil
|
96
|
+
error_description = json["error_description"]
|
97
|
+
raise error_description
|
98
|
+
end
|
99
|
+
access_token = json["access_token"]
|
100
|
+
refresh_token = json["refresh_token"]
|
101
|
+
expires_in = json["expires_in"] ||= 7200
|
102
|
+
created_at = json["created_at"] ||= Time.now.to_i
|
103
|
+
user_id = LUserAuthInfo.get_user_id(host)
|
104
|
+
user_model = LSqliteDb.shared.query_user_info(user_id)
|
105
|
+
user_model.expires_in = (created_at + expires_in)
|
106
|
+
user_model.access_token = access_token
|
107
|
+
user_model.refresh_token = refresh_token
|
108
|
+
LSqliteDb.shared.insert_user_info(user_model)
|
109
|
+
return user_model
|
110
|
+
rescue => exception
|
111
|
+
LgPodPlugin.log_yellow "刷新 `access_token` 失败, error => #{exception.to_s}"
|
112
|
+
return nil
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# 通过名称搜索项目信息
|
117
|
+
def self.request_project_info(host,project_name, access_token, git = nil )
|
118
|
+
begin
|
119
|
+
hash_map = Hash.new
|
120
|
+
hash_map["search"] = project_name
|
121
|
+
hash_map["access_token"] = access_token
|
122
|
+
uri = URI("#{host}/api/v4/projects")
|
123
|
+
uri.query = URI.encode_www_form(hash_map)
|
124
|
+
res = Net::HTTP.get_response(uri)
|
125
|
+
array = JSON.parse(res.body) if res.body
|
126
|
+
return nil unless array.is_a?(Array)
|
127
|
+
array.each do |json|
|
128
|
+
path = json["path"] ||= ""
|
129
|
+
path_with_namespace = json["path_with_namespace"] ||= ""
|
130
|
+
name_with_namespace = (json["name_with_namespace"] ||= "").gsub(/[ ]/, '')
|
131
|
+
next unless (name == project_name || path == project_name)
|
132
|
+
next unless git.include?(name_with_namespace) || git.include?(path_with_namespace)
|
133
|
+
id = json["id"]
|
134
|
+
name = json["name"] ||= ""
|
135
|
+
web_url = json["web_url"]
|
136
|
+
description = json["description"]
|
137
|
+
ssh_url_to_repo = json["ssh_url_to_repo"]
|
138
|
+
http_url_to_repo = json["http_url_to_repo"]
|
139
|
+
project = ProjectModel.new(id, name, description, path, ssh_url_to_repo, http_url_to_repo, web_url, name_with_namespace, path_with_namespace)
|
140
|
+
LSqliteDb.shared.insert_project(project)
|
141
|
+
return project
|
142
|
+
end
|
143
|
+
rescue
|
144
|
+
return nil
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
end
|