lg_pod_plugin 1.1.0 → 1.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a79947abb1d48bb8932470b1b2a3d030547213a80d8b970c5d4972f0c38bcbbd
4
- data.tar.gz: 9d25979548ab265f31e1963c4a51ea6c1169948b373fcf4ff95fdfee24e2fef4
3
+ metadata.gz: 51b66b843efb74dad6c4bead07bedda8df047db87f23e69d00b1a6bb326ba7d7
4
+ data.tar.gz: 553ffdd0eeed2e9f7fd28d858c110a52cfa9a50f00e0cdee64d85fd68c966ede
5
5
  SHA512:
6
- metadata.gz: 84301cbb6af3ab88f87e59ba09ec644c1ae9fca1964e8ba172ba90b9473e7db7bcd359be79d973ae5ba0307ce51554a3681ffaeaee9b729ed7464b3555b98a3a
7
- data.tar.gz: 2816d57b94ed38897822f3da83c29adcf75e9d678dd3112c6ad348292a3c47250a488812e41b9bcbfa9c37c61490f1d4217a7584193445054867bb556bdc1c1b
6
+ metadata.gz: a0ecf3adb2ac46859ca9b58e02306f279c3fd85084b08cb88e40da4d10aa4fdf2739aaf03c37ce7f64828682835352a3b00786aa4d6bc3cea2561b73052616b3
7
+ data.tar.gz: 1061f2907c3b2e3b345902277d2f45ca94fcd4a2aba7637f1674f43112441516f3882c40e1b4b67c35d661315fde8f30218492677d43e79f07286926a6b7e478
@@ -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
@@ -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
- # require "sqlite3"
2
- # require 'singleton'
3
- # require_relative 'l_cache'
4
- #
5
- # module LgPodPlugin
6
- #
7
- # class LSqliteDb
8
- # include Singleton
9
- # REQUIRED_ATTRS ||= %i[db table_name].freeze
10
- # attr_accessor(*REQUIRED_ATTRS)
11
- # # 初始化 db
12
- # def initialize
13
- # root_path = LFileManager.download_director.join("database")
14
- # db_file_path = root_path.join("my.db")
15
- # if !root_path.exist? || !db_file_path.exist?
16
- # FileUtils.mkdir(root_path)
17
- # FileUtils.chdir(root_path)
18
- # FileUtils.touch("my.db")
19
- # end
20
- # # FileUtils.chdir(root_path)
21
- # self.db = SQLite3::Database.new(db_file_path.to_path)
22
- # #添加表
23
- # # sql = "create table IF NOT EXISTS LgPodTable (id integer primary key autoincrement not null, name varchar(100), branch varchar(100), commit varchar(100));"
24
- # sql = "create table if not exists lg_pod_table(
25
- # id integer primary key autoincrement not null,
26
- # name varchar(100),
27
- # branch varchar(100),
28
- # sha varchar(100),
29
- # tag varchar(100),
30
- # local_path varchar(150),
31
- # update_time TimeStamp NOT NULL DEFAULT CURRENT_TIMESTAMP)"
32
- # self.db.execute(sql)
33
- # self.table_name = "lg_pod_table"
34
- # super
35
- # end
36
- #
37
- # def insert_table(name, branch, sha, tag, path)
38
- # timestamp = Time.now.to_i * 1000
39
- # pod_info = self.select_table(name, branch)
40
- # if pod_info.name
41
- # self.db.execute_batch(
42
- # "UPDATE #{table_name} SET sha = (:sha), tag = (:tag), local_path = (:local_path), update_time = (:update_time) where (name = :name) and (branch = :branch)", {"branch" => branch,"name" => name, "sha" => sha,"tag" => tag, :update_time => timestamp, :local_path => path.to_path}
43
- # )
44
- # else
45
- # self.db.execute("INSERT INTO #{table_name} (name, branch, sha, tag, local_path, update_time)
46
- # VALUES (?, ?, ?, ?,?,?)", [name, branch, sha, tag, path.to_path, timestamp])
47
- # end
48
- # end
49
- #
50
- # def should_clean_pod_info(name, branch)
51
- # current_pod = self.select_table(name, branch)
52
- # if current_pod&.path && !Dir.exist?(current_pod.path)
53
- # self.delete_table(current_pod.name, current_pod.branch)
54
- # end
55
- # array = self.select_tables(name, branch)
56
- # if array.count <= 2
57
- # return
58
- # end
59
- #
60
- # #待删除的 pod 换成
61
- # pod_info = array.first
62
- # if pod_info&.path && Dir.exist?(pod_info.path)
63
- # FileUtils.rm_rf(pod_info.path)
64
- # self.delete_table(pod_info.name, pod_info.branch)
65
- # array.delete(pod_info)
66
- # end
67
- #
68
- # end
69
- #
70
- # def select_table(name, branch)
71
- # pod_info = LCachePodInfo.new
72
- # self.db.execute( "select * from #{self.table_name} where name = '#{name}' and branch = '#{branch}'; ") do |row|
73
- # pod_info.name = row[1]
74
- # pod_info.branch = row[2]
75
- # pod_info.sha = row[3]
76
- # pod_info.tag = row[4]
77
- # pod_info.path = row[5]
78
- # pod_info.timestamp = row[6]
79
- # end
80
- # pod_info
81
- # end
82
- #
83
- # def select_tables(name, branch)
84
- # array = []
85
- # self.db.execute( "select * from #{self.table_name} where name = '#{name}' and branch != '#{branch}' order by update_time;") do |row|
86
- # pod_info = LCachePodInfo.new
87
- # pod_info.name = row[1]
88
- # pod_info.branch = row[2]
89
- # pod_info.sha = row[3]
90
- # pod_info.tag = row[4]
91
- # pod_info.path = row[5]
92
- # pod_info.timestamp = row[6]
93
- # array.append(pod_info)
94
- # end
95
- # array
96
- # end
97
- #
98
- # def delete_table(name, branch)
99
- # self.db.execute("delete from #{self.table_name} where name = ? and branch = ? ;", [name, branch])
100
- # end
101
- #
102
- # end
103
- #
104
- # end
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
- need_download = LRequest.shared.cache.find_pod_cache(name)
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.libs.delete(self.name)
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