Skip to content

Commit ff67292

Browse files
committed
Merge pull request shakacode#125 from robwise/display-commit
Add current Git commit hash to view
2 parents 8c37108 + 5b62b30 commit ff67292

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

Diff for: .buildpacks

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
https://github.com/heroku/heroku-buildpack-nodejs.git
22
https://github.com/heroku/heroku-buildpack-ruby.git
3+
https://github.com/sreid/heroku-buildpack-sourceversion.git

Diff for: app/helpers/pages_helper.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
module PagesHelper
2+
def git_commit_sha
3+
GitCommitSha.current_sha
4+
end
5+
6+
def git_commit_sha_short
7+
full_sha = git_commit_sha
8+
full_sha[-7..-1]
9+
end
210
end

Diff for: app/models/git_commit_sha.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Retrieves the current git commit SHA of the project
2+
class GitCommitSha
3+
def self.current_sha
4+
@sha ||= retrieve_sha_from_file.presence || retrieve_sha_from_git
5+
end
6+
7+
def self.current_sha=(sha)
8+
@sha = sha
9+
end
10+
11+
def self.reset_current_sha
12+
self.current_sha = nil
13+
end
14+
15+
# Assumes the git CLI is available. This is not the case in production on Heroku.
16+
def self.retrieve_sha_from_git
17+
`git rev-parse HEAD 2>/dev/null`.to_s.strip
18+
end
19+
20+
# Assumes a .source_version file with SHA inside. A special Heroku buildpack creates this for us in production.
21+
def self.retrieve_sha_from_file
22+
expected_filepath = Rails.root.join(".source_version")
23+
File.exist?(expected_filepath) ? File.read(expected_filepath).to_s.strip : nil
24+
end
25+
end

Diff for: app/views/pages/index.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<h2>Using React + Redux + Rails Backend (using the react_on_rails gem)</h2>
2+
<p>Current Commit:
3+
<%= link_to git_commit_sha_short,
4+
"https://github.com/shakacode/react-webpack-rails-tutorial/commit/#{git_commit_sha}",
5+
id: "git-commit-sha" %>
6+
</p>
27
<ul>
38
<li>
49
If this work interests you and you're a developer or designer looking for full or part-time remote work: please

Diff for: spec/features/pages_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "rails_helper"
2+
3+
shared_examples "Git Commit SHA" do
4+
background { visit root_path }
5+
it "displays the current git commit" do
6+
el = find("#git-commit-sha")
7+
expect(el.text).to eq expected_text
8+
end
9+
end
10+
11+
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+
17+
context "when .source_version file does not exist" do
18+
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5x" }
19+
let(:expected_text) { "5eead5x" }
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
24+
it_behaves_like "Git Commit SHA"
25+
end
26+
27+
context "when .source_version file exists" do
28+
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5y" }
29+
let(:expected_text) { "5eead5y" }
30+
before { `cd #{Rails.root} && echo #{sha} > .source_version` }
31+
after { `cd #{Rails.root} && rm .source_version` }
32+
it_behaves_like "Git Commit SHA"
33+
end
34+
end

0 commit comments

Comments
 (0)