File tree 5 files changed +73
-0
lines changed
5 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1
1
https://github.com/heroku/heroku-buildpack-nodejs.git
2
2
https://github.com/heroku/heroku-buildpack-ruby.git
3
+ https://github.com/sreid/heroku-buildpack-sourceversion.git
Original file line number Diff line number Diff line change 1
1
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
2
10
end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 1
1
< 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 >
2
7
< ul >
3
8
< li >
4
9
If this work interests you and you're a developer or designer looking for full or part-time remote work: please
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments