Skip to content

A platform for community discussion. Free, open, simple.

DEV: Squash-merge security fixes in release rake task (#38921)

David Taylorba84ecb92223
diff --git a/lib/tasks/release.rake b/lib/tasks/release.rake
index 41a5d735c03..ef2db1b1f85 100644
--- a/lib/tasks/release.rake
+++ b/lib/tasks/release.rake
@@ -186,62 +186,58 @@ namespace :release do
base = args[:base]
raise "Unknown base: #{base.inspect}" unless base.start_with?("release/") || base == "main"
- fix_refs = ENV["SECURITY_FIX_REFS"]&.split(",")&.map(&:strip)
- private_mirror_pr_numbers = []
-
- if fix_refs.nil? || fix_refs.empty?
- json_output, status =
- Open3.capture2(
- "gh",
- "pr",
- "list",
- "--repo",
- "discourse/discourse-private-mirror",
- "--base",
- base,
- "--state",
- "open",
- "--json",
- "number,title,headRefName",
- "--limit",
- "100",
- )
- raise "Failed to fetch PRs from private-mirror" if !status.success?
+ json_output =
+ ReleaseUtils.gh(
+ "pr",
+ "list",
+ "--repo",
+ "discourse/discourse-private-mirror",
+ "--base",
+ base,
+ "--state",
+ "open",
+ "--json",
+ "number,title,body,headRefName",
+ "--limit",
+ "100",
+ capture: true,
+ )
- prs = JSON.parse(json_output)
- raise "No open PRs found targeting #{base} on private-mirror" if prs.empty?
+ prs = JSON.parse(json_output)
+ raise "No open PRs found targeting #{base} on private-mirror" if prs.empty?
- prompt = TTY::Prompt.new
- choices =
- prs.map do |pr|
- { name: "##{pr["number"]}: #{pr["title"]}", value: pr.slice("number", "headRefName") }
- end
+ choices = prs.map { |pr| pr.slice("number", "title", "body", "headRefName") }
- selected =
+ selected =
+ if (pr_numbers = ENV["SECURITY_FIX_PR_NUMBERS"])
+ requested = pr_numbers.split(",").map(&:to_i)
+ choices.select { |pr| requested.include?(pr["number"]) }
+ else
+ prompt = TTY::Prompt.new
+ prompt_choices =
+ choices.map { |pr| { name: "##{pr["number"]}: #{pr["title"]}", value: pr } }
prompt.multi_select(
"Select security fix PRs to include (space to toggle, enter to finish):",
- choices,
+ prompt_choices,
default: [],
- per_page: choices.size,
+ per_page: prompt_choices.size,
)
- raise "No PRs selected" if selected.empty?
-
- fix_refs = selected.map { |pr| "privatemirror/#{pr["headRefName"]}" }
- private_mirror_pr_numbers = selected.map { |pr| pr["number"] }
- end
+ end
+ raise "No PRs selected" if selected.empty?
- puts "Staging security fixes for #{base} branch: #{fix_refs.inspect}"
+ puts "Staging security fixes for #{base} branch: #{selected.map { |pr| pr["headRefName"] }.inspect}"
branch = "security/#{base}-security-fixes"
ReleaseUtils.with_clean_worktree(base) do
- fix_refs.each do |ref|
- origin, origin_branch = ref.split("/", 2)
- ReleaseUtils.git "fetch", origin, origin_branch
+ selected.each do |pr|
+ ReleaseUtils.git "fetch", "privatemirror", pr["headRefName"]
+
+ ReleaseUtils.git "merge", "--squash", "privatemirror/#{pr["headRefName"]}"
+
+ commit_message = "#{pr["title"]}\n\n#{pr["body"]}".strip
- first_commit_on_branch =
- ReleaseUtils.git("log", "--format=%H", "origin/#{base}..#{ref}").lines.last.strip
- ReleaseUtils.git "cherry-pick", "#{first_commit_on_branch}^..#{ref}"
+ ReleaseUtils.git "commit", "-m", commit_message
end
if base == "main" &&
@@ -279,13 +275,13 @@ namespace :release do
ReleaseUtils.merge_pr(base: base, branch: branch)
end
- if private_mirror_pr_numbers.any?
+ if selected.any?
puts "Closing associated PRs in private-mirror..."
- private_mirror_pr_numbers.each do |pr_number|
+ selected.each do |pr|
ReleaseUtils.gh(
"pr",
"close",
- pr_number.to_s,
+ pr["number"].to_s,
"--repo",
"discourse/discourse-private-mirror",
"--delete-branch",
diff --git a/spec/tasks/release_spec.rb b/spec/tasks/release_spec.rb
index 61aca2174d5..54afffcf286 100644
--- a/spec/tasks/release_spec.rb
+++ b/spec/tasks/release_spec.rb
@@ -471,14 +471,47 @@ RSpec.describe "tasks/release" do
let(:origin_main_commits) do
Dir.chdir(origin_path) { git("log", "--pretty=%s", base_branch).lines.map(&:strip) }
end
- let(:security_fixes) { "origin/security-fix-one,origin/security-fix-two" }
+ let(:pr_list_json) do
+ [
+ {
+ "number" => 1,
+ "title" => "Security fix one",
+ "body" => "Description for fix one",
+ "headRefName" => "security-fix-one",
+ },
+ {
+ "number" => 2,
+ "title" => "Security fix two",
+ "body" => "",
+ "headRefName" => "security-fix-two",
+ },
+ ].to_json
+ end
def origin_file(path)
Dir.chdir(origin_path) { git("show", "#{base_branch}:#{path}") }
end
before do
- ENV["SECURITY_FIX_REFS"] = security_fixes
+ allow(ReleaseUtils).to receive(:gh).and_call_original
+ allow(ReleaseUtils).to receive(:gh).with(
+ "pr",
+ "list",
+ "--repo",
+ "discourse/discourse-private-mirror",
+ "--base",
+ base_branch,
+ "--state",
+ "open",
+ "--json",
+ "number,title,body,headRefName",
+ "--limit",
+ "100",
+ capture: true,
+ ).and_return(pr_list_json)
+
+ ENV["SECURITY_FIX_PR_NUMBERS"] = "1,2"
+
Dir.chdir(origin_path) do
git "checkout", "-b", "security-fix-one"
File.write("firstfile.txt", "contents")
@@ -493,20 +526,22 @@ RSpec.describe "tasks/release" do
git "add", "somefile.txt"
git "commit", "-m", "security fix two"
end
+
+ # Add privatemirror as alias for origin so fetch works
+ Dir.chdir(local_path) { git "remote", "add", "privatemirror", origin_path }
end
- after { ENV.delete("SECURITY_FIX_REFS") }
+ after { ENV.delete("SECURITY_FIX_PR_NUMBERS") }
context "when accepting the version bump" do
before { run_task }
- it "cherry-picks all security fix commits in order" do
+ it "squash-merges security fix PRs in order" do
expect(origin_main_commits).to eq(
[
"DEV: Bump development branch to v2025.12.0-latest.1",
- "security fix two",
- "security fix one, commit two",
- "security fix one, commit one",
+ "Security fix two",
+ "Security fix one",
"Initial commit",
],
)
@@ -529,14 +564,9 @@ RSpec.describe "tasks/release" do
run_task
end
- it "cherry-picks security fix commits without a version bump" do
+ it "squash-merges security fix PRs without a version bump" do
expect(origin_main_commits).to eq(
- [
- "security fix two",
- "security fix one, commit two",
- "security fix one, commit one",
- "Initial commit",
- ],
+ ["Security fix two", "Security fix one", "Initial commit"],
)
end
@@ -547,7 +577,16 @@ RSpec.describe "tasks/release" do
context "when targeting a release branch" do
let(:base_branch) { "release/2025.6" }
- let(:security_fixes) { "origin/security-fix-for-release-branch" }
+ let(:pr_list_json) do
+ [
+ {
+ "number" => 1,
+ "title" => "Security fix for release branch",
+ "body" => "",
+ "headRefName" => "security-fix-for-release-branch",
+ },
+ ].to_json
+ end
before do
Dir.chdir(origin_path) do
@@ -572,12 +611,12 @@ RSpec.describe "tasks/release" do
expect(origin_file("lib/version.rb")).to include('STRING = "2025.6.1"')
end
- it "cherry-picks security fixes and adds version bump commit" do
+ it "squash-merges security fixes and adds version bump commit" do
run_task
expect(origin_main_commits.first(3)).to eq(
[
"DEV: Bump release branch to v2025.6.1",
- "security fix for release branch",
+ "Security fix for release branch",
"Initial release branch commit",
],
)