add logging
This commit is contained in:
parent
7c378ec75a
commit
57d40097c4
16
main.py
16
main.py
@ -1,10 +1,20 @@
|
|||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
WEBBUMP_DIR = "/etc/webbump"
|
WEBBUMP_DIR = "/etc/webbump"
|
||||||
|
LOG_FILE = "/var/log/webbump.log"
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
filename=LOG_FILE,
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||||
|
)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/status', methods=['GET'])
|
@app.route('/status', methods=['GET'])
|
||||||
def status():
|
def status():
|
||||||
@ -15,13 +25,13 @@ def status():
|
|||||||
def gitea_webhook():
|
def gitea_webhook():
|
||||||
data = request.json
|
data = request.json
|
||||||
if not data:
|
if not data:
|
||||||
print("Received webhook without JSON")
|
logger.error("Received webhook without JSON")
|
||||||
return jsonify({"error": "Invalid JSON"}), 400
|
return jsonify({"error": "Invalid JSON"}), 400
|
||||||
|
|
||||||
# Repository name
|
# Repository name
|
||||||
repo_name = data.get("repository", {}).get("name")
|
repo_name = data.get("repository", {}).get("name")
|
||||||
if not repo_name:
|
if not repo_name:
|
||||||
print("Could not get Repository name from JSON")
|
logger.error("Could not get Repository name from JSON")
|
||||||
return jsonify({"error": "Repository name not found"}), 400
|
return jsonify({"error": "Repository name not found"}), 400
|
||||||
|
|
||||||
# Determine event type from header
|
# Determine event type from header
|
||||||
@ -45,7 +55,7 @@ def gitea_webhook():
|
|||||||
try:
|
try:
|
||||||
subprocess.run([script_path, *args], check=True)
|
subprocess.run([script_path, *args], check=True)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(repo_name + " " + script_name + " script failed with exit code: " + e)
|
logger.error(repo_name + " " + script_name + " script failed with exit code: " + e)
|
||||||
return jsonify({"error": f"Script failed: {e}"}), 500
|
return jsonify({"error": f"Script failed: {e}"}), 500
|
||||||
|
|
||||||
return jsonify({"message": f"Executed {script_path}"}), 200
|
return jsonify({"message": f"Executed {script_path}"}), 200
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user