class LZCodeGitLab
Public Instance Methods
checkUserInfo()
click to toggle source
检查用户信息
# 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
creatMR(addr,source_branch,repo_name,msg)
click to toggle source
创建MR
# 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
createUserInfo()
click to toggle source
创建用户信息
# 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
forkRepo(pid,name)
click to toggle source
Fork id=pid的仓库 该方法调用了:getMatchRepo(pid,repo_name)
# 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)
click to toggle source
获取assignee_id
# 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)
click to toggle source
# 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)
click to toggle source
查询该Fork了id=pid的仓库信息
# 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)
click to toggle source
获取repo_id 和 fork_repo_id
# 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)
click to toggle source
查询仓库信息
# 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)
click to toggle source
# 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)
click to toggle source
删除仓库(关键字)
# 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
resetInfo()
click to toggle source
清除用户信息
# File lib/source/LZCodeGitLab.rb, line 88 def resetInfo() if File.exist?(UserPathFile) File.delete(UserPathFile) end puts 'Operation completed!' end
searchRepoInfo(keyword)
click to toggle source
查询仓库信息
# 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)
click to toggle source
# 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