A platform for community discussion. Free, open, simple.
DEV: Add support for d-compat branches in plugins/themes (#37984)
Jarek Radosz5472f8fc05f6
diff --git a/lib/version_compatibility.rb b/lib/version_compatibility.rbindex 412648252ba..a0710470bda 100644--- a/lib/version_compatibility.rb+++ b/lib/version_compatibility.rb@@ -75,10 +75,33 @@ module Discoursecheckout_versionend+ def self.find_compatible_git_branch(path)+ current_branch =+ Discourse::Utils.execute_command("git", "-C", path, "branch", "--show-current").strip++ default_branch =+ Discourse::Utils+ .execute_command("git", "-C", path, "symbolic-ref", "--short", "refs/remotes/origin/HEAD")+ .strip+ .sub(%r{\Aorigin/}, "")++ return if current_branch != default_branch++ compat_branch = "d-compat/#{Discourse::VERSION::MAJOR}.#{Discourse::VERSION::MINOR}"+ remote_branch_ref = "refs/remotes/origin/#{compat_branch}"++ Discourse::Utils.execute_command("git", "-C", path, "rev-parse", remote_branch_ref).strip+ rescue Discourse::Utils::CommandError+ end+# Find a compatible resource from a git repodef self.find_compatible_git_resource(path)return unless File.directory?("#{path}/.git")+ if compat_branch_sha = find_compatible_git_branch(path)+ return compat_branch_sha+ end+tree_info =Discourse::Utils.execute_command("git",@@ -108,7 +131,6 @@ module DiscourseDiscourse.find_compatible_resource(compat_resource)rescue InvalidVersionListError => e$stderr.puts "Invalid version list in #{path}"- rescue Discourse::Utils::CommandError => e- nil+ rescue Discourse::Utils::CommandErrorendenddiff --git a/spec/lib/version_compatibility_spec.rb b/spec/lib/version_compatibility_spec.rbindex 45c64041a2f..a05107d8cd5 100644--- a/spec/lib/version_compatibility_spec.rb+++ b/spec/lib/version_compatibility_spec.rb@@ -154,36 +154,80 @@ RSpec.describe Discourse doenddescribe ".find_compatible_git_resource" do- let!(:git_directory) do- path = nil+ let(:compat_branch_name) do+ "d-compat/#{Discourse::VERSION::MAJOR}.#{Discourse::VERSION::MINOR}"+ end- capture_stdout do- # Note the lack of colon between version and hash- path = setup_git_repo(".discourse-compatibility" => "1.0.0.beta1 12f82d5")+ context "when there is a matching d-compat branch" do+ let!(:git_directory) do+ path = setup_git_repo(".discourse-compatibility" => "<= 9999.0: fallback-ref")++ system("git -C #{path} checkout -q -b #{compat_branch_name}", exception: true)+ add_to_git_repo(path, "compat.txt" => "from branch")+ system("git -C #{path} checkout -q -", exception: true)- # Simulate a remote upstream- `cd #{path} && git remote add origin #{path}/.git && git fetch -q`- `cd #{path} && git branch -u origin/$(git rev-parse --abbrev-ref HEAD)`+ setup_remote_upstream(path)+ pathend- path+ after { FileUtils.remove_entry(git_directory) }++ it "returns the branch commit and ignores .discourse-compatibility" do+ compat_branch_sha = `cd #{git_directory} && git rev-parse #{compat_branch_name}`.strip++ expect(Discourse.find_compatible_git_resource(git_directory)).to eq(compat_branch_sha)+ endend- after { FileUtils.remove_entry(git_directory) }+ context "when there is no matching d-compat branch" do+ let!(:git_directory) do+ path = setup_git_repo(".discourse-compatibility" => "<= 9999.0: fallback-ref")+ setup_remote_upstream(path)+ path+ end- it "gracefully handles invalid input" do- output =- capture_stderr { expect(Discourse.find_compatible_git_resource(git_directory)).to be_nil }+ after { FileUtils.remove_entry(git_directory) }- expect(output).to include("Invalid version list")+ it "falls back to .discourse-compatibility" do+ expect(Discourse.find_compatible_git_resource(git_directory)).to eq("fallback-ref")+ endend- it "gracefully handles large .discourse-compatibility files" do- stub_const(Discourse, "MAX_METADATA_FILE_SIZE", 1) do+ context "with an invalid .discourse-compatibility file" do+ let!(:git_directory) do+ path = setup_git_repo(".discourse-compatibility" => "1.0.0.beta1 12f82d5")+ setup_remote_upstream(path)+ path+ end++ after { FileUtils.remove_entry(git_directory) }++ it "gracefully handles invalid input" dooutput =capture_stderr { expect(Discourse.find_compatible_git_resource(git_directory)).to be_nil }- expect(output).to include(Discourse::VERSION_COMPATIBILITY_FILENAME)+ expect(output).to include("Invalid version list")+ end+ end++ context "with a large .discourse-compatibility file" do+ let!(:git_directory) do+ path = setup_git_repo(".discourse-compatibility" => "<= 9999.0: fallback-ref")+ setup_remote_upstream(path)+ path+ end++ after { FileUtils.remove_entry(git_directory) }++ it "gracefully handles large .discourse-compatibility files" do+ stub_const(Discourse, "MAX_METADATA_FILE_SIZE", 1) do+ output =+ capture_stderr do+ expect(Discourse.find_compatible_git_resource(git_directory)).to be_nil+ end++ expect(output).to include(Discourse::VERSION_COMPATIBILITY_FILENAME)+ endendendenddiff --git a/spec/support/helpers.rb b/spec/support/helpers.rbindex 8ac8beb9e1a..83de1aa9cbc 100644--- a/spec/support/helpers.rb+++ b/spec/support/helpers.rb@@ -179,26 +179,38 @@ module Helpersdef setup_git_repo(files)repo_dir = Dir.mktmpdir- `cd #{repo_dir} && git init . #{"--initial-branch=main" if GIT_INITIAL_BRANCH_SUPPORTED}`- `cd #{repo_dir} && git config user.email '[email protected]'`- `cd #{repo_dir} && git config user.name 'The Cool One'`- `cd #{repo_dir} && git config commit.gpgsign 'false'`+ system(+ "git -C #{repo_dir} init -q . #{"--initial-branch=main" if GIT_INITIAL_BRANCH_SUPPORTED}",+ exception: true,+ )+ system("git -C #{repo_dir} config user.email '[email protected]'", exception: true)+ system("git -C #{repo_dir} config user.name 'The Cool One'", exception: true)+ system("git -C #{repo_dir} config commit.gpgsign 'false'", exception: true)files.each do |name, data|FileUtils.mkdir_p(Pathname.new("#{repo_dir}/#{name}").dirname)File.write("#{repo_dir}/#{name}", data)- `cd #{repo_dir} && git add #{name}`+ system("git -C #{repo_dir} add #{name}", exception: true)end- `cd #{repo_dir} && git commit -am 'first commit'`+ system("git -C #{repo_dir} commit -q -am 'first commit'", exception: false)repo_dirend+ def setup_remote_upstream(path)+ system("git -C #{path} remote add origin #{path}/.git", exception: true)+ system("git -C #{path} fetch -q", exception: true)+ branch = `git -C #{path} rev-parse --abbrev-ref HEAD`.strip+ raise "no branch in setup_remote_upstream" if branch.blank?+ system("git -C #{path} branch -u origin/#{branch}", exception: true)+ system("git -C #{path} remote set-head origin #{branch}", exception: true)+ end+def add_to_git_repo(repo_dir, files)files.each do |name, data|FileUtils.mkdir_p(Pathname.new("#{repo_dir}/#{name}").dirname)File.write("#{repo_dir}/#{name}", data)- `cd #{repo_dir} && git add #{name}`+ system("git -C #{repo_dir} add #{name}", exception: true)end- `cd #{repo_dir} && git commit -am 'add #{files.size} files'`+ system("git -C #{repo_dir} commit -q -am 'add #{files.size} files'", exception: true)repo_dirend