Class: KCommercial::KCPipeline::AutoCherryPick

Inherits:
Object
  • Object
show all
Defined in:
lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mentioned_listObject

Returns the value of attribute mentioned_list.



22
23
24
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 22

def mentioned_list
  @mentioned_list
end

Instance Method Details

#auto_cherry_pick_in_branch(target_b, current_rb) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 53

def auto_cherry_pick_in_branch(target_b,current_rb)
   diffs = KCBranchDiffs.new(current_rb,target_b).diffs
   unless diffs.empty?
      KCommercial::UI.info"#{current_rb}分支有#{diffs.size}个commit需要cherry--pick到#{target_b}分支"
      KCGit.git.checkout(target_b)

      result = nil
      #尝试整体合入

      all_commits = []
      diffs.reverse_each do |diff|
         all_commits << diff.ori_commit_id
      end
      rs = KCGit.git.branch.cherry_pick_all(all_commits)
      unless rs.code == 0
         roll_back_cherry_pick
         #尝试单个cherry pick
         result = cherry_pick_by_one(diffs,target_b,current_rb)
      else
         KCommercial::UI.info"开始推送#{all_commits.size}个commit 到#{target_b}分支"
         KCGit.git.push("origin",target_b)
         result = AutoCherryPickResult.new(target_b:target_b,conflicts:[],cherry_picks:diffs,source_b: current_rb)
      end

   else
      KCommercial::UI.info"不需要自动cherry-pick到#{target_b}分支,退出cherry-pick"
   end
   result
end

#cherry_pick_by_one(diffs, target_b, current_rb) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 83

def cherry_pick_by_one(diffs,target_b,current_rb)
   conflicts = []
   cherry_picks = []
   diffs.reverse_each do |diff|
      rs = KCGit.git.branch.cherry_pick(diff.ori_commit_id)
      unless rs.code == 0
         roll_back_cherry_pick
         KCommercial::UI.info("commitid = #{diff.ori_commit_id},message = #{diff.message},autho = #{diff.author.author} 自动cherry-pick失败,开始回滚")
         conflicts << diff
      else
         cherry_picks << diff
      end
   end
   KCommercial::UI.info"#{cherry_picks.size}个已经 cherry-pick 到#{target_b}分支,有#{conflicts.size}个自动cherry-pick到#{target_b}分支失败"
   unless cherry_picks.size == 0
      KCommercial::UI.info"开始推送#{cherry_picks.size}个commit 到#{target_b}分支"
      KCGit.git.push("origin",target_b)
   end
   result = AutoCherryPickResult.new(target_b:target_b,conflicts:conflicts,cherry_picks:cherry_picks,source_b: current_rb)
   result
end

#diff_to_data(diffs) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 156

def diff_to_data(diffs)
   datas = []
   diffs.each do |c|
      data = []
      data << c.message.split("\n\n")[0] || ""
      data << c.ori_commit_id || ""
      data << c.author.author || ""
      unless mentioned_list.include?(c.author.author)
         mentioned_list << c.author.author
      end
      data << c.date || ""
      datas << data
   end
   datas
end

#diffs_to_markdown(conflicts, title) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 145

def diffs_to_markdown(conflicts,title)
   unless  conflicts.empty?
      labels = ['commit_msg', 'commit', 'author', "date"]
      table = MarkdownTables.make_table(labels, diff_to_data(conflicts), is_rows: true, align: %w[l c c l])
      <<EOF
# #{title}
#{table}
EOF
   end
end

#notify_cherry_picks(rs) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 135

def notify_cherry_picks(rs)
   cp__title = "#{rs.target_b}成功自动cherry-pick<font color = red size =4 >#{rs.cherry_picks.size}个</font>commit"
   msg = diffs_to_markdown(rs.cherry_picks,cp__title)
   kim_model = KimModel.new(MessageType::Markdown, "", msg, "", mentioned_list)
   robot_keys.each do |robot_key|
      kim = Kim.new(robot_key, kim_model)
      kim.notifi_kim
   end
end

#notify_conflicts(rs) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 125

def notify_conflicts(rs)
   conflicts__title = "#{rs.target_b}自动cherry-pick有<font color = red size =4 >#{rs.conflicts.size}个冲突</font>,请对应开发人工处理!"
   msg = diffs_to_markdown(rs.conflicts,conflicts__title)
   kim_model = KimModel.new(MessageType::Markdown, "", msg, "", mentioned_list)
   robot_keys.each do |robot_key|
      kim = Kim.new(robot_key, kim_model)
      kim.notifi_kim
   end
end

#parse_result(results) ⇒ Object

解析结果,发送通知



114
115
116
117
118
119
120
121
122
123
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 114

def parse_result(results)
   results.each do |rs|
      unless rs.conflicts.empty?
         notify_conflicts(rs)
      end
      unless rs.cherry_picks.empty?
         notify_cherry_picks(rs)
      end
   end
end

#prepareObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 28

def prepare
   #忽略由于环境配置造成的变更 不做提交
   `git reset --hard`
   current_rb = KCGit.git.current_branch
   target_branchs = BranchTool.bigger_release_branchs(current_rb)
   target_branchs << "develop"
   rs_list = []
   target_branchs.each do |b|
      KCommercial::UI.info"开始向#{b}分支自动cherry-pick"
      rs = auto_cherry_pick_in_branch(b,current_rb)
      unless rs.nil?
         rs_list << rs
      end
   end
   rs_list
end

#robot_keysObject



49
50
51
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 49

def robot_keys
   @robot_keys ||= KimConfig.configs["AutoCherryPick"]['kim']["robot_key"] || []
end

#roll_back_cherry_pickObject



104
105
106
107
108
109
110
111
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 104

def  roll_back_cherry_pick
   rs = KCGit.git.branch.roll_back
   unless rs.code == 0
      KCommercial::UI.error("自动cherry-pick失败,回滚失败,请手动合入!")
      exit! -1
   end
   KCommercial::UI.debug("自动cherry-pick失败,回滚成功")
end

#runObject



23
24
25
26
# File 'lib/KCommercialPipeline/core/version_pipeline/auto_cherry_pick.rb', line 23

def run
   rs_list = prepare
   parse_result(rs_list)
end