Skip to content

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

DEV: `GitUtils` overriding via `config/git-utils-overrides.json` (#37571)

David Taylor339746a413c1
diff --git a/lib/git_utils.rb b/lib/git_utils.rb
index dc926720dde..d357eb23e38 100644
--- a/lib/git_utils.rb
+++ b/lib/git_utils.rb
@@ -2,16 +2,17 @@
class GitUtils
def self.git_version
- self.try_git("git rev-parse HEAD", "unknown")
+ filesystem_overrides["git_version"] || self.try_git("git rev-parse HEAD", "unknown")
end
def self.git_branch
- self.try_git("git branch --show-current", nil) ||
+ filesystem_overrides["git_branch"] || self.try_git("git branch --show-current", nil) ||
self.try_git("git config user.discourse-version", "unknown")
end
def self.full_version
- self.try_git('git describe --dirty --match "v[0-9]*" 2> /dev/null', "unknown")
+ filesystem_overrides["full_version"] ||
+ self.try_git('git describe --dirty --match "v[0-9]*" 2> /dev/null', "unknown")
end
def self.has_commit?(hash)
@@ -36,4 +37,15 @@ class GitUtils
(!value.empty? ? value : nil) || default_value
end
+
+ # The `config/git-utils-override.json` file can be used by hosting providers
+ # to override the git information that Discourse reports in the UI.
+ def self.filesystem_overrides
+ @filesystem_overrides ||=
+ begin
+ JSON.parse(File.read("#{Rails.root}/config/git-utils-overrides.json"))
+ rescue Errno::ENOENT
+ {}
+ end
+ end
end
diff --git a/spec/lib/git_utils_spec.rb b/spec/lib/git_utils_spec.rb
new file mode 100644
index 00000000000..0fe05a895ba
--- /dev/null
+++ b/spec/lib/git_utils_spec.rb
@@ -0,0 +1,113 @@
+# frozen_string_literal: true
+
+RSpec.describe GitUtils do
+ let(:temp_dir) { Dir.mktmpdir }
+
+ before { GitUtils.instance_variable_set(:@filesystem_overrides, nil) }
+
+ after { FileUtils.remove_entry(temp_dir) }
+
+ def run(command)
+ system(command, exception: true)
+ end
+
+ def within_temp_git_repo(&block)
+ Dir.chdir(temp_dir) do
+ run("git init --quiet")
+ run("git config user.email '[email protected]'")
+ run("git config user.name 'Test User'")
+ run("git config commit.gpgsign false")
+ yield
+ end
+ end
+
+ def create_commit(filename = "test.txt")
+ File.write(filename, "content")
+ run("git add #{filename}")
+ run("git commit --quiet -m 'Commit #{filename}'")
+ end
+
+ it "returns git version, branch, full_version, and last_commit_date" do
+ within_temp_git_repo do
+ create_commit
+ run("git checkout --quiet -b feature-branch")
+ run("git tag -a v1.0.0 -m 'Version 1.0.0'")
+
+ expect(GitUtils.git_version).to eq(`git rev-parse HEAD`.strip)
+ expect(GitUtils.git_branch).to eq("feature-branch")
+ expect(GitUtils.full_version).to start_with("v1.0.0")
+ expect(GitUtils.last_commit_date).to be_within(60).of(DateTime.now)
+ end
+ end
+
+ it "returns fallback values when not in a git repo or missing data" do
+ within_temp_git_repo do
+ create_commit
+ expect(GitUtils.full_version).to eq("unknown")
+ end
+
+ Dir.chdir(temp_dir) do
+ FileUtils.rm_rf(".git")
+ expect(GitUtils.git_version).to eq("unknown")
+ end
+ end
+
+ it "falls back to user.discourse-version when in detached HEAD" do
+ within_temp_git_repo do
+ create_commit
+ run("git config user.discourse-version 'v3.2.0'")
+ run("git checkout --quiet --detach HEAD")
+
+ expect(GitUtils.git_branch).to eq("v3.2.0")
+ end
+ end
+
+ describe ".has_commit?" do
+ it "validates commit existence and format" do
+ within_temp_git_repo do
+ create_commit
+ first_sha = `git rev-parse HEAD`.strip
+ create_commit("test2.txt")
+
+ expect(GitUtils.has_commit?(first_sha)).to eq(true)
+ expect(GitUtils.has_commit?("a" * 40)).to eq(false)
+ expect(GitUtils.has_commit?("invalid")).to eq(false)
+ end
+ end
+ end
+
+ describe ".filesystem_overrides" do
+ it "reads overrides from JSON file and applies them to git methods" do
+ FileUtils.mkdir_p(File.join(temp_dir, "config"))
+ File.write(
+ File.join(temp_dir, "config", "git-utils-overrides.json"),
+ {
+ "git_version" => "override-sha",
+ "git_branch" => "override-branch",
+ "full_version" => "override-full",
+ }.to_json,
+ )
+ Rails.stubs(:root).returns(Pathname.new(temp_dir))
+
+ expect(GitUtils.filesystem_overrides).to eq(
+ {
+ "git_version" => "override-sha",
+ "git_branch" => "override-branch",
+ "full_version" => "override-full",
+ },
+ )
+
+ within_temp_git_repo do
+ create_commit
+ expect(GitUtils.git_version).to eq("override-sha")
+ expect(GitUtils.git_branch).to eq("override-branch")
+ expect(GitUtils.full_version).to eq("override-full")
+ end
+ end
+
+ it "returns empty hash when file does not exist" do
+ Rails.stubs(:root).returns(Pathname.new(temp_dir))
+ expect(GitUtils.filesystem_overrides).to eq({})
+ end
+ end
+end