66 lines
1.3 KiB
Python
66 lines
1.3 KiB
Python
from flask import Flask, jsonify
|
|
import os.path
|
|
import os
|
|
import git
|
|
|
|
|
|
repositories_base_path = "/opt/opengnsys/images"
|
|
|
|
# Create an instance of the Flask class
|
|
app = Flask(__name__)
|
|
|
|
# Define a route for the root URL
|
|
@app.route('/')
|
|
def home():
|
|
return jsonify({
|
|
"message": "OpenGnsys Git API"
|
|
})
|
|
|
|
# Define another route for /hello/<name>
|
|
@app.route('/repositories')
|
|
def get_repositories():
|
|
|
|
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]
|
|
|
|
return jsonify({
|
|
"repositories": repos
|
|
})
|
|
|
|
|
|
@app.route('/repositories/<repo>/branches')
|
|
def get_repository_branches(repo):
|
|
|
|
|
|
gitRepo = git.Repo(os.path.join(repositories_base_path, repo + ".git"))
|
|
|
|
branches = []
|
|
for branch in gitRepo.branches:
|
|
branches = branches + [branch.name]
|
|
|
|
|
|
return jsonify({
|
|
"branches": branches
|
|
})
|
|
|
|
|
|
|
|
# Define a route for health check
|
|
@app.route('/health')
|
|
def health_check():
|
|
return jsonify({
|
|
"status": "OK"
|
|
})
|
|
|
|
# Run the Flask app
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|
|
|
|
|