module GGSM::MR

Public Instance Methods

create_mr(sub) click to toggle source
# File lib/ggsm/command/mr.rb, line 78
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@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
        # params['remove_source_branch'] = true
        uri = URI.parse("https://git.souche-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/ggsm/TOKEN`
            @@token = init_check_token
            create_mr(sub)
            return
        end
      rescue => e
        puts "error: #{e}".red
        exit 1
      end
    end
  end
end
init_check_token() click to toggle source
# File lib/ggsm/command/mr.rb, line 53
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
mr_flow(target_branch, msg) click to toggle source
# File lib/ggsm/command/mr.rb, line 22
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