@@ -36,11 +36,13 @@ module Secpick
36
36
end
37
37
38
38
def git_commands
39
- [ "git fetch #{ @options [ :remote ] } #{ stable_branch } " ,
40
- "git checkout -B #{ source_branch } #{ @options [ :remote ] } /#{ stable_branch } --no-track" ,
41
- "git cherry-pick #{ @options [ :sha ] } " ,
42
- "git push #{ @options [ :remote ] } #{ source_branch } --no-verify" ,
43
- "git checkout #{ @options [ :branch ] } " ]
39
+ [
40
+ fetch_stable_branch ,
41
+ create_backport_branch ,
42
+ cherry_pick_commit ,
43
+ push_to_remote ,
44
+ checkout_original_branch
45
+ ]
44
46
end
45
47
46
48
def gitlab_params
@@ -62,8 +64,10 @@ module Secpick
62
64
puts "\n Git commands:" . blue
63
65
puts git_commands . join ( "\n " )
64
66
65
- puts "\n Merge request URL:" . blue
66
- puts new_mr_url
67
+ if !@options [ :merge_request ]
68
+ puts "\n Merge request URL:" . blue
69
+ puts new_mr_url
70
+ end
67
71
68
72
puts "\n Merge request params:" . blue
69
73
pp gitlab_params
@@ -74,7 +78,7 @@ module Secpick
74
78
puts stdout . read &.green
75
79
puts stderr . read &.red
76
80
77
- if wait_thr . value . success?
81
+ if wait_thr . value . success? && ! @options [ :merge_request ]
78
82
puts "#{ new_mr_url } ?#{ gitlab_params . to_query } " . blue
79
83
end
80
84
@@ -85,7 +89,7 @@ module Secpick
85
89
end
86
90
87
91
def self . options
88
- { version : nil , branch : nil , sha : nil } . tap do |options |
92
+ { version : nil , branch : nil , sha : nil , merge_request : false } . tap do |options |
89
93
parser = OptionParser . new do |opts |
90
94
opts . banner = "Usage: #{ $0} [options]"
91
95
opts . on ( '-v' , '--version 10.0' , 'Version' ) do |version |
@@ -104,6 +108,10 @@ module Secpick
104
108
options [ :remote ] = remote
105
109
end
106
110
111
+ opts . on ( '--mr' , '--merge-request' , 'Create relevant security Merge Request targeting the stable branch' ) do
112
+ options [ :merge_request ] = true
113
+ end
114
+
107
115
opts . on ( '-d' , '--dry-run' , 'Only show Git commands, without calling them' ) do
108
116
options [ :try ] = true
109
117
end
@@ -129,6 +137,45 @@ module Secpick
129
137
abort ( "Wrong version format #{ options [ :version ] . bold } " . red ) unless options [ :version ] =~ /\A \d *\- \d *\Z /
130
138
end
131
139
end
140
+
141
+ private
142
+
143
+ def checkout_original_branch
144
+ "git checkout #{ @options [ :branch ] } "
145
+ end
146
+
147
+ def push_to_remote
148
+ [
149
+ "git push #{ @options [ :remote ] } #{ source_branch } --no-verify" ,
150
+ *merge_request_push_options
151
+ ] . join ( ' ' )
152
+ end
153
+
154
+ def merge_request_push_options
155
+ return [ ] unless @options [ :merge_request ]
156
+
157
+ [
158
+ "-o mr.create" ,
159
+ "-o mr.target='#{ stable_branch } '" ,
160
+ "-o mr.description='Please apply Security Release template. /milestone %#{ milestone } '"
161
+ ]
162
+ end
163
+
164
+ def cherry_pick_commit
165
+ "git cherry-pick #{ @options [ :sha ] } "
166
+ end
167
+
168
+ def create_backport_branch
169
+ "git checkout -B #{ source_branch } #{ @options [ :remote ] } /#{ stable_branch } --no-track"
170
+ end
171
+
172
+ def fetch_stable_branch
173
+ "git fetch #{ @options [ :remote ] } #{ stable_branch } "
174
+ end
175
+
176
+ def milestone
177
+ @options [ :version ] . gsub ( '-' , '.' )
178
+ end
132
179
end
133
180
end
134
181
0 commit comments