mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
Separate PR build announcements (#418)
* Separate PR build announcements and retire closed builds * Isolate watcher test clocks and clean up the agent server
This commit is contained in:
1 parent
1304809240
commit
9d8ff9615f
6 files changed
+369
-36
No files matched your search
@@ -0,0 +1,170 @@
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
REPORT = "<!-- droiddeck-pr-release -->"
|
||||
STATE = re.compile(r"\n<!-- droiddeck-discord: (\{[^\n]*\}) -->")
|
||||
|
||||
|
||||
def github(path, method="GET", body=None):
|
||||
args = ["gh", "api", path, "--method", method]
|
||||
if body is not None:
|
||||
args += ["--input", "-"]
|
||||
result = subprocess.run(args, input=json.dumps(body) if body is not None else None,
|
||||
capture_output=True, text=True)
|
||||
if result.returncode:
|
||||
raise RuntimeError("GitHub request failed")
|
||||
return json.loads(result.stdout) if result.stdout.strip() else None
|
||||
|
||||
|
||||
def report_comment(repo, number):
|
||||
page = 1
|
||||
while True:
|
||||
comments = github(f"repos/{repo}/issues/{number}/comments?per_page=100&page={page}")
|
||||
for comment in comments:
|
||||
if (comment.get("user", {}).get("login") == "github-actions[bot]"
|
||||
and (comment.get("body") or "").startswith(REPORT)):
|
||||
return comment
|
||||
if len(comments) < 100:
|
||||
return None
|
||||
page += 1
|
||||
|
||||
|
||||
def saved_message(body, webhook_id):
|
||||
match = STATE.search(body)
|
||||
if not match:
|
||||
return None
|
||||
try:
|
||||
state = json.loads(match.group(1))
|
||||
except ValueError:
|
||||
return None
|
||||
if (state.get("webhook_id") == webhook_id
|
||||
and re.fullmatch(r"[0-9]{15,22}", str(state.get("message_id", "")))):
|
||||
return state["message_id"]
|
||||
return None
|
||||
|
||||
|
||||
class Discord:
|
||||
def __init__(self, webhook):
|
||||
match = re.fullmatch(r"https://discord\.com/api(?:/v[0-9]+)?/webhooks/([0-9]{15,22})/([A-Za-z0-9_-]+)", webhook)
|
||||
if not match:
|
||||
raise RuntimeError("Invalid Discord webhook configuration")
|
||||
self.url = webhook
|
||||
self.id = match.group(1)
|
||||
|
||||
def request(self, method, suffix="", body=None):
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
request = urllib.request.Request(self.url + suffix, data=data,
|
||||
headers={"Content-Type": "application/json",
|
||||
"User-Agent": "DroidDeck-PR-builds"}, method=method)
|
||||
for attempt in range(3):
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=30) as response:
|
||||
return json.load(response)
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code == 404:
|
||||
return None
|
||||
if method != "POST" and error.code in (429, 500, 502, 503, 504) and attempt < 2:
|
||||
time.sleep(2 ** attempt)
|
||||
continue
|
||||
raise RuntimeError(f"Discord request failed (HTTP {error.code})") from None
|
||||
except (OSError, ValueError):
|
||||
raise RuntimeError("Discord request failed") from None
|
||||
|
||||
|
||||
def plain_title(title):
|
||||
title = " ".join(title.split())[:300]
|
||||
return re.sub(r"([\\`*_~|\[\]<>])", r"\\\1", title)
|
||||
|
||||
|
||||
def build_content(repo, number, title, sha, apk, release):
|
||||
return (f"**Signed PR test build** `pr-{number}` · `{sha[:7]}`\n"
|
||||
f"[#{number}](https://github.com/{repo}/pull/{number}): {plain_title(title)}\n"
|
||||
"_Not merged — for testing._\n"
|
||||
f"Download: [standard APK](<{apk}>) · [all variants](<{release}>)")
|
||||
|
||||
|
||||
def retired_content(content, merged):
|
||||
label = "Merged" if merged else "Closed"
|
||||
if content.startswith(("**Merged** —", "**Closed** —")):
|
||||
content = "\n".join(line[2:-2] if line.startswith("~~") and line.endswith("~~") else line
|
||||
for line in content.splitlines()[2:])
|
||||
struck = "\n".join(f"~~{line}~~" if line else "" for line in content.splitlines())
|
||||
return f"**{label}** — this PR test build is no longer available.\n\n{struck}"
|
||||
|
||||
|
||||
def sync(mode, repo, number, discord, sha="", apk="", release=""):
|
||||
pull = github(f"repos/{repo}/pulls/{number}")
|
||||
if mode == "publish":
|
||||
if (pull["state"] != "open" or pull.get("draft") or pull["base"]["ref"] != "main"
|
||||
or pull["head"]["sha"] != sha):
|
||||
print("PR changed; no Discord announcement published")
|
||||
return
|
||||
elif pull["state"] != "closed":
|
||||
print("PR is open; Discord announcement retained")
|
||||
return
|
||||
comment = report_comment(repo, number)
|
||||
if comment is None:
|
||||
if mode == "retire":
|
||||
return
|
||||
raise RuntimeError("Signed build report comment is missing")
|
||||
message_id = saved_message(comment["body"], discord.id)
|
||||
if mode == "retire":
|
||||
if message_id:
|
||||
message = discord.request("GET", f"/messages/{message_id}")
|
||||
if message:
|
||||
discord.request("PATCH", f"/messages/{message_id}",
|
||||
{"content": retired_content(message["content"], pull.get("merged", False)),
|
||||
"allowed_mentions": {"parse": []}, "flags": 4})
|
||||
return
|
||||
content = build_content(repo, number, pull["title"], sha, apk, release)
|
||||
if len(content) > 1800:
|
||||
raise RuntimeError("Discord announcement is too long")
|
||||
payload = {"content": content, "allowed_mentions": {"parse": []}, "flags": 4}
|
||||
message = discord.request("PATCH", f"/messages/{message_id}", payload) if message_id else None
|
||||
if message is None:
|
||||
message = discord.request("POST", "?wait=true", payload)
|
||||
if message is None:
|
||||
raise RuntimeError("Discord webhook no longer exists")
|
||||
state = {"webhook_id": discord.id, "message_id": message["id"]}
|
||||
latest = github(f"repos/{repo}/issues/comments/{comment['id']}")
|
||||
body = STATE.sub("", latest["body"]).rstrip() + f"\n<!-- droiddeck-discord: {json.dumps(state)} -->"
|
||||
github(f"repos/{repo}/issues/comments/{comment['id']}", "PATCH", {"body": body})
|
||||
print("Discord PR announcement updated")
|
||||
|
||||
|
||||
def main():
|
||||
webhook = os.environ.get("WEBHOOK", "")
|
||||
if not webhook:
|
||||
print("::notice::No DISCORD_PR_BUILDS_WEBHOOK secret; nothing posted")
|
||||
return
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
number = os.environ["PR_NUMBER"]
|
||||
if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repo) or not re.fullmatch(r"[1-9][0-9]*", number):
|
||||
raise RuntimeError("Invalid PR configuration")
|
||||
mode = sys.argv[1]
|
||||
if mode not in ("publish", "retire"):
|
||||
raise RuntimeError("Invalid announcement mode")
|
||||
sha, apk, release = (os.environ.get(key, "") for key in ("HEAD_SHA", "APK", "RELEASE"))
|
||||
if mode == "publish":
|
||||
prefix = f"https://github.com/Droid-Deck/DroidDeck-CI/releases/"
|
||||
if (not re.fullmatch(r"[0-9a-f]{40}", sha)
|
||||
or not apk.startswith(prefix + f"download/pr-{number}/")
|
||||
or release != prefix + f"tag/pr-{number}"
|
||||
or re.search(r"[\s<>]", apk)):
|
||||
raise RuntimeError("Invalid published build configuration")
|
||||
sync(mode, repo, number, Discord(webhook), sha, apk, release)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as error:
|
||||
print(f"::error::{error}" if isinstance(error, RuntimeError) else "::error::Discord announcement failed", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -8,6 +8,7 @@ import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
@@ -218,23 +219,28 @@ class AgentTest(unittest.TestCase):
|
||||
self.assertIn("ReferenceError", answer["error"]["message"])
|
||||
|
||||
def test_serve_answers_request_files(self):
|
||||
os.environ.pop("DISPLAY", None)
|
||||
thread = threading.Thread(target=agent.serve, args=(self.dir, False, 0.02), daemon=True)
|
||||
thread.start()
|
||||
server = subprocess.Popen([sys.executable, str(SCRIPT), "--no-focus"],
|
||||
env=dict(os.environ, BL_LAUNCH_DIR=str(self.dir)),
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
def stop_server():
|
||||
server.terminate()
|
||||
server.wait(timeout=5)
|
||||
self.addCleanup(stop_server)
|
||||
agent_dir = self.dir / "agent"
|
||||
for _ in range(100):
|
||||
if (self.dir / "hello.json").exists():
|
||||
if (agent_dir / "hello.json").exists():
|
||||
break
|
||||
time.sleep(0.02)
|
||||
hello = json.loads((self.dir / "hello.json").read_text())
|
||||
self.assertEqual(os.getpid(), hello["pid"])
|
||||
hello = json.loads((agent_dir / "hello.json").read_text())
|
||||
self.assertEqual(server.pid, hello["pid"])
|
||||
self.assertFalse(hello["focus"])
|
||||
(self.dir / "req" / "a.json.tmp").write_text(json.dumps({"kind": "ping"}))
|
||||
os.replace(self.dir / "req" / "a.json.tmp", self.dir / "req" / "a.json")
|
||||
(agent_dir / "req" / "a.json.tmp").write_text(json.dumps({"kind": "ping"}))
|
||||
os.replace(agent_dir / "req" / "a.json.tmp", agent_dir / "req" / "a.json")
|
||||
for _ in range(200):
|
||||
if (self.dir / "resp" / "a.json").exists():
|
||||
if (agent_dir / "resp" / "a.json").exists():
|
||||
break
|
||||
time.sleep(0.02)
|
||||
self.assertEqual(agent.VERSION, json.loads((self.dir / "resp" / "a.json").read_text())["version"])
|
||||
self.assertEqual(agent.VERSION, json.loads((agent_dir / "resp" / "a.json").read_text())["version"])
|
||||
|
||||
def test_game_pids_reads_the_steam_environment(self):
|
||||
import subprocess
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from tools.release import pr_discord as module
|
||||
|
||||
|
||||
class PrDiscordTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.pull = {"state": "open", "draft": False, "base": {"ref": "main"},
|
||||
"head": {"sha": "a" * 40}, "title": "Fix **input** @everyone", "merged": False}
|
||||
self.comment = {"id": 10, "body": module.REPORT + "\nBuild download"}
|
||||
self.discord = Mock(id="1554202953248940172")
|
||||
self.discord.request.return_value = {"id": "1557547293375332465"}
|
||||
self.github = patch.object(module, "github").start()
|
||||
self.addCleanup(patch.stopall)
|
||||
self.github.side_effect = lambda path, method="GET", body=None: self.pull if "/pulls/" in path else self.comment
|
||||
patch.object(module, "report_comment", return_value=self.comment).start()
|
||||
|
||||
def publish(self):
|
||||
module.sync("publish", "Droid-Deck/DroidDeck", "410", self.discord, "a" * 40,
|
||||
"https://github.com/Droid-Deck/DroidDeck-CI/releases/download/pr-410/build.apk",
|
||||
"https://github.com/Droid-Deck/DroidDeck-CI/releases/tag/pr-410")
|
||||
|
||||
def save_state(self):
|
||||
self.comment["body"] += '\n<!-- droiddeck-discord: ' + json.dumps({
|
||||
"webhook_id": self.discord.id, "message_id": "1557547293375332465"}) + ' -->'
|
||||
|
||||
def test_create_confirmed_message_and_persist_reference(self):
|
||||
self.publish()
|
||||
args = self.discord.request.call_args.args
|
||||
self.assertEqual(("POST", "?wait=true"), args[:2])
|
||||
self.assertEqual({"parse": []}, args[2]["allowed_mentions"])
|
||||
self.assertIn(r"Fix \*\*input\*\*", args[2]["content"])
|
||||
saved = self.github.call_args.args[2]["body"]
|
||||
self.assertEqual("1557547293375332465", module.saved_message(saved, self.discord.id))
|
||||
self.assertIn("Build download", saved)
|
||||
|
||||
def test_new_build_updates_existing_message(self):
|
||||
self.save_state()
|
||||
self.publish()
|
||||
self.assertEqual("PATCH", self.discord.request.call_args.args[0])
|
||||
saved = self.github.call_args.args[2]["body"]
|
||||
self.assertEqual(1, len(module.STATE.findall(saved)))
|
||||
|
||||
def test_deleted_message_is_recreated(self):
|
||||
self.save_state()
|
||||
self.discord.request.side_effect = [None, {"id": "1557547293375332466"}]
|
||||
self.publish()
|
||||
self.assertEqual(["PATCH", "POST"], [c.args[0] for c in self.discord.request.call_args_list])
|
||||
|
||||
def test_changed_pr_does_not_publish(self):
|
||||
for change in ({"state": "closed"}, {"draft": True}, {"base": {"ref": "other"}},
|
||||
{"head": {"sha": "b" * 40}}):
|
||||
original = self.pull.copy()
|
||||
self.pull.update(change)
|
||||
self.publish()
|
||||
self.discord.request.assert_not_called()
|
||||
self.pull = original
|
||||
|
||||
def test_retirement_strikes_original_and_distinguishes_merge(self):
|
||||
self.save_state()
|
||||
original = module.build_content("Droid-Deck/DroidDeck", "410", self.pull["title"], "a" * 40, "apk", "release")
|
||||
for merged, label in ((True, "Merged"), (False, "Closed")):
|
||||
self.pull.update(state="closed", merged=merged)
|
||||
self.discord.request.side_effect = [{"content": original}, {"id": "1557547293375332465"}]
|
||||
module.sync("retire", "Droid-Deck/DroidDeck", "410", self.discord)
|
||||
payload = self.discord.request.call_args.args[2]
|
||||
self.assertTrue(payload["content"].startswith(f"**{label}** —"))
|
||||
self.assertIn("~~Download:", payload["content"])
|
||||
self.assertEqual({"parse": []}, payload["allowed_mentions"])
|
||||
self.assertEqual(4, payload["flags"])
|
||||
|
||||
def test_retirement_is_idempotent_and_reopen_skips_it(self):
|
||||
once = module.retired_content("first\n\nlast", True)
|
||||
self.assertEqual(once, module.retired_content(once, True))
|
||||
self.save_state()
|
||||
module.sync("retire", "Droid-Deck/DroidDeck", "410", self.discord)
|
||||
self.discord.request.assert_not_called()
|
||||
|
||||
def test_old_build_without_saved_id_is_not_guessed(self):
|
||||
self.pull["state"] = "closed"
|
||||
module.sync("retire", "Droid-Deck/DroidDeck", "410", self.discord)
|
||||
self.discord.request.assert_not_called()
|
||||
|
||||
def test_replaced_webhook_cannot_edit_old_webhook_message(self):
|
||||
self.save_state()
|
||||
self.discord.id = "1554202953248940173"
|
||||
self.publish()
|
||||
self.assertEqual("POST", self.discord.request.call_args.args[0])
|
||||
|
||||
def test_only_github_actions_report_is_trusted(self):
|
||||
with patch.object(module, "report_comment", wraps=original_report):
|
||||
forged = {"user": {"login": "contributor"}, "body": module.REPORT, "id": 1}
|
||||
real = {"user": {"login": "github-actions[bot]"}, "body": module.REPORT, "id": 2}
|
||||
self.github.side_effect = [[forged, real]]
|
||||
self.assertEqual(2, module.report_comment("Droid-Deck/DroidDeck", "410")["id"])
|
||||
|
||||
def test_invalid_webhook_never_echoes_secret(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "^Invalid Discord webhook configuration$"):
|
||||
module.Discord("https://attacker.invalid/webhooks/secret")
|
||||
|
||||
def test_network_error_redacts_webhook_and_does_not_retry_creation(self):
|
||||
webhook = "https://discord.com/api/webhooks/1554202953248940172/private_token"
|
||||
with patch.object(module.urllib.request, "urlopen", side_effect=module.urllib.error.URLError(webhook)) as request:
|
||||
with self.assertRaisesRegex(RuntimeError, "^Discord request failed$"):
|
||||
module.Discord(webhook).request("POST", "?wait=true", {"content": "build"})
|
||||
self.assertEqual(1, request.call_count)
|
||||
|
||||
def test_edit_failure_does_not_create_duplicate_announcement(self):
|
||||
self.save_state()
|
||||
self.discord.request.side_effect = RuntimeError("Discord request failed")
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.publish()
|
||||
self.assertEqual(1, self.discord.request.call_count)
|
||||
self.assertEqual("PATCH", self.discord.request.call_args.args[0])
|
||||
|
||||
def test_missing_build_report_prevents_untracked_message(self):
|
||||
module.report_comment.return_value = None
|
||||
with self.assertRaisesRegex(RuntimeError, "Signed build report comment is missing"):
|
||||
self.publish()
|
||||
self.discord.request.assert_not_called()
|
||||
|
||||
def test_title_cannot_insert_status_or_strikethrough(self):
|
||||
content = module.build_content("Droid-Deck/DroidDeck", "410", "~~Retired~~\n**Merged**", "a" * 40, "apk", "release")
|
||||
self.assertNotIn("\n**Merged**", content)
|
||||
self.assertIn(r"\~\~Retired\~\~", content)
|
||||
self.assertLessEqual(len(module.retired_content(content, True)), 2000)
|
||||
|
||||
|
||||
original_report = module.report_comment
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -6,7 +6,7 @@ from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
|
||||
|
||||
@@ -76,8 +76,8 @@ class ImportsTest(unittest.TestCase):
|
||||
result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True})))
|
||||
with patch.object(imports, 'identify', side_effect=[None, 42]) as identify, \
|
||||
patch.object(imports, 'request_restart'), \
|
||||
patch.object(imports.time, 'monotonic', side_effect=[0, 301]), \
|
||||
patch.object(imports.time, 'sleep', side_effect=[None, RuntimeError('stop')]), \
|
||||
patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=[0, 301]),
|
||||
sleep=Mock(side_effect=[None, RuntimeError('stop')]))), \
|
||||
patch.object(imports.subprocess, 'run', return_value=result), \
|
||||
patch('builtins.print'):
|
||||
with self.assertRaisesRegex(RuntimeError, 'stop'):
|
||||
@@ -98,8 +98,8 @@ class ImportsTest(unittest.TestCase):
|
||||
with patch.object(imports, 'identify', return_value=42), \
|
||||
patch.object(imports, 'game_running', return_value=False), \
|
||||
patch.object(imports, 'request_restart') as restart, \
|
||||
patch.object(imports.time, 'monotonic', side_effect=list(range(0, 30 * beats, 30))), \
|
||||
patch.object(imports.time, 'sleep', side_effect=[None] * (beats - 1) + [RuntimeError('stop')]), \
|
||||
patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=list(range(0, 30 * beats, 30))),
|
||||
sleep=Mock(side_effect=[None] * (beats - 1) + [RuntimeError('stop')]))), \
|
||||
patch.object(imports.subprocess, 'run', return_value=result), \
|
||||
patch('builtins.print'):
|
||||
with self.assertRaisesRegex(RuntimeError, 'stop'):
|
||||
|
||||
Reference in new issue
Block a user