81 lines
3.8 KiB
Python
81 lines
3.8 KiB
Python
import os, time, shutil, zipfile, subprocess
|
|
from datetime import datetime
|
|
from core.package_updater import (
|
|
PLUGINS_DIR, DATAPACKS_DIR, BACKUP_DIR,
|
|
load_registry, save_registry, _verify_archive
|
|
)
|
|
|
|
def deploy_from_forgejo_release(payload: dict) -> dict:
|
|
"""
|
|
Declenche lorsqu'un Guardian publie une Release sur Forgejo.
|
|
Verifie que le paquet est bien route sur la voie 'forgejo' par le Ministre du Lapis.
|
|
"""
|
|
action = payload.get("action")
|
|
if action != "published":
|
|
return {"ok": True, "ignored": f"Action release '{action}' ignoree"}
|
|
|
|
repo_full = payload.get("repository", {}).get("full_name", "")
|
|
rel = payload.get("release", {})
|
|
tag = str(rel.get("tag_name", "")).lstrip("v")
|
|
assets = rel.get("assets", [])
|
|
zipball_url = rel.get("zipball_url", "")
|
|
|
|
reg = load_registry()
|
|
pkg = next((p for p in reg.get("packages", []) if p.get("forgejo", {}).get("repo", "").lower() == repo_full.lower()), None)
|
|
if not pkg:
|
|
return {"ok": False, "error": f"Aucun paquet associe au depot {repo_full}"}
|
|
|
|
if pkg.get("track") != "forgejo":
|
|
return {"ok": False, "error": f"Refuse : {pkg['name']} est actuellement route sur la voie Externe (Quartz) par Lapis"}
|
|
|
|
ptype = pkg.get("type", "plugin")
|
|
target_dir = PLUGINS_DIR if ptype == "plugin" else DATAPACKS_DIR
|
|
ext_wanted = ".jar" if ptype == "plugin" else ".zip"
|
|
|
|
chosen_asset = next((a for a in assets if a.get("name", "").endswith(ext_wanted)), None)
|
|
dl_url = chosen_asset.get("browser_download_url") if chosen_asset else (zipball_url if ptype == "datapack" else "")
|
|
new_fn = chosen_asset.get("name") if chosen_asset else f"{pkg['id']}-{tag}.zip"
|
|
|
|
if not dl_url:
|
|
return {"ok": False, "error": f"Aucun fichier {ext_wanted} attache a la release {tag}"}
|
|
|
|
tmp_path = f"/tmp/fc_forge_{int(time.time())}_{new_fn}"
|
|
try:
|
|
res = subprocess.run(["curl", "-4", "-fL", "--connect-timeout", "15", dl_url, "-o", tmp_path])
|
|
if res.returncode != 0:
|
|
raise RuntimeError(f"Echec telechargement release Forgejo (curl {res.returncode})")
|
|
|
|
# Si datapack telecharge depuis zipball GitHub/Forgejo (dossier racine imbrique), re-empaqueter proprement
|
|
if not chosen_asset and ptype == "datapack":
|
|
clean_zip = f"/tmp/fc_dp_clean_{int(time.time())}_{new_fn}"
|
|
with zipfile.ZipFile(tmp_path, "r") as zin:
|
|
mcmeta = next((n for n in zin.namelist() if n.endswith("pack.mcmeta")), None)
|
|
if not mcmeta:
|
|
raise RuntimeError("pack.mcmeta introuvable dans la release du datapack")
|
|
prefix = mcmeta[:-len("pack.mcmeta")]
|
|
with zipfile.ZipFile(clean_zip, "w", zipfile.ZIP_DEFLATED) as zout:
|
|
for item in zin.infolist():
|
|
if item.filename.startswith(prefix) and len(item.filename) > len(prefix):
|
|
zout.writestr(item.filename[len(prefix):], zin.read(item.filename))
|
|
os.remove(tmp_path)
|
|
tmp_path = clean_zip
|
|
|
|
_verify_archive(tmp_path, ptype)
|
|
|
|
old_path = os.path.join(target_dir, pkg.get("filename", ""))
|
|
if os.path.exists(old_path):
|
|
shutil.copy2(old_path, os.path.join(BACKUP_DIR, f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{pkg['filename']}"))
|
|
if pkg["filename"] != new_fn:
|
|
os.remove(old_path)
|
|
|
|
final_p = os.path.join(target_dir, new_fn)
|
|
shutil.move(tmp_path, final_p)
|
|
shutil.chown(final_p, user="pterodactyl", group="pterodactyl")
|
|
|
|
pkg.update({"filename": new_fn, "version": tag})
|
|
save_registry(reg)
|
|
return {"ok": True, "msg": f"Release Forgejo {tag} deployee pour {pkg['name']} ({new_fn})"}
|
|
except Exception as e:
|
|
if os.path.exists(tmp_path):
|
|
os.remove(tmp_path)
|
|
return {"ok": False, "error": str(e)}
|