peppa/scripts/server-move/previews-open.py

25 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# PEP-231, на сервере: какие превью с Mac ещё нужны — PR открыт в новой Gitea (остальные на Mac просто
# останавливаются). Вход: ~/peppa/products-move/previews.txt (проект|slug|PR|sha|работает), его кладёт
# products-move.sh previews; выход: previews-open.txt (проект|sha).
import json, sys, urllib.request
P = "/home/vibe42/peppa"; M = f"{P}/products-move"; T = open(f"{P}/gitea-vibe42.token").read().strip()
keep, closed = [], []
for line in open(f"{M}/previews.txt"):
proj, slug, num, sha, running = line.strip().split("|")
try:
req = urllib.request.Request(f"https://git.vibe42.kz/api/v1/repos/peppa-bot/{slug}/pulls/{num}", headers={"Authorization": f"token {T}"})
pr = json.load(urllib.request.urlopen(req, timeout=20))
state, head = pr["state"], pr["head"]["sha"]
except Exception as e:
state, head = f"нет ({e})", ""
note = "" if head == sha or not head else f" голова PR {head[:8]} ≠ превью {sha[:8]}"
print(f"{proj}: {state}{note}{'' if running != '0' else ' (на Mac остановлен)'}")
if state == "open" and running != "0" and sha:
keep.append(f"{proj}|{sha}")
elif state == "closed" or "404" in state:
closed.append(proj) # PR закрыт или репозитория нет (проект удалён) — превью больше не нужно
open(f"{M}/previews-open.txt", "w").write("\n".join(keep) + "\n")
open(f"{M}/previews-closed.txt", "w").write("\n".join(closed) + "\n")
print(f"открытых PR с работающим превью: {len(keep)} из {sum(1 for _ in open(f'{M}/previews.txt'))}")