Add git repo synchronization

oggit
Vadim vtroshchinskiy 2025-05-07 09:52:06 +02:00
parent 9bbd9de285
commit 2c869ea524
1 changed files with 56 additions and 1 deletions

View File

@ -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/<string:repo>/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/<string:repo>/backup", methods=['POST'])
def git_backup_repository(repo):