/
version_bump.rake
422 lines (342 loc) · 13 KB
/
version_bump.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
48
49
50
51
52
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# frozen_string_literal: true
def dry_run?
!!ENV["DRY_RUN"]
end
def test_mode?
ENV["RUNNING_VERSION_BUMP_IN_RSPEC_TESTS"] == "1"
end
class PlannedTag
attr_reader :name, :message
def initialize(name:, message:)
@name = name
@message = message
end
end
class PlannedCommit
attr_reader :version, :tags, :ref
def initialize(version:, tags: [])
@version = version
@tags = tags
end
def perform!
write_version(@version)
git "add", "lib/version.rb"
git "commit", "-m", "Bump version to v#{@version}"
@ref = git("rev-parse", "HEAD").strip
end
end
def read_version_rb
File.read("lib/version.rb")
end
def parse_current_version
version = read_version_rb[/STRING = "(.*)"/, 1]
raise "Unable to parse current version" if version.nil?
puts "Parsed current version: #{version.inspect}"
version
end
def write_version(version)
File.write("lib/version.rb", read_version_rb.sub(/STRING = ".*"/, "STRING = \"#{version}\""))
end
def git(*args, allow_failure: false, silent: false)
puts "> git #{args.inspect}" unless silent
stdout, stderr, status = Open3.capture3({ "LEFTHOOK" => "0" }, "git", *args)
if !status.success? && !allow_failure
raise "Command failed: git #{args.inspect}\n#{stdout.indent(2)}\n#{stderr.indent(2)}"
end
stdout
end
def ref_exists?(ref)
git "rev-parse", "--verify", ref
true
rescue StandardError
false
end
def confirm(msg)
loop do
print "#{msg} (yes/no)..."
break if test_mode?
response = $stdin.gets.strip
case response.downcase
when "no"
raise "Aborted"
when "yes"
break
else
puts "unknown response: #{response}"
end
end
end
def make_commits(commits:, branch:, base:)
raise "You have other staged changes. Aborting." if !git("diff", "--cached").empty?
git "branch", "-D", branch if ref_exists?(branch)
git "checkout", "-b", branch
commits.each(&:perform!)
git("push", "-f", "--set-upstream", "origin", branch)
make_pr(
base: base,
branch: branch,
title:
"Version bump#{"s" if commits.length > 1} for #{base}: #{commits.map { |c| "v#{c.version}" }.join(", ")}",
)
end
def make_pr(base:, branch:, title:)
params = { expand: 1, title: title, body: <<~MD }
> :warning: This PR should not be merged via the GitHub web interface
>
> It should only be merged (via fast-forward) using the associated `bin/rake version_bump:*` task.
MD
if !test_mode?
system(
"open",
"https://github.com/discourse/discourse/compare/#{base}...#{branch}?#{params.to_query}",
exception: true,
)
end
puts "Do not merge the PR via the GitHub web interface. Get it approved, then come back here to continue."
end
def fastforward(base:, branch:)
if dry_run?
puts "[DRY RUN] Skipping fastforward of #{base}"
return
end
confirm "Ready to merge? This will fast-forward #{base} to #{branch}"
begin
git "push", "origin", "#{branch}:#{base}"
rescue => e
raise <<~MSG
#{e.message}
Error occured during fastforward. Maybe another commit was added to `#{base}` since the PR was created, or maybe the PR wasn't approved.
Don't worry, this is not unusual. To update the branch and try again, rerun this script again. The existing PR and approval will be reused.
MSG
end
puts "Merge successful"
end
def stage_tags(commits)
puts "Staging tags locally..."
commits.each do |commit|
commit.tags.each { |tag| git "tag", "-f", "-a", tag.name, "-m", tag.message, commit.ref }
end
end
def push_tags(commits)
tag_names = commits.flat_map { |commit| commit.tags.map(&:name) }
if dry_run?
puts "[DRY RUN] Skipping pushing tags to origin (#{tag_names.join(", ")})"
return
end
confirm "Ready to push tags #{tag_names.join(", ")} to origin?"
tag_names.each { |tag_name| git "push", "-f", "origin", "refs/tags/#{tag_name}" }
end
def with_clean_worktree(origin_branch)
origin_url = git("remote", "get-url", "origin").strip
if !test_mode? && !origin_url.include?("discourse/discourse")
raise "Expected 'origin' remote to point to discourse/discourse (got #{origin_url})"
end
git "fetch", "origin", origin_branch
path = "#{Rails.root}/tmp/version-bump-worktree-#{SecureRandom.hex}"
begin
FileUtils.mkdir_p(path)
git "worktree", "add", path, "origin/#{origin_branch}"
Dir.chdir(path) { yield } # rubocop:disable Discourse/NoChdir
ensure
puts "Cleaning up temporary worktree..."
git "worktree", "remove", "--force", path, silent: true, allow_failure: true
FileUtils.rm_rf(path)
end
end
desc "Stage commits for a beta version bump (e.g. beta1.dev -> beta1 -> beta2.dev). A PR will be created for approval, then the script will prompt to perform the release"
task "version_bump:beta" do
branch = "version_bump/beta"
base = "main"
with_clean_worktree(base) do
current_version = parse_current_version
commits =
if current_version.start_with?("3.1")
# Legacy strategy - no `-dev` suffix
next_version = current_version.sub(/beta(\d+)/) { "beta#{$1.to_i + 1}" }
[
PlannedCommit.new(
version: next_version,
tags: [
PlannedTag.new(name: "beta", message: "latest beta release"),
PlannedTag.new(name: "latest-release", message: "latest release"),
PlannedTag.new(name: "v#{next_version}", message: "version #{next_version}"),
],
),
]
else
raise "Expected current version to end in -dev" if !current_version.end_with?("-dev")
beta_release_version = current_version.sub("-dev", "")
next_dev_version = current_version.sub(/beta(\d+)/) { "beta#{$1.to_i + 1}" }
[
PlannedCommit.new(
version: beta_release_version,
tags: [
PlannedTag.new(name: "beta", message: "latest beta release"),
PlannedTag.new(name: "latest-release", message: "latest release"),
PlannedTag.new(
name: "v#{beta_release_version}",
message: "version #{beta_release_version}",
),
],
),
PlannedCommit.new(version: next_dev_version),
]
end
make_commits(commits: commits, branch: branch, base: base)
fastforward(base: base, branch: branch)
stage_tags(commits)
push_tags(commits)
end
puts "Done!"
end
desc "Stage commits for minor stable version bump (e.g. 3.1.1 -> 3.1.2). A PR will be created for approval, then the script will prompt to perform the release"
task "version_bump:minor_stable" do
base = "stable"
branch = "version_bump/stable"
with_clean_worktree(base) do
current_version = parse_current_version
if current_version !~ /^(\d+)\.(\d+)\.(\d+)$/
raise "Expected current stable version to be in the form X.Y.Z. It was #{current_version}"
end
new_version = current_version.sub(/\.(\d+)\z/) { ".#{$1.to_i + 1}" }
commits = [
PlannedCommit.new(
version: new_version,
tags: [PlannedTag.new(name: "v#{new_version}", message: "version #{new_version}")],
),
]
make_commits(commits: commits, branch: branch, base: base)
fastforward(base: base, branch: branch)
stage_tags(commits)
push_tags(commits)
end
puts "Done!"
end
desc "Stage commits for a major version bump (e.g. 3.1.0.beta6-dev -> 3.1.0.beta6 -> 3.1.0 -> 3.2.0.beta1-dev). A PR will be created for approval, then the script will merge to `main`. Should be passed a version number for the next stable version (e.g. 3.2.0)"
task "version_bump:major_stable_prepare", [:next_major_version_number] do |t, args|
unless args[:next_major_version_number] =~ /\A\d+\.\d+\.\d+\z/
raise "Expected next_major_version number to be in the form X.Y.Z"
end
base = "main"
branch = "version_bump/beta"
with_clean_worktree(base) do
current_version = parse_current_version
# special case for moving away from the 'legacy' release system where we don't use the `-dev` suffix
is_31_release = args[:next_major_version_number] == "3.2.0"
if !current_version.end_with?("-dev") && !is_31_release
raise "Expected current version to end in -dev"
end
beta_release_version =
if is_31_release
# The 3.1.0 beta series didn't use the -dev suffix
current_version.sub(/beta(\d+)/) { "beta#{$1.to_i + 1}" }
else
current_version.sub("-dev", "")
end
next_dev_version = args[:next_major_version_number] + ".beta1-dev"
final_beta_release =
PlannedCommit.new(
version: beta_release_version,
tags: [
PlannedTag.new(name: "beta", message: "latest beta release"),
PlannedTag.new(name: "latest-release", message: "latest release"),
PlannedTag.new(
name: "v#{beta_release_version}",
message: "version #{beta_release_version}",
),
],
)
commits = [final_beta_release, PlannedCommit.new(version: next_dev_version)]
make_commits(commits: commits, branch: branch, base: base)
fastforward(base: base, branch: branch)
stage_tags(commits)
push_tags(commits)
puts <<~MSG
The #{base} branch is now ready for a stable release.
Now run this command to merge the release into the stable branch:
bin/rake "version_bump:major_stable_merge[v#{beta_release_version}]"
MSG
end
end
desc <<~DESC
Stage the merge of a stable version bump into the stable branch.
A PR will be created for approval, then the script will merge to `stable`.
Should be passed the ref of the beta release which should be promoted to stable
(output from the version_bump:major_stable_prepare rake task)
e.g.:
bin/rake "version_bump:major_stable_merge[v3.1.0.beta12]"
DESC
task "version_bump:major_stable_merge", [:version_bump_ref] do |t, args|
merge_ref = args[:version_bump_ref]
raise "Must pass version_bump_ref" unless merge_ref.present?
git "fetch", "origin", merge_ref
raise "Unknown version_bump_ref: #{merge_ref.inspect}" unless ref_exists?(merge_ref)
base = "stable"
branch = "version_bump/stable"
with_clean_worktree(base) do
git "branch", "-D", branch if ref_exists?(branch)
git "checkout", "-b", branch
git "merge", "--no-commit", merge_ref, allow_failure: true
out, status =
Open3.capture2e "git diff --binary #{merge_ref} | patch -p1 -R --no-backup-if-mismatch"
raise "Error applying diff\n#{out}}" unless status.success?
git "add", "."
merged_version = parse_current_version
git "commit", "-m", "Merge v#{merged_version} into #{base}"
diff_to_base = git("diff", merge_ref).strip
raise "There are diffs remaining to #{merge_ref}" unless diff_to_base.empty?
stable_release_version = merged_version.sub(/\.beta\d+\z/, "")
stable_release_commit =
PlannedCommit.new(
version: stable_release_version,
tags: [
PlannedTag.new(
name: "v#{stable_release_version}",
message: "version #{stable_release_version}",
),
],
)
stable_release_commit.perform!
git("push", "-f", "--set-upstream", "origin", branch)
make_pr(base: base, branch: branch, title: "Merge v#{merged_version} into #{base}")
fastforward(base: base, branch: branch)
stage_tags([stable_release_commit])
push_tags([stable_release_commit])
end
end
desc <<~DESC
squash-merge many security fixes into a single branch for review/merge.
Pass a list of comma-separated branches in the SECURITY_FIX_REFS env var, including the remote name.
Pass the name of the destination branch as the argument of the rake task.
e.g.
SECURITY_FIX_REFS='privatemirror/mybranch1,privatemirror/mybranch2' bin/rake "version_bump:stage_security_fixes[main]"
DESC
task "version_bump:stage_security_fixes", [:base] do |t, args|
base = args[:base]
raise "Unknown base: #{base.inspect}" unless %w[stable main].include?(base)
fix_refs = ENV["SECURITY_FIX_REFS"]&.split(",")&.map(&:strip)
raise "No branches specified in SECURITY_FIX_REFS env" if fix_refs.nil? || fix_refs.empty?
fix_refs.each do |ref|
if !ref.include?("/")
raise "Ref #{ref} did not specify an origin. Please specify the origin, e.g. privatemirror/mybranch"
end
end
puts "Staging security fixes for #{base} branch: #{fix_refs.inspect}"
branch = "security/#{base}-security-fixes"
with_clean_worktree(base) do
git "branch", "-D", branch if ref_exists?(branch)
git "checkout", "-b", branch
fix_refs.each do |ref|
origin, origin_branch = ref.split("/", 2)
git "fetch", origin, origin_branch
first_commit_on_branch = git("log", "--format=%H", "origin/#{base}..#{ref}").lines.last.strip
git "cherry-pick", "#{first_commit_on_branch}^..#{ref}"
end
puts "Finished merging commits into a locally-staged #{branch} branch. Git log is:"
puts git("log", "origin/#{base}..#{branch}")
confirm "Check the log above. Ready to push this branch to the origin and create a PR?"
git("push", "-f", "--set-upstream", "origin", branch)
make_pr(base: base, branch: branch, title: "Security fixes for #{base}")
fastforward(base: base, branch: branch)
end
end