Class: Pixab::MergeRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/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
# File 'lib/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

  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
    else
    end
  end
end

Instance Attribute Details

#command_optionsObject (readonly)

Returns the value of attribute command_options.



15
16
17
# File 'lib/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/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/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/MergeRequest.rb', line 14

def need_merge_origin
  @need_merge_origin
end

#repo_managerObject (readonly)

Returns the value of attribute repo_manager.



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

def repo_manager
  @repo_manager
end

#repo_typeObject

Returns the value of attribute repo_type.



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

def repo_type
  @repo_type
end

#reposObject (readonly)

Returns the value of attribute repos.



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

def repos
  @repos
end

Instance Method Details

#commitObject

提交代码



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/MergeRequest.rb', line 68

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

合并代码



95
96
97
98
99
100
101
# File 'lib/MergeRequest.rb', line 95

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

#push_and_create_mrObject

推送MR



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
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/MergeRequest.rb', line 104

def push_and_create_mr()
  if !need_creat_mr
    return
  end

  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

#read_repo_infosObject

读取组件信息



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/MergeRequest.rb', line 53

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



45
46
47
48
49
50
# File 'lib/MergeRequest.rb', line 45

def run
  read_repo_infos
  commit
  merge
  push_and_create_mr
end