require 'colorize' require 'oj' require 'net/http' require_relative '../util/submodule' require_relative '../mr/token' require_relative '../mr/email' module GGSM module MR include Submodule include Token include Email @@current_branch = '' @@target_branch = '' @@pwd = '' @@token = '' @@msg = '' @@urls = {} def mr_flow(target_branch, msg) check_submodule @@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(@@msg, content) end end def init_check_token token = '' path = '.git/ggsm/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) if get_head_commit("origin/#{@@current_branch}") != get_head_commit("origin/#{@@target_branch}") # origin git@git.souche.com:Destiny_Android/Destiny_Android.git (push) # origin http://git.souche.com/Destiny_Android/Mine.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 uri = URI.parse("http://git.souche.com/api/v4/projects/#{project_name}/merge_requests?private_token=#{@@token}") response = Net::HTTP.post_form(uri, params) result = Oj.load(response.body) if result['message'] == '404 Project Not Found' Dir.chdir @@pwd `rm -rf .git/ggsm/TOKEN` @@token = init_check_token create_mr(sub) return end if Net::HTTPSuccess mr_url = result['web_url'] if mr_url != nil @@urls[sub] = mr_url puts mr_url.blue end end rescue => e puts "error: #{e}".red exit 1 end end end end end end