From d0c27618159fefb1cffad6f996b2780fdfd81c71 Mon Sep 17 00:00:00 2001
From: Alejandro Villar <avillar@ogc.org>
Date: Mon, 29 Jan 2024 17:52:30 +0100
Subject: [PATCH] Add modified and git metadata to register.json

---
 Dockerfile                |  3 ++-
 ogc/bblocks/entrypoint.py | 44 ++++++++++++++++++++++++++-------------
 2 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 17a580f..d41860c 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -11,7 +11,8 @@ RUN apk update && \
     /venv/bin/python -m pip install --upgrade pip && \
     git config --global --add safe.directory '*' && \
     npm install jsonld && \
-    echo "$BBP_GIT_INFO" > /GIT_INFO
+    echo "$BBP_GIT_INFO" > /GIT_INFO && \
+    git config --system --add safe.directory '*'
 
 RUN /venv/bin/python -m pip install -r /requirements.txt
 
diff --git a/ogc/bblocks/entrypoint.py b/ogc/bblocks/entrypoint.py
index 24371f7..e8549ef 100644
--- a/ogc/bblocks/entrypoint.py
+++ b/ogc/bblocks/entrypoint.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+import datetime
 import os
 import shutil
 import subprocess
@@ -154,6 +155,12 @@
                 print(f"Deleting {old_dir} recursively", file=sys.stderr)
                 shutil.rmtree(old_dir, ignore_errors=True)
 
+    # Fix git config
+    try:
+        subprocess.run(['git', 'config', '--global', '--add', 'safe.directory', '*'])
+    except Exception as e:
+        print(f"Error configuring git safe.directory: {e}", file=sys.stderr)
+
     # Read local bblocks-config.yaml, if present
     id_prefix = 'ogc.'
     annotated_path = Path(args.annotated_path)
@@ -181,6 +188,8 @@
         if sparql_conf and sparql_conf.get('query'):
             register_additional_metadata['sparqlEndpoint'] = sparql_conf['query']
 
+    register_additional_metadata['modified'] = datetime.datetime.now().isoformat()
+
     if os.environ.get('BBP_GIT_INFO_FILE'):
         with open(os.environ['BBP_GIT_INFO_FILE']) as f:
             git_info = f.readline().strip()
@@ -196,22 +205,29 @@
     base_url = args.base_url
     github_base_url = args.github_base_url
     git_repo_path = None
-    if not base_url or not github_base_url:
-        try:
-            import git
-            repo = git.Repo()
-            git_repo_path = Path(repo.working_dir)
-            remote_branch = repo.active_branch.tracking_branch()
-            remote = repo.remote(remote_branch.remote_name)
-            remote_url = next(remote.urls)
-            gh_repo = get_github_repo(remote_url)
-            if gh_repo:
+    try:
+        import git
+        repo = git.Repo()
+        git_repo_path = Path(repo.working_dir)
+        remote_branch = repo.active_branch.tracking_branch()
+        remote = repo.remote(remote_branch.remote_name)
+        remote_url = next(remote.urls)
+        if remote_url:
+            register_additional_metadata['gitRepository'] = remote_url
+
+        gh_repo = get_github_repo(remote_url)
+        if gh_repo:
+            if not base_url:
                 base_url = f"https://{gh_repo[0]}.github.io/{gh_repo[1]}/"
+            if not github_base_url:
                 github_base_url = f"https://github.com/{gh_repo[0]}/{gh_repo[1]}/"
-                print(f"Autodetected GitHub repo {gh_repo[0]}/{gh_repo[1]}")
-        except:
-            print('[WARN] Could not autodetect base_url / github_base_url', file=sys.stderr)
-            pass
+            print(f"Autodetected GitHub repo {gh_repo[0]}/{gh_repo[1]}")
+
+        if github_base_url:
+            register_additional_metadata['gitHubRepository'] = github_base_url
+    except Exception as e:
+        print(f"[WARN] Could not autodetect base_url / github_base_url ({e})", file=sys.stderr)
+        pass
 
     steps = args.steps.split(',') if args.steps else None