require 'colorize' require 'oj' require 'net/http' require_relative '../util/submodule' require_relative '../mr/token' require_relative '../mr/email' module DNGG module MR include Submodule include Token include Email @@current_branch = '' @@target_branch = '' @@pwd = '' @@token = '' @@msg = '' @@urls = {} @@user = '' def mr_flow(target_branch, msg) check_submodule(false) @@current_branch = get_current_branch @@target_branch = target_branch if target_branch.start_with?('origin/') @@target_branch = target_branch.split('origin/')[1] end @@pwd = Dir.pwd @@token = init_check_token @@msg = msg foreach_module {|sub| create_mr(sub) } puts '==> 进入主工程:'.yellow create_mr('主工程') content = '' @@urls.each do |sub, url| content = " #{content}\n #{sub}: #{url}" end if content != '' send_email(@@user, @@msg, content) end end def init_check_token token = '' path = '.git/dngg/TOKEN' unless check_token system "vim #{path}" result = IO.read(path).split('# 请输入GitLab private-token') if result.length > 1 token = result[0] end end if token == '' token = IO.read(path).split('# 请输入GitLab private-token')[0] end if token == '' puts '请输入GitLab private-token'.red exit 1 end return token end def create_mr(sub) current_branch_head_commit = get_head_commit("origin/#{@@current_branch}").chomp if current_branch_head_commit!="origin/#{@@current_branch}" && # 判断远程分支是否存在 get_head_commit("origin/#{@@current_branch}") != get_head_commit("origin/#{@@target_branch}") # origin git@gitlab.qima-inc.com:guang-mobile-shop/AiGuangMaxApp.git (push) # origin https://gitlab.qima-inc.com/guang-mobile-shop/AiGuangMaxApp.git (push) result = `git remote -v | grep push` project_name = result.split('.com/')[1] if project_name == nil || project_name.strip == '' project_name = result.split('.com:')[1] end if project_name != nil project_name = project_name.split('.git')[0].gsub('/', '%2F') begin params = {} params['source_branch'] = @@current_branch params['target_branch'] = @@target_branch params['title'] = @@msg # params['remove_source_branch'] = true uri = URI.parse("https://gitlab.qima-inc.com/api/v4/projects/#{project_name}/merge_requests?private_token=#{@@token}") response = Net::HTTP.post_form(uri, params) result = Oj.load(response.body) case response when Net::HTTPSuccess then mr_url = result['web_url'] if @@user == '' @@user = result['author']['username'] end if mr_url != nil @@urls[sub] = mr_url puts mr_url.blue end when Net::HTTPConflict then puts 'Merge Request已存在!' when Net::HTTPNotFound, Net::HTTPUnauthorized Dir.chdir @@pwd `rm -rf .git/dngg/TOKEN` @@token = init_check_token create_mr(sub) return when Net::HTTPForbidden then puts response.body.red end rescue => e puts "error: #{e}".red exit 1 end end end end end end