Module: GGSM::MR

Includes:
Email, Submodule, Token
Included in:
Cli
Defined in:
lib/ggsm/command/mr.rb

Constant Summary collapse

@@current_branch =
''
@@target_branch =
''
@@pwd =
''
@@token =
''
@@msg =
''
@@urls =
{}
@@user =
''

Instance Method Summary collapse

Methods included from Email

#send_email

Methods included from Token

#check_token

Methods included from Submodule

#check_submodule, #check_submodule_status, #check_un_commit_code, #correct_dir, #foreach_module, #get_current_branch, #get_head_commit, #get_lastest_msg, #get_lastest_msg_not_merge, #get_lastest_msg_of_module, #get_modified_submodule, #get_submodule, #get_submodule_commit, #is_submodule, #tip_contact_author

Methods included from Hooks

#check_hooks, #cp_files, #cp_hooks, #install_billow, #update_hooks

Instance Method Details

#create_mr(sub) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
# 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	[email protected]: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_tokenObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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