Skip to content

Commit 5b62b30

Browse files
committed
Git Commit Sha - fix loose matching error and add comments
1 parent 44404f1 commit 5b62b30

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

Diff for: app/helpers/pages_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ def git_commit_sha
55

66
def git_commit_sha_short
77
full_sha = git_commit_sha
8-
full_sha[-8..-1]
8+
full_sha[-7..-1]
99
end
1010
end

Diff for: app/models/git_commit_sha.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# Retrieves the current git commit SHA of the project
22
class GitCommitSha
33
def self.current_sha
4-
@commit_sha ||= retrieve_sha_from_file.presence || retrieve_sha_from_git
4+
@sha ||= retrieve_sha_from_file.presence || retrieve_sha_from_git
55
end
66

77
def self.current_sha=(sha)
8-
@commit_sha = sha
8+
@sha = sha
99
end
1010

1111
def self.reset_current_sha
1212
self.current_sha = nil
1313
end
1414

15+
# Assumes the git CLI is available. This is not the case in production on Heroku.
1516
def self.retrieve_sha_from_git
1617
`git rev-parse HEAD 2>/dev/null`.to_s.strip
1718
end
1819

20+
# Assumes a .source_version file with SHA inside. A special Heroku buildpack creates this for us in production.
1921
def self.retrieve_sha_from_file
2022
expected_filepath = Rails.root.join(".source_version")
21-
File.exist?(expected_filepath) ? File.read(expected_filepath) : nil
23+
File.exist?(expected_filepath) ? File.read(expected_filepath).to_s.strip : nil
2224
end
2325
end

Diff for: spec/features/pages_spec.rb

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
require "rails_helper"
22

33
shared_examples "Git Commit SHA" do
4-
# Keep existing env variable intact
5-
let!(:old_sha) { ENV["DEPLOYMENT_SHA"] }
6-
after { ENV["DEPLOYMENT_SHA"] = old_sha }
74
background { visit root_path }
85
it "displays the current git commit" do
9-
expect(page).to have_css("#git-commit-sha", text: expected_text)
6+
el = find("#git-commit-sha")
7+
expect(el.text).to eq expected_text
108
end
119
end
1210

1311
feature "Git Commit SHA" do
12+
before do
13+
# sha gets cached as an instance variable, so need to start fresh
14+
GitCommitSha.reset_current_sha
15+
end
16+
1417
context "when .source_version file does not exist" do
1518
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5x" }
1619
let(:expected_text) { "5eead5x" }
17-
before { GitCommitSha.current_sha = sha }
20+
before do
21+
# stub this method since we need to control what the sha actually is
22+
allow(GitCommitSha).to receive(:retrieve_sha_from_git) { sha }
23+
end
1824
it_behaves_like "Git Commit SHA"
1925
end
26+
2027
context "when .source_version file exists" do
2128
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5y" }
2229
let(:expected_text) { "5eead5y" }
23-
before do
24-
`cd #{Rails.root} && echo #{sha} > .source_version`
25-
GitCommitSha.reset_current_sha
26-
end
30+
before { `cd #{Rails.root} && echo #{sha} > .source_version` }
2731
after { `cd #{Rails.root} && rm .source_version` }
2832
it_behaves_like "Git Commit SHA"
2933
end

0 commit comments

Comments
 (0)