Skip to content

Commit

Permalink
Add route for displaying current Git SHA
Browse files Browse the repository at this point in the history
For:
#9
  • Loading branch information
liammulh committed Mar 26, 2024
1 parent 780470f commit 22fd785
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .ebextensions/git.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Git is needed for the route that displays the current Git SHA.
packages:
yum:
git: []
17 changes: 16 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Set up API routes.
"""

# Built-in libraries:
import subprocess

# Third-party dependencies:
from flask import Flask

Expand All @@ -12,4 +15,16 @@
@app.route("/")
def hello_world():
"""A cheeky hello world route."""
return {"message": "Look on my Affiliations Service, ye Mighty, and despair!"}
return "<p>Look on my Affiliations Service, ye Mighty, and despair!</p>"


@app.route("/sha")
def current_git_sha():
"""Displays current Git SHA."""
command = ["git", "rev-parse", "HEAD"]
output = subprocess.run(command, check=False, capture_output=True).stdout.decode(
"utf-8"
)
# Strip newline character from the end of the string.
sha = output[0 : len(output) - 1]
return sha
14 changes: 11 additions & 3 deletions src/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def test_root():
"""Ensure our cheeky hello world route works."""
response = CLIENT.get("/")
assert response.status_code == 200
assert response.json == {
"message": "Look on my Affiliations Service, ye Mighty, and despair!"
}
assert (
response.text
== "<p>Look on my Affiliations Service, ye Mighty, and despair!</p>"
)


def test_sha():
"""Ensure we can get a SHA."""
response = CLIENT.get("/sha")
assert response.status_code == 200
assert len(response.text) == 40 # SHA-1 is 40 characters in length.

0 comments on commit 22fd785

Please sign in to comment.