Compare commits
No commits in common. "oggit" and "main" have entirely different histories.
449
api/repo_api.py
449
api/repo_api.py
|
@ -48,20 +48,6 @@ trash_file = '/opt/opengnsys/ogrepository/etc/trashinfo.json'
|
||||||
config_file = '/opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg'
|
config_file = '/opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------------------------
|
|
||||||
# GIT
|
|
||||||
# --------------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
REPOSITORIES_BASE_PATH = "/opt/opengnsys/ogrepository/oggit/git/oggit/"
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import git
|
|
||||||
import pkgutil
|
|
||||||
import importlib
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------------------
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
# --------------------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------------------
|
||||||
|
@ -2199,441 +2185,6 @@ def rename_image():
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------------------------------------
|
|
||||||
# ____ _ _
|
|
||||||
# / ___(_) |_
|
|
||||||
# | | _| | __|
|
|
||||||
# | |_| | | |_
|
|
||||||
# \____|_|\__|
|
|
||||||
#
|
|
||||||
# -----------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
def git_compact_repository_task(repo, job_id):
|
|
||||||
journal.send("Running function 'git_compact_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)
|
|
||||||
|
|
||||||
git_repo.git.gc()
|
|
||||||
|
|
||||||
data = {
|
|
||||||
'job_id': job_id,
|
|
||||||
'success': True
|
|
||||||
}
|
|
||||||
|
|
||||||
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():
|
|
||||||
"""
|
|
||||||
Retrieve a list of Git repositories.
|
|
||||||
|
|
||||||
This endpoint scans the OpenGnsys image path for directories that
|
|
||||||
appear to be Git repositories (i.e., they contain a "HEAD" file).
|
|
||||||
It returns a JSON response containing the names of these repositories.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response with a list of repository names or an
|
|
||||||
error message if the repository storage is not found.
|
|
||||||
- 200 OK: When the repositories are successfully retrieved.
|
|
||||||
- 500 Internal Server Error: When the repository storage is not found.
|
|
||||||
|
|
||||||
Example JSON response:
|
|
||||||
{
|
|
||||||
"repositories": ["repo1", "repo2"]
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
journal.send("Running endpoint 'Obtener Información de todos los repositorios Git'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
|
|
||||||
if not os.path.isdir(REPOSITORIES_BASE_PATH):
|
|
||||||
journal.send(f"Can't list 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
|
|
||||||
|
|
||||||
repos = []
|
|
||||||
for entry in os.scandir(REPOSITORIES_BASE_PATH):
|
|
||||||
if entry.is_dir(follow_symlinks=False) and os.path.isfile(os.path.join(entry.path, "HEAD")):
|
|
||||||
name = entry.name
|
|
||||||
if name.endswith(".git"):
|
|
||||||
name = name[:-4]
|
|
||||||
|
|
||||||
repos = repos + [name]
|
|
||||||
|
|
||||||
|
|
||||||
journal.send(f"Returning {len(repos)} repositories", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
|
|
||||||
return jsonify({
|
|
||||||
"repositories": repos
|
|
||||||
}), 200
|
|
||||||
|
|
||||||
def _load_module(module_name):
|
|
||||||
# module = importlib.util.find_spec(module_name)
|
|
||||||
module = importlib.import_module(module_name)
|
|
||||||
|
|
||||||
if module is not None:
|
|
||||||
journal.send(f"Module {module_name} loaded successfully. Got {module}", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
|
||||||
return module
|
|
||||||
|
|
||||||
journal.send(f"Module {module_name} failed to load, not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _load_installer():
|
|
||||||
|
|
||||||
journal.send(f"Loading oggit installer module", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
|
||||||
|
|
||||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
||||||
|
|
||||||
system_lib_path = '/opt/opengnsys/oggit/bin/'
|
|
||||||
devel_lib_path = os.path.join(script_dir, "../../oggit/installer")
|
|
||||||
|
|
||||||
if devel_lib_path not in sys.path and os.path.isdir(devel_lib_path):
|
|
||||||
journal.send(f"Using {devel_lib_path} development library path", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
|
||||||
sys.path.append(devel_lib_path)
|
|
||||||
|
|
||||||
if system_lib_path not in sys.path:
|
|
||||||
journal.send(f"Using {system_lib_path} system library path", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
|
||||||
sys.path.append(system_lib_path)
|
|
||||||
|
|
||||||
return _load_module("opengnsys_git_installer")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories", methods=['POST'])
|
|
||||||
def git_create_repository():
|
|
||||||
"""
|
|
||||||
Create a new Git repository.
|
|
||||||
|
|
||||||
This endpoint creates a new Git repository with the specified name.
|
|
||||||
If the repository already exists, it returns a status message indicating so.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (str): The name of the repository to be created.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response with a status message and HTTP status code.
|
|
||||||
- 200: If the repository already exists.
|
|
||||||
- 201: If the repository is successfully created.
|
|
||||||
"""
|
|
||||||
data = request.json
|
|
||||||
|
|
||||||
if data is None:
|
|
||||||
journal.send(f"Can't create repository, JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error" : "Parameters missing"}), 400
|
|
||||||
|
|
||||||
repo = data["name"]
|
|
||||||
|
|
||||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
|
||||||
if os.path.isdir(repo_path):
|
|
||||||
journal.send(f"Can't create repository {repo}, already exists at {repo_path}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"status": "Repository already exists"}), 200
|
|
||||||
|
|
||||||
module = _load_installer()
|
|
||||||
print(f"Got {module}")
|
|
||||||
OpengnsysGitInstaller = getattr(module, 'OpengnsysGitInstaller')
|
|
||||||
installer = OpengnsysGitInstaller()
|
|
||||||
installer.add_forgejo_repo(repo)
|
|
||||||
|
|
||||||
journal.send(f"Repository {repo} created", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
|
|
||||||
return jsonify({"status": "Repository created"}), 201
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories", methods=['DELETE'])
|
|
||||||
def git_delete_repository():
|
|
||||||
return jsonify({"error" : "Not implemented"}), 500
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/sync", methods=['POST'])
|
|
||||||
def git_sync_repository(repo):
|
|
||||||
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):
|
|
||||||
return jsonify({"error" : "Not implemented"}), 500
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/compact", methods=['POST'])
|
|
||||||
def git_compact_repository(repo):
|
|
||||||
journal.send("Running endpoint 'Compactar repositorio Git'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
|
|
||||||
if not os.path.isdir(REPOSITORIES_BASE_PATH):
|
|
||||||
journal.send(f"Can't list 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
|
|
||||||
|
|
||||||
|
|
||||||
job_id = f"GitGC_{''.join(random.choice('0123456789abcdef') for char in range(8))}"
|
|
||||||
threading.Thread(target=git_compact_repository_task, args=(repo, job_id,)).start()
|
|
||||||
|
|
||||||
return jsonify({
|
|
||||||
"success": True,
|
|
||||||
"output": "Compacting...",
|
|
||||||
"job_id": job_id
|
|
||||||
}), 200
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/branches", methods=['GET'])
|
|
||||||
def git_get_branches(repo):
|
|
||||||
"""
|
|
||||||
Retrieve the list of branches for a given repository.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (str): The name of the repository.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response containing a list of branch names or an error message if the repository is not found.
|
|
||||||
- 200: A JSON object with a "branches" key containing a list of branch names.
|
|
||||||
- 404: A JSON object with an "error" key containing the message "Repository not found" if the repository does not exist.
|
|
||||||
"""
|
|
||||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
|
||||||
if not os.path.isdir(repo_path):
|
|
||||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Repository not found"}), 404
|
|
||||||
|
|
||||||
git_repo = git.Repo(repo_path)
|
|
||||||
git_repo.git.config('--global', '--add', 'safe.directory', repo_path)
|
|
||||||
|
|
||||||
|
|
||||||
branches = []
|
|
||||||
for branch in git_repo.branches:
|
|
||||||
branches = branches + [branch.name]
|
|
||||||
|
|
||||||
journal.send(f"Returning {len(branches)} branches", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return {
|
|
||||||
"branches": branches
|
|
||||||
}
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/branches/<string:branch>", methods=['POST'])
|
|
||||||
def git_create_branch(repo, branch):
|
|
||||||
"""Create a given branch in a given repository
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (str): The name of the repository.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response containing a creation status
|
|
||||||
- 201: A JSON object with a "status" key containing "created"
|
|
||||||
- 404: A JSON object with an "error" key containing the message "Repository not found"
|
|
||||||
- 409: A JSON object with an "error" key containing the message "Tag already exists"
|
|
||||||
"""
|
|
||||||
|
|
||||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
|
||||||
if not os.path.isdir(repo_path):
|
|
||||||
journal.send(f"Can't create branch. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Repository not found"}), 404
|
|
||||||
|
|
||||||
git_repo = git.Repo(repo_path)
|
|
||||||
git_repo.git.config('--global', '--add', 'safe.directory', repo_path)
|
|
||||||
|
|
||||||
data = request.json
|
|
||||||
if data is None:
|
|
||||||
journal.send(f"Can't create branch. JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error" : "Parameters missing"}), 400
|
|
||||||
|
|
||||||
if not "commit" in data:
|
|
||||||
journal.send(f"Can't create branch. Commit parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error" : "commit parameter missing"}), 400
|
|
||||||
|
|
||||||
if branch in git_repo.branches:
|
|
||||||
journal.send(f"Can't create branch. Already found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Branch already exists"}), 409
|
|
||||||
|
|
||||||
git_repo.create_tag(branch, ref = data["commit"])
|
|
||||||
|
|
||||||
journal.send(f"Branch {branch} created in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"status": "created"}), 201
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/branches/<string:branch>", methods=['DELETE'])
|
|
||||||
def git_delete_branch(repo, branch):
|
|
||||||
"""Delete a given branch in a given repository
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (str): The name of the repository.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response containing a list of branch names or an error message if the repository is not found.
|
|
||||||
- 200: A JSON object with a "status" key containing "deleted"
|
|
||||||
- 404: A JSON object with an "error" key containing the message "Repository not found" or "Branch not found"
|
|
||||||
"""
|
|
||||||
|
|
||||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
|
||||||
if not os.path.isdir(repo_path):
|
|
||||||
journal.send(f"Can't delete branch. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return {"error": "Repository not found"}, 404
|
|
||||||
|
|
||||||
git_repo = git.Repo(repo_path)
|
|
||||||
git_repo.git.config('--global', '--add', 'safe.directory', repo_path)
|
|
||||||
|
|
||||||
|
|
||||||
if not branch in git_repo.branches:
|
|
||||||
journal.send(f"Can't delete branch. Not found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Branch not found"}), 404
|
|
||||||
|
|
||||||
git_repo.delete_head(branch)
|
|
||||||
journal.send(f"Branch {branch} deleted in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
|
|
||||||
return jsonify({"status": "deleted"}), 200
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/tags", methods=['GET'])
|
|
||||||
def git_list_tags(repo):
|
|
||||||
"""
|
|
||||||
Retrieve the list of tags for a given repository.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (str): The name of the repository.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response containing a list of tag names or an error message if the repository is not found.
|
|
||||||
- 200: A JSON object with a "branches" key containing a list of tag names.
|
|
||||||
- 404: A JSON object with an "error" key containing the message "Repository not found" if the repository does not exist.
|
|
||||||
"""
|
|
||||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
|
||||||
if not os.path.isdir(repo_path):
|
|
||||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Repository not found"}), 404
|
|
||||||
|
|
||||||
git_repo = git.Repo(repo_path)
|
|
||||||
git_repo.git.config('--global', '--add', 'safe.directory', repo_path)
|
|
||||||
|
|
||||||
|
|
||||||
tags = []
|
|
||||||
for tag in git_repo.tags:
|
|
||||||
tags = tags + [tag.name]
|
|
||||||
|
|
||||||
journal.send(f"Returning {len(tags)} branches", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return {
|
|
||||||
"tags": tags
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/tags/<string:tag>", methods=['POST'])
|
|
||||||
def git_create_tag(repo, tag):
|
|
||||||
"""Create a given tag in a given repository
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (str): The name of the repository.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response containing a creation status
|
|
||||||
- 200: A JSON object with a "status" key containing "created"
|
|
||||||
- 404: A JSON object with an "error" key containing the message "Repository not found"
|
|
||||||
- 409: A JSON object with an "error" key containing the message "Tag already exists"
|
|
||||||
"""
|
|
||||||
|
|
||||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
|
||||||
if not os.path.isdir(repo_path):
|
|
||||||
journal.send(f"Can't create tag. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Repository not found"}), 404
|
|
||||||
|
|
||||||
git_repo = git.Repo(repo_path)
|
|
||||||
git_repo.git.config('--global', '--add', 'safe.directory', repo_path)
|
|
||||||
|
|
||||||
data = request.json
|
|
||||||
if data is None:
|
|
||||||
journal.send(f"Can't create tag. JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error" : "Parameters missing"}), 400
|
|
||||||
|
|
||||||
if not "commit" in data:
|
|
||||||
journal.send(f"Can't create tag. Commit parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error" : "commit parameter missing"}), 400
|
|
||||||
|
|
||||||
if tag in git_repo.tags:
|
|
||||||
journal.send(f"Can't create tag. Already found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Tag already exists"}), 409
|
|
||||||
|
|
||||||
git_repo.create_tag(tag, ref = data["commit"])
|
|
||||||
|
|
||||||
journal.send(f"Tag {tag} created in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"status": "created"}), 200
|
|
||||||
|
|
||||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/tags/<string:tag>", methods=['DELETE'])
|
|
||||||
def git_delete_tag(repo, tag):
|
|
||||||
"""Delete a given tag in a given repository
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (str): The name of the repository.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response: A JSON response containing a list of tag names or an error message if the repository is not found.
|
|
||||||
- 200: A JSON object with a "status" key containing "deleted"
|
|
||||||
- 404: A JSON object with an "error" key containing the message "Repository not found" or "Tag not found"
|
|
||||||
"""
|
|
||||||
|
|
||||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
|
||||||
if not os.path.isdir(repo_path):
|
|
||||||
journal.send(f"Can't delete tag. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return {"error": "Repository not found"}, 404
|
|
||||||
|
|
||||||
git_repo = git.Repo(repo_path)
|
|
||||||
git_repo.git.config('--global', '--add', 'safe.directory', repo_path)
|
|
||||||
|
|
||||||
|
|
||||||
if not tag in git_repo.tags:
|
|
||||||
journal.send(f"Can't delete tag. Not found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
return jsonify({"error": "Tag not found"}), 404
|
|
||||||
|
|
||||||
git_repo.delete_head(tag)
|
|
||||||
journal.send(f"Tag {tag} deleted in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
|
||||||
|
|
||||||
return jsonify({"status": "deleted"}), 200
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
356
api/swagger.yaml
356
api/swagger.yaml
|
@ -18,7 +18,7 @@ tags:
|
||||||
- name: "Transferencia entre Repositorios y Backup"
|
- name: "Transferencia entre Repositorios y Backup"
|
||||||
- name: "Importar y Exportar Máquinas Virtuales"
|
- name: "Importar y Exportar Máquinas Virtuales"
|
||||||
- name: "Varios"
|
- name: "Varios"
|
||||||
- name: "Git"
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------------------------------------
|
||||||
# Apartado "Estado de ogRepository"
|
# Apartado "Estado de ogRepository"
|
||||||
|
@ -1960,357 +1960,3 @@ paths:
|
||||||
example: "(Exception description)"
|
example: "(Exception description)"
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------------------------------------
|
|
||||||
# ____ _ _
|
|
||||||
# / ___(_) |_
|
|
||||||
# | | _| | __|
|
|
||||||
# | |_| | | |_
|
|
||||||
# \____|_|\__|
|
|
||||||
#
|
|
||||||
# -----------------------------------------------------------
|
|
||||||
|
|
||||||
/ogrepository/v1/git/repositories:
|
|
||||||
get:
|
|
||||||
summary: "Obtener lista de repositorios"
|
|
||||||
description: |
|
|
||||||
Devuelve una lista de repositorios de Git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: "Lista de repositorios"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
repositories:
|
|
||||||
type: array
|
|
||||||
items:
|
|
||||||
type: string
|
|
||||||
example: linux
|
|
||||||
|
|
||||||
"500":
|
|
||||||
description: "Excepción"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
type: boolean
|
|
||||||
example: false
|
|
||||||
exception:
|
|
||||||
type: string
|
|
||||||
example: "(Exception description)"
|
|
||||||
post:
|
|
||||||
summary: "Crear repositorio"
|
|
||||||
description: |
|
|
||||||
Crea un repositorio nuevo de Git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
parameters:
|
|
||||||
- name: JSON
|
|
||||||
in: body
|
|
||||||
required: true
|
|
||||||
description: |
|
|
||||||
* **name** - Nombre de repositorio
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
example: linux
|
|
||||||
responses:
|
|
||||||
"201":
|
|
||||||
description: "Repositorio creado"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
status:
|
|
||||||
type: string
|
|
||||||
example: "Repository created"
|
|
||||||
"500 (Exception)":
|
|
||||||
description: "JSON post data missing"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
error:
|
|
||||||
type: string
|
|
||||||
example: "Parameters missing"
|
|
||||||
/ogrepository/v1/git/repositories/{repository}/tags:
|
|
||||||
get:
|
|
||||||
summary: "Obtener lista de tags"
|
|
||||||
description: |
|
|
||||||
Devuelve una lista de tags de Git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
parameters:
|
|
||||||
- name: repository
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Nombre de repositorio"
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: "Lista de tags"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
tags:
|
|
||||||
type: array
|
|
||||||
items:
|
|
||||||
type: string
|
|
||||||
example: v0.1
|
|
||||||
|
|
||||||
"500":
|
|
||||||
description: "Excepción"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
type: boolean
|
|
||||||
example: false
|
|
||||||
exception:
|
|
||||||
type: string
|
|
||||||
example: "(Exception description)"
|
|
||||||
/ogrepository/v1/git/repositories/{repository}/tags/{tag}:
|
|
||||||
post:
|
|
||||||
summary: "Crear tag"
|
|
||||||
description: |
|
|
||||||
Crea una tag de git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
parameters:
|
|
||||||
- name: repository
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Nombre de repositorio"
|
|
||||||
- name: tag
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Tag del repositorio"
|
|
||||||
- name: JSON
|
|
||||||
in: body
|
|
||||||
required: true
|
|
||||||
description: |
|
|
||||||
* **commit** - Commit al que apunta el tag nuevo. Puede ser un nombre de otra rama/tag.
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
example: HEAD
|
|
||||||
responses:
|
|
||||||
"201":
|
|
||||||
description: "Tag creado"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
status:
|
|
||||||
type: string
|
|
||||||
example: created
|
|
||||||
|
|
||||||
"500":
|
|
||||||
description: "Excepción"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
type: boolean
|
|
||||||
example: false
|
|
||||||
exception:
|
|
||||||
type: string
|
|
||||||
example: "(Exception description)"
|
|
||||||
"404":
|
|
||||||
description: "El repositorio no existe"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
error:
|
|
||||||
type: string
|
|
||||||
example: "Repository not found"
|
|
||||||
"409":
|
|
||||||
description: "El tag ya existe"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
error:
|
|
||||||
type: string
|
|
||||||
example: "Tag already exists"
|
|
||||||
delete:
|
|
||||||
summary: "Eliminar tag"
|
|
||||||
description: |
|
|
||||||
Elimina un tag de git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
parameters:
|
|
||||||
- name: repository
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Nombre de repositorio"
|
|
||||||
- name: tag
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Rama del repositorio"
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: "Tag eliminado"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
status:
|
|
||||||
type: string
|
|
||||||
example: deleted
|
|
||||||
"500":
|
|
||||||
description: "Excepción"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
type: boolean
|
|
||||||
example: false
|
|
||||||
exception:
|
|
||||||
type: string
|
|
||||||
example: "(Exception description)"
|
|
||||||
/ogrepository/v1/git/repositories/{repository}/branches:
|
|
||||||
get:
|
|
||||||
summary: "Obtener lista de branches"
|
|
||||||
description: |
|
|
||||||
Devuelve una lista de branches de Git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
parameters:
|
|
||||||
- name: repository
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Nombre de repositorio"
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: "Lista de branches"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
branches:
|
|
||||||
type: array
|
|
||||||
items:
|
|
||||||
type: string
|
|
||||||
example: main
|
|
||||||
|
|
||||||
"500":
|
|
||||||
description: "Excepción"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
type: boolean
|
|
||||||
example: false
|
|
||||||
exception:
|
|
||||||
type: string
|
|
||||||
example: "(Exception description)"
|
|
||||||
"404":
|
|
||||||
description: "El repositorio no existe"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
error:
|
|
||||||
type: string
|
|
||||||
example: "Repository not found"
|
|
||||||
"409":
|
|
||||||
description: "El branch ya existe"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
error:
|
|
||||||
type: string
|
|
||||||
example: "Branch already exists"
|
|
||||||
|
|
||||||
/ogrepository/v1/git/repositories/{repository}/branches/{branch}:
|
|
||||||
post:
|
|
||||||
summary: "Crear branch"
|
|
||||||
description: |
|
|
||||||
Crea una rama de git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
parameters:
|
|
||||||
- name: repository
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Nombre de repositorio"
|
|
||||||
- name: branch
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Branch del repositorio"
|
|
||||||
- name: JSON
|
|
||||||
in: body
|
|
||||||
required: true
|
|
||||||
description: |
|
|
||||||
* **commit** - Commit al que apunta la rama nueva. Puede ser un nombre de otra rama/tag.
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
example: HEAD
|
|
||||||
responses:
|
|
||||||
"201":
|
|
||||||
description: "Rama creada"
|
|
||||||
schema:
|
|
||||||
type: array
|
|
||||||
items:
|
|
||||||
type: string
|
|
||||||
example: main
|
|
||||||
|
|
||||||
"500":
|
|
||||||
description: "Git no instalado"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
type: boolean
|
|
||||||
example: false
|
|
||||||
exception:
|
|
||||||
type: string
|
|
||||||
example: "(Exception description)"
|
|
||||||
delete:
|
|
||||||
summary: "Eliminar branch"
|
|
||||||
description: |
|
|
||||||
Elimina una rama de git
|
|
||||||
tags:
|
|
||||||
- "Git"
|
|
||||||
parameters:
|
|
||||||
- name: repository
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Nombre de repositorio"
|
|
||||||
- name: branch
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
description: "Branch del repositorio"
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: "Branch eliminado"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
status:
|
|
||||||
type: string
|
|
||||||
example: deleted
|
|
||||||
"500":
|
|
||||||
description: "Excepción"
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
type: boolean
|
|
||||||
example: false
|
|
||||||
exception:
|
|
||||||
type: string
|
|
||||||
example: "(Exception description)"
|
|
Loading…
Reference in New Issue