diff --git a/.github/workflows/pr-release-cleanup.yml b/.github/workflows/pr-release-cleanup.yml index 0cd4867e6..af4581f45 100644 --- a/.github/workflows/pr-release-cleanup.yml +++ b/.github/workflows/pr-release-cleanup.yml @@ -13,13 +13,28 @@ permissions: contents: read pull-requests: read +concurrency: + group: pr-release-${{ github.event.pull_request.number }} + cancel-in-progress: false + queue: max + jobs: cleanup: runs-on: ubuntu-24.04 timeout-minutes: 5 steps: + - name: Check the current PR state + id: state + env: + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + set -euo pipefail + state=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq .state) + echo "closed=$([ "$state" = closed ] && echo true || echo false)" >> "$GITHUB_OUTPUT" # A token of the org's "DroidDeck CI Publisher" app, as build.yml's announce job uses. - uses: actions/create-github-app-token@v2 + if: ${{ steps.state.outputs.closed == 'true' }} id: ci-token with: app-id: ${{ vars.DROIDDECK_CI_APP_ID }} @@ -27,6 +42,7 @@ jobs: owner: Droid-Deck repositories: DroidDeck-CI - name: Delete the test build + if: ${{ steps.state.outputs.closed == 'true' }} env: GH_TOKEN: ${{ steps.ci-token.outputs.token }} PR_NUMBER: ${{ github.event.pull_request.number }} @@ -42,6 +58,19 @@ jobs: done [ "$found" = 1 ] || echo "No test build for PR #$PR_NUMBER" + - uses: actions/checkout@v4 + if: ${{ steps.state.outputs.closed == 'true' }} + with: + ref: main + persist-credentials: false + - name: Retire the Discord PR announcement + if: ${{ steps.state.outputs.closed == 'true' }} + env: + WEBHOOK: ${{ secrets.DISCORD_PR_BUILDS_WEBHOOK }} + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number }} + run: python3 tools/release/pr_discord.py retire + catalog: needs: cleanup if: ${{ always() }} diff --git a/.github/workflows/pr-release-publish.yml b/.github/workflows/pr-release-publish.yml index 6679d9482..ffe2e2c1d 100644 --- a/.github/workflows/pr-release-publish.yml +++ b/.github/workflows/pr-release-publish.yml @@ -26,6 +26,7 @@ permissions: concurrency: group: pr-release-${{ github.event_name == 'pull_request_target' && github.event.pull_request.number || github.event_name == 'workflow_run' && github.event.workflow_run.pull_requests[0].number || inputs.pr_number }} cancel-in-progress: false + queue: max jobs: authorize: @@ -178,6 +179,10 @@ jobs: issues: write pull-requests: write steps: + - uses: actions/checkout@v4 + with: + ref: main + persist-credentials: false - uses: actions/download-artifact@v4 with: pattern: droiddeck-pr-signed-* @@ -307,39 +312,27 @@ jobs: $NEXT EOF ) - id=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" --paginate \ - --jq ".[] | select(.body | startswith(\"$mark\")) | .id" | head -1 || true) + comment=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" --paginate --slurp \ + --jq "[.[][] | select(.user.login == \"github-actions[bot]\") | select(.body | startswith(\"$mark\"))] | first // {}") + id=$(jq -r '.id // empty' <<<"$comment") if [ -n "$id" ]; then + state=$(jq -r '.body | split("\n")[] | select(startswith(""))' <<<"$comment") + if [ -n "$state" ]; then body=$(printf '%s\n%s' "$body" "$state"); fi gh api -X PATCH "repos/$GITHUB_REPOSITORY/issues/comments/$id" -f body="$body" >/dev/null else gh api -X POST "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" -f body="$body" >/dev/null fi - # Asked for (the label, a manual run): always announced. Signed on its own after a push: - # only a PR's first test build is, and later ones update its release quietly. - - name: Post to Discord - if: ${{ always() && steps.publish.outcome == 'success' && (needs.authorize.outputs.automatic != 'true' || steps.publish.outputs.first == 'true') }} + - name: Update the Discord PR announcement + if: ${{ always() && steps.publish.outcome == 'success' }} env: - WEBHOOK: ${{ secrets.DISCORD_BUILDS_WEBHOOK }} + WEBHOOK: ${{ secrets.DISCORD_PR_BUILDS_WEBHOOK }} GH_TOKEN: ${{ github.token }} PR_NUMBER: ${{ needs.authorize.outputs.pr_number }} HEAD_SHA: ${{ needs.authorize.outputs.head_sha }} RELEASE: ${{ steps.publish.outputs.release }} APK: ${{ steps.publish.outputs.apk }} - run: | - set -euo pipefail - if [ -z "$WEBHOOK" ]; then - echo "::notice::No DISCORD_BUILDS_WEBHOOK secret; nothing posted" - exit 0 - fi - short=${HEAD_SHA:0:7} - title=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq .title) - what="[#$PR_NUMBER]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER): $title" - get=$(printf 'Download: [standard APK](<%s>) · [all variants](<%s>)' "$APK" "$RELEASE") - content=$(printf '**New signed PR test build** \`pr-%s\` · \`%s\`\n%s\n_Not merged — for testing._\n%s' "$PR_NUMBER" "$short" "$what" "$get") - jq -n --arg content "$content" '{content: $content, allowed_mentions: {parse: []}}' | - curl -fsS -H 'Content-Type: application/json' -d @- "$WEBHOOK" -o /dev/null - echo "Posted: $content" + run: python3 tools/release/pr_discord.py publish catalog: needs: publish diff --git a/tools/release/pr_discord.py b/tools/release/pr_discord.py new file mode 100644 index 000000000..acb5acdb5 --- /dev/null +++ b/tools/release/pr_discord.py @@ -0,0 +1,170 @@ +import json +import os +import re +import subprocess +import sys +import time +import urllib.error +import urllib.request + + +REPORT = "" +STATE = re.compile(r"\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" + 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) diff --git a/tools/tests/test_droiddeck_agent.py b/tools/tests/test_droiddeck_agent.py index e0dce56a7..c04f2eeb8 100644 --- a/tools/tests/test_droiddeck_agent.py +++ b/tools/tests/test_droiddeck_agent.py @@ -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 diff --git a/tools/tests/test_pr_discord.py b/tools/tests/test_pr_discord.py new file mode 100644 index 000000000..8de1d1d74 --- /dev/null +++ b/tools/tests/test_pr_discord.py @@ -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' + + 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() diff --git a/tools/tests/test_steam_games.py b/tools/tests/test_steam_games.py index e7878d554..f74277714 100644 --- a/tools/tests/test_steam_games.py +++ b/tools/tests/test_steam_games.py @@ -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'):