Class: LZCodeGitLab

Inherits:
Object
  • Object
show all
Defined in:
lib/source/LZCodeGitLab.rb

Instance Method Summary collapse

Instance Method Details

#checkUserInfoObject

检查用户信息



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/source/LZCodeGitLab.rb', line 42

def checkUserInfo()
	if File.exist?(UserPathFile)
      	f = File.open(UserPathFile, 'r')
		a = f.readlines.map! { |line| line.chomp }

		if a[0] && a[1]
			$Private_Token = a[0]
			$GitHTTP_Address = a[1]
		else
			createUserInfo()
		end

		f.close
	else
      	createUserInfo()
	end
end

#createUserInfoObject

创建用户信息



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
# File 'lib/source/LZCodeGitLab.rb', line 61

def createUserInfo()
	if $Private_Token.size < 1
		pt = ''
		while pt.length == 0
			print "Please enter a gitlab private token:"
	    	pt = STDIN.gets.chomp
		end
		$Private_Token = pt
	end
	
	if $GitHTTP_Address.size < 1
		ga = ''
		while !(ga =~ /(^http|^https)[:][\/][\/][a-zA-Z0-9]/)
			print "Please enter a gitlab url:"
	    	ga = STDIN.gets.chomp
		end
		$GitHTTP_Address = ga
	end

	f=File.new(UserPathFile,"w+")
  	f.puts $Private_Token
  	f.puts $GitHTTP_Address
  	f.close

end

#creatMR(addr, source_branch, repo_name, msg) ⇒ Object

创建MR



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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/source/LZCodeGitLab.rb', line 268

def creatMR(addr,source_branch,repo_name,msg)

	if source_branch ==nil || source_branch == 'master'
		return false
	end

	checkUserInfo()

	info = getPid(addr,repo_name)

	pid = info["id"]

	fork_pid = info["fork_id"]

	userID = getAssignee(fork_pid)

	# uri = "curl --request POST --header \"PRIVATE-TOKEN:#{$Private_Token}\" --form \"title=NewMR\" --form \"source_branch=#{source_branch}\" --form \"target_branch=master\" --form \"assignee_id=5858\" #{$GitHTTP_Address}/api/v4/projects/#{pid}/merge_requests"

	# result = system(uri)

	# if result == true
	# 	puts 'Create successful!'
	# else
	# 	puts 'Create error!'
	# end

	params = Hash.new 
	params["private_token"] = $Private_Token
	params["source_branch"] = source_branch
	params["target_branch"] = "master"
	params["title"] = msg
	params["assignee_id"] = userID

	uri = URI.parse("#{$GitHTTP_Address}/api/v4/projects/#{pid}/merge_requests")
	
	html_response = nil 

	t = Thread.new {
         startLoading("Create MR")
       }

	begin
   		req = Net::HTTP.post_form(uri, params)
	rescue  StandardError,Timeout::Error, SystemCallError,SocketError, Errno::ECONNREFUSED
		print "\r"
		puts 'URL Access Error!'
		exit
	else
		print "\r"
		html_response = req
	end

	Thread.kill(t)

	if html_response.instance_of? Net::HTTPCreated
		return true
	else
		return false
	end
end

#forkRepo(pid, name) ⇒ Object

Fork id=pid的仓库 该方法调用了:getMatchRepo(pid,repo_name)



227
228
229
230
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
# File 'lib/source/LZCodeGitLab.rb', line 227

def forkRepo(pid,name)

	checkUserInfo()

	params = Hash.new 
	params["private_token"] = $Private_Token
	uri = URI.parse("#{$GitHTTP_Address}/api/v4/projects/#{pid}/fork")

	html_response = nil 

	t = Thread.new {
         startLoading("Forking")
       }

	begin
   		req = Net::HTTP.post_form(uri, params)
	rescue  StandardError,Timeout::Error, SystemCallError,SocketError, Errno::ECONNREFUSED
		print "\r"
		puts 'URL Access Error!'
		exit
	else
		print "\r"
		html_response = req
	end

	result = Hash.new
	if html_response.instance_of? Net::HTTPCreated
		while result.keys.size == 0
			sleep 2.0
			result = getMatchRepo(pid,name)
		end
		Thread.kill(t)
	else
		puts 'Fork error!'
	end

	return result
end

#getAssignee(pid) ⇒ Object

获取assignee_id



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/source/LZCodeGitLab.rb', line 401

def getAssignee(pid)
	
	info = getAssigneeInfo(pid)

	while !(info.count > 0)
		puts "Please check the keywords!"
		info = getAssigneeInfo(pid)
	end

	if info.count > 0
		print "Match list:\n"
		i = 1
		info.each do |temp|
			puts " #{i}:#{temp["name"]} #{temp["username"]}\n"
			i += 1
		end

		num = 0
		while num >info.count || num == 0
			print "Please access the number you want to operate:"
	    	num = STDIN.gets.chomp.to_i
		end
		return info[num-1]["id"];
		else
	end
end

#getAssigneeInfo(pid) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/source/LZCodeGitLab.rb', line 363

def getAssigneeInfo(pid)
	pt = ''
	while pt.length == 0
		print "Please enter the keyword of a assignee:"
    	pt = STDIN.gets.chomp
	end

	uri = "#{$GitHTTP_Address}/api/v4/projects/30123/users?private_token=#{$Private_Token}&per_page=100"

	html_response = nil 

	begin
   		req = open(uri).read
	rescue  StandardError,Timeout::Error, SystemCallError,SocketError, Errno::ECONNREFUSED
		print "\r"
		puts 'URL Access Error!'
		exit
	else
		print "\r"
		html_response = req 
	end

	data = JSON.parse(html_response)

	info = Array.new

	data.each do |d|
		name = d["name"]
		username = d["username"]

		if  username.include?pt or name.include?pt
			info << d
		end
	end
	return info
end

#getMatchRepo(pid, repo_name) ⇒ Object

查询该Fork了id=pid的仓库信息



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/source/LZCodeGitLab.rb', line 200

def getMatchRepo(pid,repo_name)

	checkUserInfo()
	
	uri = "#{$GitHTTP_Address}/api/v4/projects?private_token=#{$Private_Token}&search=#{repo_name}&per_page=100&owned=true"

	data = httpInner("",uri)

	info = Hash.new

	data.each do |d|

		forkInfo = d["forked_from_project"]
		if  forkInfo 
			tpid = forkInfo["id"]
			if pid == tpid
				info["ssh_url_to_repo"] = d["ssh_url_to_repo"]
				info["forked_from_project"] = d["forked_from_project"]
				break
			end 
		end
	end

	return info
end

#getPid(ssh_addr, keyword) ⇒ Object

获取repo_id 和 fork_repo_id



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/source/LZCodeGitLab.rb', line 330

def getPid(ssh_addr,keyword)

	uri = "#{$GitHTTP_Address}/api/v4/projects?private_token=#{$Private_Token}&search=#{keyword}&per_page=100&owned=true&order_by=updated_at"
	
	html_response = nil 

	begin
   		req = open(uri).read
	rescue  StandardError,Timeout::Error, SystemCallError,SocketError, Errno::ECONNREFUSED
		print "\r"
		puts 'URL Access Error!'
		exit
	else
		print "\r"
		html_response = req 
	end

	data = JSON.parse(html_response)

	info = Hash.new

	data.each do |d|
		forkInfo = d["ssh_url_to_repo"]
		if  forkInfo == ssh_addr 
			info["id"] = d["id"]
			info["fork_id"] = d["forked_from_project"]["id"]
			break
		end
	end

	return info
end

#getRepoInfo(keyword) ⇒ Object

查询仓库信息



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
# File 'lib/source/LZCodeGitLab.rb', line 156

def getRepoInfo(keyword)

	checkUserInfo()

	codeInfo = Hash.new

	uri = "#{$GitHTTP_Address}/api/v4/projects?private_token=#{$Private_Token}&search=#{keyword}&per_page=100"

	data = httpInner("Searching",uri)

	info = Array.new

	data.each do |d|
		temp = Hash.new 
		temp["name"] = d["name"]
		temp["web_url"] = d["web_url"]
		temp["ssh_url_to_repo"] = d["ssh_url_to_repo"]
		temp["path"] = d["path"]
		temp["id"] = d["id"]
		temp["forked_from_project"] = d["forked_from_project"]

		info << temp
	end

	if info.count > 0
		print "Search results:\n"
		i = 1
		info.each do |temp|
			puts " #{i}:#{temp["name"]} #{temp["ssh_url_to_repo"]}\n"
			i += 1
		end

		num = 0
		while num >info.count || num == 0
			print "Please access the number you want to operate:"
	    	num = STDIN.gets.chomp.to_i
		end
		codeInfo = info[num-1];
	end

	return codeInfo
end

#httpInner(text, uri) ⇒ Object



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
# File 'lib/source/LZCodeGitLab.rb', line 15

def httpInner(text,uri)

	html_response = nil 

	t = Thread.new {
         startLoading(text)
       }

	begin
   		req = open(uri).read
	rescue  StandardError,Timeout::Error, SystemCallError,SocketError, Errno::ECONNREFUSED
   		print "\r"
		puts 'URL Access Error!'
		exit
	else
		print "\r"
		html_response = req 
	end

	Thread.kill(t)

	data = JSON.parse(html_response)

	return data
end

#removeRepo(keyword) ⇒ Object

删除仓库(关键字)



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
# File 'lib/source/LZCodeGitLab.rb', line 96

def removeRepo(keyword)

	checkUserInfo()

	uri = "#{$GitHTTP_Address}/api/v4/projects?private_token=#{$Private_Token}&search=#{keyword}&per_page=100&owned=true"

	data = httpInner("Searching",uri)

	info = Array.new

	data.each do |d|
		temp = Hash.new 
		temp["name"] = d["name"]
		temp["web_url"] = d["web_url"]
		temp["ssh_url_to_repo"] = d["ssh_url_to_repo"]
		temp["path"] = d["path"]
		temp["id"] = d["id"]
		temp["forked_from_project"] = d["forked_from_project"]

		info << temp
	end

	codeInfo = Hash.new

	if info.count > 0
		print "Search results:\n"
		i = 1
		info.each do |temp|
			puts " #{i}:#{temp["name"]} #{temp["ssh_url_to_repo"]}\n"
			i += 1
		end

		num = 0
		while num >info.count || num == 0
			print "Please access the number you want to operate:"
	    	num = STDIN.gets.chomp.to_i
		end
		codeInfo = info[num-1];

		if codeInfo["id"] != nil

			puts 'Performing delete...'

			result = system("curl --request DELETE --header \"PRIVATE-TOKEN: #{$Private_Token}\" #{$GitHTTP_Address}/api/v4/projects/#{codeInfo["id"]} >/dev/null 2>&1")
		
			if result == true
				puts 'Operation completed!'
			else
				puts 'Operation failure!'
			end
		else
			puts "No search result by the keyword.Please try again."
		end
	else
		puts "No search result by the keyword.Please try again."

	end
end

#resetInfoObject

清除用户信息



88
89
90
91
92
93
# File 'lib/source/LZCodeGitLab.rb', line 88

def resetInfo()
	if File.exist?(UserPathFile)
		File.delete(UserPathFile)
	end
    puts 'Operation completed!'
end

#searchRepoInfo(keyword) ⇒ Object

查询仓库信息



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/source/LZCodeGitLab.rb', line 429

def searchRepoInfo(keyword)

	checkUserInfo()

	codeInfo = Hash.new

	uri = "#{$GitHTTP_Address}/api/v4/projects?private_token=#{$Private_Token}&search=#{keyword}&per_page=100"

	html_response = nil 

	t = Thread.new {
         startLoading("Searching")
       }

	begin
   		req = open(uri).read
	rescue  StandardError,Timeout::Error, SystemCallError,SocketError, Errno::ECONNREFUSED
		print "\r"
		puts 'URL Access Error!'
		exit
	else
		print "\r"
		html_response = req 
	end

	Thread.kill(t)

	data = JSON.parse(html_response)

	info = Array.new

	data.each do |d|
		temp = Hash.new 
		temp["name"] = d["name"]
		temp["web_url"] = d["web_url"]
		temp["ssh_url_to_repo"] = d["ssh_url_to_repo"]
		temp["path"] = d["path"]
		temp["id"] = d["id"]
		temp["forked_from_project"] = d["forked_from_project"]

		info << temp
	end

	if info.count > 0
		print "Search results:\n"
		puts " --------"
		i = 1
		info.each do |temp|
			puts " #{i}:#{temp["name"]}\n #{temp["web_url"]}\n #{temp["ssh_url_to_repo"]}\n #{temp["path"]}\n --------"
			i += 1
		end
		puts 'Operation completed!'
	else
   		puts "No search result by the keyword."
	end

end

#startLoading(tips) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/source/LZCodeGitLab.rb', line 487

def startLoading(tips)
    spin = ['.   ','..  ','... ','....']
    cunt = 0
    while true
      cunt +=1
      str = spin[cunt%4]
      print "#{tips}:#{str}"
      sleep 0.15
      print "\r"
      
    end
end