FIX: Handle .discourse-compatibility syntax errors (#10891)

Previously, any errors in those files would e.g. blow up the update process in docker_manager.
Now it prints out an error and proceeds as if there was no compatibility file.

Includes:

* DEV: Extract setup_git_repo
* DEV: Use `Dir.mktmpdir`
* DEV: Default to `main` branch (The latest versions of git already do this, so to avoid problems do this by default)
This commit is contained in:
Jarek Radosz
2020-10-12 18:25:06 +02:00
committed by GitHub
parent a47c8f0585
commit 6932a373a3
5 changed files with 76 additions and 44 deletions
+12 -2
View File
@@ -16,6 +16,8 @@ module Discourse
end
end
class InvalidVersionListError < StandardError; end
def self.has_needed_version?(current, needed)
Gem::Version.new(current) >= Gem::Version.new(needed)
end
@@ -29,10 +31,16 @@ module Discourse
# 2.4.4.beta6: some-other-branch-ref
# 2.4.2.beta1: v1-tag
def self.find_compatible_resource(version_list, version = ::Discourse::VERSION::STRING)
return unless version_list
version_list = YAML.load(version_list).sort_by { |v, pin| Gem::Version.new(v) }.reverse
begin
version_list = YAML.safe_load(version_list)
rescue Psych::SyntaxError, Psych::DisallowedClass => e
end
raise InvalidVersionListError unless version_list.is_a?(Hash)
version_list = version_list.sort_by { |v, pin| Gem::Version.new(v) }.reverse
# If plugin compat version is listed as less than current Discourse version, take the version/hash listed before.
checkout_version = nil
@@ -54,5 +62,7 @@ module Discourse
return unless File.directory?("#{path}/.git")
compat_resource, std_error, s = Open3.capture3("git -C '#{path}' show HEAD@{upstream}:#{Discourse::VERSION_COMPATIBILITY_FILENAME}")
Discourse.find_compatible_resource(compat_resource) if s.success?
rescue InvalidVersionListError => e
$stderr.puts "Invalid version list in #{path}"
end
end