From 8f9ee6e607aa6be1e5d2ee702618db8713414d22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leon=20Michel=20Gori=C3=9Fen?=
 <leon.gorissen@llt.rwth-aachen.de>
Date: Thu, 23 May 2024 10:48:03 +0200
Subject: [PATCH] Feat: Make git commit hash avialable

---
 coscine_watchdog/coscine_client.py | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/coscine_watchdog/coscine_client.py b/coscine_watchdog/coscine_client.py
index ef949fb..8814304 100644
--- a/coscine_watchdog/coscine_client.py
+++ b/coscine_watchdog/coscine_client.py
@@ -11,9 +11,9 @@ import coscine
 import os
 import signal
 import sys
+import subprocess
 
 # TODO create new coscine resource with new application profile once available
-# TODO add git commit to data
 
 app = Flask(__name__)  # Create a new Flask application
 
@@ -39,8 +39,30 @@ logger.addHandler(console_handler)  # Add console handler to logger
 logger.addHandler(file_handler)  # Add file handler to logger
 
 # Retrieve environment variables for API token and robot UUID
-api_token = os.environ.get("COSCINE_API_TOKEN")  # Get COSCINE_API_TOKEN from environment
-robot_uuid = os.environ.get("ROBOT_UUID")  # Get ROBOT_UUID from environment
+def get_env_variable(var_name):
+    value = os.environ.get(var_name)
+    if value is None:
+        raise EnvironmentError(f"The environment variable {var_name} is not set.")
+    return value
+
+try:
+    api_token = get_env_variable("COSCINE_API_TOKEN")  # Get COSCINE_API_TOKEN from environment
+    robot_uuid = get_env_variable("ROBOT_UUID")  # Get ROBOT_UUID from environment
+except EnvironmentError as e:
+    print(e)
+    # Handle the error appropriately, e.g., exit the program or set default values
+    api_token = None
+    robot_uuid = None
+
+try:
+    # Run the git command to get the latest commit hash
+    result = subprocess.run(['git', 'rev-parse', 'HEAD'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) 
+    # The commit hash will be in result.stdout
+    commit_hash = result.stdout.strip()
+except subprocess.CalledProcessError as e:
+    print(f"Error occurred while getting the latest git commit: {e.stderr}")
+
+
 
 def upload_coscine(filepath: Path, token: str) -> None:
     """
-- 
GitLab