Class: Pixab::MergeRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/projects/MergeRequest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_manager = RepoManager.new, commands = nil) ⇒ MergeRequest

Returns a new instance of MergeRequest.



17
18
19
20
21
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
# File 'lib/projects/MergeRequest.rb', line 17

def initialize(repo_manager = RepoManager.new, commands = nil)
  @repo_manager = repo_manager
  @repo_type = 2
  @default_commit_msg = "[Feature]"
  @need_merge_origin = true
  @need_creat_mr = true
  @need_push_remote = false

  if commands.nil?
    return
  end
  commands.each_index do |index|
    command = commands[index]
    case command
    when "-a"
      @repo_type = 0
    when "-m"
      @repo_type = 1
    when "--commit-m"
      @default_commit_msg = commands[index + 1]
    when "--no-merge-origin"
      @need_merge_origin = false
    when "--no-mr"
      @need_creat_mr = false
    when "--current-branch"
      @need_creat_mr = false
      @need_push_remote = true
    else
    end
  end
end

Instance Attribute Details

#command_optionsObject (readonly)

Returns the value of attribute command_options.



15
16
17
# File 'lib/projects/MergeRequest.rb', line 15

def command_options
  @command_options
end

#default_commit_msgObject

Returns the value of attribute default_commit_msg.



14
15
16
# File 'lib/projects/MergeRequest.rb', line 14

def default_commit_msg
  @default_commit_msg
end

#need_creat_mrObject

Returns the value of attribute need_creat_mr.



14
15
16
# File 'lib/projects/MergeRequest.rb', line 14

def need_creat_mr
  @need_creat_mr
end

#need_merge_originObject

Returns the value of attribute need_merge_origin.



14
15
16
# File 'lib/projects/MergeRequest.rb', line 14

def need_merge_origin
  @need_merge_origin
end

#need_push_remoteObject

Returns the value of attribute need_push_remote.



14
15
16
# File 'lib/projects/MergeRequest.rb', line 14

def need_push_remote
  @need_push_remote
end

#repo_managerObject (readonly)

Returns the value of attribute repo_manager.



15
16
17
# File 'lib/projects/MergeRequest.rb', line 15

def repo_manager
  @repo_manager
end

#repo_typeObject

Returns the value of attribute repo_type.



14
15
16
# File 'lib/projects/MergeRequest.rb', line 14

def repo_type
  @repo_type
end

#reposObject (readonly)

Returns the value of attribute repos.



15
16
17
# File 'lib/projects/MergeRequest.rb', line 15

def repos
  @repos
end

Instance Method Details

#commitObject

提交代码



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/projects/MergeRequest.rb', line 76

def commit()
  should_commit = false
  repos.each do |repo|
    repo_name = repo["name"]
    FileUtils.cd("#{repo_manager.root_path}/#{repo_name}")
    git_status = `git status --porcelain`
    if !git_status.empty?
      should_commit = true
      break
    end
  end
  
  if should_commit 
    input_msg = Utilities.display_dialog("请输入提交信息:", default_commit_msg.nil? ? "" : default_commit_msg)
    reg = /\[(Feature|Bugfix|Optimization|Debug)\][a-z_A-Z0-9\-\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/",'<>~\·`\?:;|\s]+$/
    commit_msg = input_msg.match(reg)
    if commit_msg.nil?
      puts "Error: commit message is malformed".red
      exit(1)
    end
  
    system "mbox git add .#{command_options}"
    system "mbox git commit -m \"#{commit_msg}\"#{command_options}"
  end
end

#mergeObject

合并代码



103
104
105
106
107
108
109
# File 'lib/projects/MergeRequest.rb', line 103

def merge()
  if need_merge_origin 
    repos.each do |repo|
      system "mbox merge --repo #{repo["name"]}"
    end
  end
end

#push_and_create_mrObject

推送MR



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/projects/MergeRequest.rb', line 112

def push_and_create_mr()
  feature_branch = repo_manager.feature_branch

  reviewers = Utilities.display_dialog("正在创建Merge Request\n请输入审核人员ID:\n子琰(979) 丕臻(1385) 再润(1569) 思保(1922)", "979 1385").split()
  mr_request_assign = ""
  reviewers.each do |reviewer|
    mr_request_assign += " -o merge_request.assign=#{reviewer}"
  end
  mr_source_branch = "-o merge_request.remove_source_branch"

  repos.each do |repo|
    repo_name = repo["name"]
    puts "\n[#{repo_name}]"
    FileUtils.cd("#{repo_manager.root_path}/#{repo_name}")
    current_branch = GitUtils.current_branch
    if current_branch != feature_branch
      puts "\n[!] The repo #{repo_name} is not in feature branch `#{feature_branch}`. Skip it.".yellow
      next
    end
    
    repo_target_branch = repo["target_branch"]          
    
    log_content = `git log origin/#{repo_target_branch}..#{current_branch} --pretty=format:"%H"`
    if log_content.empty?
      puts "\n[!] branch `#{current_branch}` is same as branch `origin/#{repo_target_branch}`. Skip it.".yellow
      next          
    end
    mr_target = "-o merge_request.target=#{repo_target_branch}"
    # mr_title = "-o merge_request.title=#{repo_last_branch}"
    commad = "git push"
    if repo["last_branch"].nil?
      commad += " --set-upstream origin #{current_branch}"
    end
    `#{commad} -o merge_request.create #{mr_target} #{mr_source_branch} #{mr_request_assign}`
  end
end

#push_to_remoteObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/projects/MergeRequest.rb', line 149

def push_to_remote
  feature_branch = repo_manager.feature_branch

  repos.each do |repo|
    repo_name = repo["name"]
    puts "\n[#{repo_name}]"
    FileUtils.cd("#{repo_manager.root_path}/#{repo_name}")
    current_branch = GitUtils.current_branch
    if current_branch != feature_branch
      puts "\n[!] The repo #{repo_name} is not in feature branch `#{feature_branch}`. Skip it.".yellow
      next
    end
    
    repo_target_branch = repo["target_branch"]          
    
    log_content = `git log origin/#{repo_target_branch}..#{current_branch} --pretty=format:"%H"`
    if log_content.empty?
      puts "\n[!] branch `#{current_branch}` is same as branch `origin/#{repo_target_branch}`. Skip it.".yellow
      next          
    end

    commad = "git push"
    if repo["last_branch"].nil?
      commad += " --set-upstream origin #{current_branch}"
    end
    `#{commad}`
  end
end

#read_repo_infosObject

读取组件信息



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/projects/MergeRequest.rb', line 61

def read_repo_infos() 
  main_repo = repo_manager.main_repo
  @command_options = ""
  case repo_type
  when 0
  when 1
    @repos = [main_repo]
    @command_options = " --repo #{main_repo["name"]}"
  else
    @repos = repo_manager.sub_repos
    @command_options = " --no-repo #{main_repo["name"]}"
  end
end

#runObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/projects/MergeRequest.rb', line 49

def run
  read_repo_infos
  commit
  merge
  if need_creat_mr
    push_and_create_mr
  elsif need_push_remote
    push_to_remote
  end
end