From 73b9fc5ef83b84c168b3edc26a4c0fc937e80c92 Mon Sep 17 00:00:00 2001
From: "r.jaepel" <r.jaepel@fz-juelich.de>
Date: Mon, 27 Nov 2023 11:10:47 +0100
Subject: [PATCH] Add additional checks and updates for the output_remotes.json

---
 cadetrdm/initialize_repo.py |  2 ++
 cadetrdm/repositories.py    | 15 ++++++++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/cadetrdm/initialize_repo.py b/cadetrdm/initialize_repo.py
index 5001730..c3eb722 100644
--- a/cadetrdm/initialize_repo.py
+++ b/cadetrdm/initialize_repo.py
@@ -182,6 +182,8 @@ def clone(project_url, path_to_repo: str = None):
     output_folder_name = os.path.join(path_to_repo, meta_dict["output_folder_name"])
     ssh_remotes = list(meta_dict["output_remotes"].values())
     http_remotes = [ssh_url_to_http_url(url) for url in ssh_remotes]
+    if len(ssh_remotes + http_remotes) == 0:
+        raise RuntimeError("No output remotes configured in output_remotes.json")
     for output_remote in ssh_remotes + http_remotes:
         try:
             print(f"Attempting to clone {output_remote} into {output_folder_name}")
diff --git a/cadetrdm/repositories.py b/cadetrdm/repositories.py
index de34d09..ab4c0e9 100644
--- a/cadetrdm/repositories.py
+++ b/cadetrdm/repositories.py
@@ -115,6 +115,13 @@ class BaseRepo:
         :return:
         """
         self._git_repo.create_remote(remote_name, url=remote_url)
+        with open(self.data_json_path, "r") as handle:
+            rdm_data = json.load(handle)
+        if rdm_data["is_project_repo"]:
+            pass
+        if rdm_data["is_output_repo"]:
+            project_repo = ProjectRepo(os.path.split(self.working_dir)[0])
+            project_repo.update_output_remotes_json()
 
     def import_remote_repo(self, source_repo_location, source_repo_branch, target_repo_location=None):
         """
@@ -726,17 +733,19 @@ class ProjectRepo(BaseRepo):
             Option to add all changed and new files to git automatically.
         """
 
+        self.update_output_remotes_json()
+
+        super().commit(message=message, add_all=add_all)
+
+    def update_output_remotes_json(self):
         output_repo_remotes = self.output_repo.remote_urls
         self.add_list_of_remotes_in_readme_file("output_repo", output_repo_remotes)
-
         output_json_filepath = os.path.join(self.working_dir, "output_remotes.json")
         with open(output_json_filepath, "w") as file_handle:
             remotes_dict = {remote.name: str(remote.url) for remote in self.output_repo.remotes}
             json_dict = {"output_folder_name": self.output_folder, "output_remotes": remotes_dict}
             json.dump(json_dict, file_handle, indent=2)
 
-        super().commit(message=message, add_all=add_all)
-
     def download_file(self, url, file_path):
         """
         Download the file from the url and put it in the output+file_path location.
-- 
GitLab