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:
Kurt Himebauch authored and GitHub committed 2026-10-08 00:23:55 -04:00
1 parent 1304809240
commit 9d8ff9615f
6 files changed
+369 -36

No files matched your search

+16 -10
View File
@@ -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
+135
View File
@@ -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()
+5 -5
View File
@@ -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'):