From 6fa66870673b010a68c80cc0592bd658090a1b23 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Fri, 4 Dec 2026 14:27:59 +0000 Subject: [PATCH] ci(android): Add a links-only mode to the verify-pr test workflow Co-Authored-By: Antonis Lilis --- .github/verify-pr-spike/spike.py | 41 ++++++++++++++++++--- .github/workflows/verify-pr-spike.yml | 53 +++++++++++++++++++++++++-- samples/react-native/src/verifyRun.ts | 16 ++++++-- 3 files changed, 97 insertions(+), 13 deletions(-) diff --git a/.github/verify-pr-spike/spike.py b/.github/verify-pr-spike/spike.py index aea7e25988..3e656db677 100644 --- a/.github/verify-pr-spike/spike.py +++ b/.github/verify-pr-spike/spike.py @@ -9,7 +9,9 @@ checks. After each flow it reads the ids of the error events the app sent from the device log, and with $VERIFY_TRANSACTIONS set to `true` the ids of its transactions too; the `outputs` step hands them to the fetch job, one -comma-separated list per side and kind. +comma-separated list per side and kind. With $VERIFY_LINKS_ONLY set to `true` a +side needs no event id, and each side's UTC flow start and end are recorded and +printed in the summary instead, for a person to look its metrics up by hand. Each subcommand is one workflow step. Results go to $RUNNER_TEMP/verify-pr-spike, which the workflow uploads. @@ -25,6 +27,7 @@ import sys import time import zipfile +from datetime import datetime, timezone from pathlib import Path # The `ref` checkout: the code under test. This script comes from main. @@ -40,6 +43,8 @@ SIDES = ("without", "with") # The workflow's `transactions` input: collect transaction ids as evidence. TRANSACTIONS = os.environ["VERIFY_TRANSACTIONS"] == "true" +# The workflow's `links_only` input: the evidence is a metric, read by hand. +LINKS_ONLY = os.environ["VERIFY_LINKS_ONLY"] == "true" CORE = "packages/core" CORE_SRC = f"{CORE}/src/" # Same exclusions as the skill's patch (SKILL.md step 2). @@ -243,12 +248,16 @@ def adb(*args, **kwargs): return run(["adb", *args], **kwargs) +def utc_now() -> str: + return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + def logged_ids(pattern, logcat): """Event ids from one kind of beforeSend log line, in order, without duplicates.""" return list(dict.fromkeys(pattern.findall(logcat))) -def run_side(side, results): +def run_side(side: str, results: dict[str, dict[str, object]]) -> int: pr = os.environ["VERIFY_PR"] # Set by the workflow from github.run_id; the fetch job expects the same. run_id = os.environ[f"RUN_ID_{side.upper()}"] @@ -269,6 +278,8 @@ def run_side(side, results): sys.exit(f"adb install did not print Success for {side}") adb("logcat", "-c") + if LINKS_ONLY: + record["flow_start_utc"] = utc_now() maestro = subprocess.run( [ "maestro", "test", @@ -282,6 +293,8 @@ def run_side(side, results): cwd=run_dir, check=False, ) + if LINKS_ONLY: + record["flow_end_utc"] = utc_now() record["maestro_exit"] = maestro.returncode time.sleep(SEND_WAIT_SECONDS) @@ -304,7 +317,7 @@ def run_side(side, results): return maestro.returncode -def cmd_run(_args): +def cmd_run(_args: argparse.Namespace) -> None: results = load() results["timings"]["emulator_boot"] = round(time.time() - results["marks"]["emulator_start"]) failures = [] @@ -312,11 +325,13 @@ def cmd_run(_args): if run_side(side, results) != 0: failures.append(f"the Maestro flow failed for {side}") # With transactions, each side needs one and error events are optional. + # In links-only mode, which the workflow refuses with transactions, + # neither is needed. record = results["runs"][side] if TRANSACTIONS: if not record["transaction_ids"]: failures.append(f"no 'Transaction beforeSend:' event id in the device log for {side}") - elif not record["event_ids"]: + elif not LINKS_ONLY and not record["event_ids"]: failures.append(f"no 'Event beforeSend:' event id in the device log for {side}") save(results) if failures: @@ -340,13 +355,27 @@ def cmd_outputs(_args): fh.write(f"{side}_transaction_ids={','.join(transaction_ids[:MAX_TRANSACTIONS])}\n") -def cmd_summary(_args): +def cmd_summary(_args: argparse.Namespace) -> None: results = load() marks = results["marks"] if "job_start" in marks: results["timings"]["job_total"] = round(time.time() - marks["job_start"]) save(results) - lines = [f"## verify-pr test run for #{os.environ['VERIFY_PR']}", "", "```json", json.dumps(results, indent=2), "```"] + lines = [f"## verify-pr test run for #{os.environ['VERIFY_PR']}", ""] + if LINKS_ONLY: + # A side whose flow didn't start or finish has no time for it. + lines += [ + "Links-only run: nothing was read back from Sentry. Each side's run id and flow time window:", + "", + "| side | verify.run_id | flow start (UTC) | flow end (UTC) |", + "| --- | --- | --- | --- |", + ] + for side in SIDES: + record = results["runs"].get(side, {}) + row = [record.get(key, "not recorded") for key in ("run_id", "flow_start_utc", "flow_end_utc")] + lines.append(f"| {side} | {' | '.join(row)} |") + lines.append("") + lines += ["```json", json.dumps(results, indent=2), "```"] summary = os.environ.get("GITHUB_STEP_SUMMARY") if summary: with open(summary, "a") as fh: diff --git a/.github/workflows/verify-pr-spike.yml b/.github/workflows/verify-pr-spike.yml index 44ee8daf47..4155666abe 100644 --- a/.github/workflows/verify-pr-spike.yml +++ b/.github/workflows/verify-pr-spike.yml @@ -26,6 +26,15 @@ run-name: "${{ inputs.pr != '' && inputs.fetch_run_id == '' && inputs.probe_metr # `fetch_without_transaction_ids` or `fetch_with_transaction_ids` turn on # `transactions`. `ref`, `pr`, `base_sha` and `flow` are not needed then. # +# Links-only mode: with `links_only` set, for PRs whose evidence is a metric, +# which the token can't read. The build job builds and runs both sides as above +# but needs no error or transaction id on either side, records each side's run +# id and the UTC start and end of its flow in results.json, and prints them in +# the job summary for a person to look the metrics up by hand. The fetch job is +# skipped, so nothing is read back from Sentry. It can't be combined with +# `transactions`, a fetch-only input or `probe_metric`: the build job refuses +# that in its first step, so in this mode it runs even when one of those is set. +# # Probe mode: with `probe_metric` set, the build job is skipped and the fetch # job makes one request for that metric's rows of the last 24 hours, to find # out whether the token can read metrics. It prints only the URL, the HTTP @@ -63,6 +72,11 @@ on: required: false type: boolean default: false + links_only: + description: Links-only mode, for metric evidence. Builds and runs both sides, needs no event ids, records each side's run id and flow time window, and skips the fetch job + required: false + type: boolean + default: false fetch_run_id: description: Fetch-only mode. Id of an earlier run of this workflow, digits only; skips the build job required: false @@ -104,7 +118,9 @@ env: jobs: build: name: Build and run without and with the PR - if: ${{ inputs.fetch_run_id == '' && inputs.probe_metric == '' }} + # In links-only mode it always runs, so its first step can refuse links_only + # together with a fetch-only or probe input. + if: ${{ inputs.links_only || (inputs.fetch_run_id == '' && inputs.probe_metric == '') }} runs-on: ubuntu-latest timeout-minutes: 120 permissions: @@ -122,6 +138,7 @@ jobs: VERIFY_FLOW: ${{ inputs.flow }} BASE_SHA: ${{ inputs.base_sha }} VERIFY_TRANSACTIONS: ${{ inputs.transactions }} + VERIFY_LINKS_ONLY: ${{ inputs.links_only }} RUN_ID_WITHOUT: gh${{ github.run_id }}-without RUN_ID_WITH: gh${{ github.run_id }}-with steps: @@ -133,11 +150,40 @@ jobs: INPUT_PR: ${{ inputs.pr }} INPUT_BASE_SHA: ${{ inputs.base_sha }} INPUT_FLOW: ${{ inputs.flow }} + # Links-only mode and the inputs it can't be combined with. + INPUT_LINKS_ONLY: ${{ inputs.links_only }} + INPUT_TRANSACTIONS: ${{ inputs.transactions }} + INPUT_FETCH_RUN_ID: ${{ inputs.fetch_run_id }} + INPUT_FETCH_WITHOUT_IDS: ${{ inputs.fetch_without_ids }} + INPUT_FETCH_WITH_IDS: ${{ inputs.fetch_with_ids }} + INPUT_FETCH_WITHOUT_TRANSACTION_IDS: ${{ inputs.fetch_without_transaction_ids }} + INPUT_FETCH_WITH_TRANSACTION_IDS: ${{ inputs.fetch_with_transaction_ids }} + INPUT_PROBE_METRIC: ${{ inputs.probe_metric }} run: | import os import re import sys + # Each is refused with links_only when set. An unset boolean is "false". + LINKS_ONLY_EXCLUSIVE = { + "transactions": "INPUT_TRANSACTIONS", + "fetch_run_id": "INPUT_FETCH_RUN_ID", + "fetch_without_ids": "INPUT_FETCH_WITHOUT_IDS", + "fetch_with_ids": "INPUT_FETCH_WITH_IDS", + "fetch_without_transaction_ids": "INPUT_FETCH_WITHOUT_TRANSACTION_IDS", + "fetch_with_transaction_ids": "INPUT_FETCH_WITH_TRANSACTION_IDS", + "probe_metric": "INPUT_PROBE_METRIC", + } + if os.environ["INPUT_LINKS_ONLY"] == "true": + # Names only: an input's value is never printed. + combined = [ + name + for name, variable in LINKS_ONLY_EXCLUSIVE.items() + if os.environ[variable] not in ("", "false") + ] + if combined: + sys.exit(f"invalid inputs: links_only can't be combined with {', '.join(combined)}") + PATTERNS = { "ref": ("INPUT_REF", r"verify/[A-Za-z0-9._-]+"), "pr": ("INPUT_PR", r"[0-9]+"), @@ -309,8 +355,9 @@ jobs: fetch: name: Fetch the events from Sentry needs: build - # In fetch-only and probe mode the build job is skipped; otherwise it must pass. - if: ${{ !cancelled() && (inputs.fetch_run_id != '' || inputs.probe_metric != '' || needs.build.result == 'success') }} + # Skipped in links-only mode. In fetch-only and probe mode the build job is + # skipped; otherwise it must pass. + if: ${{ !cancelled() && !inputs.links_only && (inputs.fetch_run_id != '' || inputs.probe_metric != '' || needs.build.result == 'success') }} runs-on: ubuntu-latest timeout-minutes: 20 permissions: diff --git a/samples/react-native/src/verifyRun.ts b/samples/react-native/src/verifyRun.ts index 2546e90bfe..183f5b2df1 100644 --- a/samples/react-native/src/verifyRun.ts +++ b/samples/react-native/src/verifyRun.ts @@ -9,21 +9,29 @@ interface VerifyRunArgs { /** * Tags every event with the verification run passed as launch arguments, so - * the `verify-pr` skill can find its events in Sentry. No-op without them. + * the `verify-pr` skill can find its events in Sentry. Also sets them as scope + * attributes, because metrics, logs and streamed spans carry attributes, not + * tags. No-op without them. * * Android: `adb shell am start -n /.MainActivity -e verifyRunId ` * Maestro: `launchApp: { arguments: { verifyRunId: } }` */ export function tagVerifyRun(): void { const args = LaunchArguments.value(); - const tags: Record = { + const candidates: Record = { 'verify.pr': args.verifyPr, 'verify.commit': args.verifyCommit, 'verify.run_id': args.verifyRunId, }; - for (const [key, value] of Object.entries(tags)) { + const values: Record = {}; + for (const [key, value] of Object.entries(candidates)) { if (value) { - Sentry.setTag(key, String(value)); + values[key] = String(value); } } + if (Object.keys(values).length === 0) { + return; + } + Sentry.setTags(values); + Sentry.setAttributes(values); }