From 2c869ea5248c50cf9c3a89c7985beaa3fc93a575 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Wed, 7 May 2025 09:52:06 +0200 Subject: [PATCH] Add git repo synchronization --- api/repo_api.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/api/repo_api.py b/api/repo_api.py index 6e0b5d6..d56dd55 100644 --- a/api/repo_api.py +++ b/api/repo_api.py @@ -2226,6 +2226,34 @@ def git_compact_repository_task(repo, job_id): journal.send(f"Calling function 'recall_ogcore' (JOB_ID: {job_id}, SUCCESS: True)", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") recall_ogcore(data) +def git_sync_repository_task(repo, remote_repository, job_id): + journal.send("Running function 'git_sync_repository_task'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + + git_repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") + git_repo = git.Repo(git_repo_path) + git_repo.git.config('--global', '--add', 'safe.directory', git_repo_path) + + # Recreate the remote every time, it might change + if "backup" in git_repo.remotes: + git_repo.delete_remote("backup") + + backup_repo = git_repo.create_remote("backup", remote_repository) + pushed_references = backup_repo.push("*:*") + results = [] + + # This gets returned to the API + for ref in pushed_references: + results = results + [ {"local_ref" : ref.local_ref.name, "remote_ref" : ref.remote_ref.name, "summary" : ref.summary }] + + data = { + 'job_id': job_id, + 'success': True, + 'updated_references' : results + } + + journal.send(f"Calling function 'recall_ogcore' (JOB_ID: {job_id}, SUCCESS: True)", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + recall_ogcore(data) + @app.route("/ogrepository/v1/git/repositories", methods=['GET']) def git_list_repositories(): @@ -2345,7 +2373,34 @@ def git_delete_repository(): @app.route("/ogrepository/v1/git/repositories//sync", methods=['POST']) def git_sync_repository(repo): - return jsonify({"error" : "Not implemented"}), 500 + journal.send("Running endpoint 'Sincronizar repositorio Git'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + + if not os.path.isdir(REPOSITORIES_BASE_PATH): + journal.send(f"Can't sync repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error": "Repository storage not found, git functionality may not be installed."}), 500 + + data = request.json + + if data is None: + journal.send(f"Can't sync repository, JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + if "remote_repository" in data: + remote_repository = data["remote_repository"] + else: + journal.send(f"Can't sync repository, JSON remote_repository parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + + job_id = f"GitSync_{''.join(random.choice('0123456789abcdef') for char in range(8))}" + + threading.Thread(target=git_sync_repository_task, args=(repo, remote_repository, job_id,)).start() + + return jsonify({ + "success": True, + "output": "Synchronizing...", + "job_id": job_id + }), 200 @app.route("/ogrepository/v1/git/repositories//backup", methods=['POST']) def git_backup_repository(repo):