fastapi/fastapi · error · RuntimeError

No PR number available

Error message

No PR number available

What it means

Control-flow guard in notify_translations.main (scripts/notify_translations.py:324-327): the script needs a PR number to post comments; it takes it from the GitHub event payload (github_event.pull_request.number) or falls back to settings.number (the PR_NUMBER-style env var). If both are absent — the event is not a pull_request event and no number was configured — it raises RuntimeError('No PR number available').

Source

Thrown at scripts/notify_translations.py:326

    if settings.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)
    logging.debug(f"Using config: {settings.model_dump_json()}")
    g = Github(settings.github_token.get_secret_value())
    repo = g.get_repo(settings.github_repository)
    if not settings.github_event_path.is_file():
        raise RuntimeError(
            f"No github event file available at: {settings.github_event_path}"
        )
    contents = settings.github_event_path.read_text("utf-8")
    github_event = PartialGitHubEvent.model_validate_json(contents)
    logging.info(f"Using GitHub event: {github_event}")
    number = (
        github_event.pull_request and github_event.pull_request.number
    ) or settings.number
    if number is None:
        raise RuntimeError("No PR number available")
    number = cast(int, number)

    # Avoid race conditions with multiple labels
    sleep_time = random.random() * 10  # random number between 0 and 10 seconds
    logging.info(
        f"Sleeping for {sleep_time} seconds to avoid "
        "race conditions and multiple comments"
    )
    time.sleep(sleep_time)

    # Get PR
    logging.debug(f"Processing PR: #{number}")
    pr = repo.get_pull(number)
    label_strs = {label.name for label in pr.get_labels()}
    langs = []
    for label in label_strs:
        if label.startswith("lang-") and not label == lang_all_label:
            langs.append(label[5:])

View on GitHub (pinned to a1fa70d423)

Solutions

  1. Restrict the workflow to pull_request events (on: pull_request: types: [...] ) so the event always carries pull_request.number.
  2. For manual dispatch, provide the PR number via the workflow's 'number' input mapped to the corresponding env var read by Settings.
  3. If you must support other events, add an upfront guard in the workflow (if: github.event_name == 'pull_request') before calling the script.
  4. Log the event JSON when debugging to confirm which shape arrived.

Example fix

# before (.github/workflows/notify.yml)
on: [push, pull_request]  # push events have no PR number -> RuntimeError

# after
on:
  pull_request:
    types: [opened, synchronize, reopened]
Defensive patterns

Strategy: validation

Validate before calling

import json, os
from pathlib import Path

def resolve_pr_number() -> int | None:
    p = os.environ.get("GITHUB_EVENT_PATH")
    if p and Path(p).is_file():
        n = json.loads(Path(p).read_text("utf-8")).get("pull_request", {}).get("number")
        if n:
            return int(n)
    env_num = os.environ.get("PR_NUMBER")  # mirror of Settings.number
    return int(env_num) if env_num and env_num.isdigit() else None

Prevention

When it happens

Trigger: The workflow triggers on non-PR events (push, issue_comment, schedule) so the event JSON has no pull_request node, and no explicit number env var is set; or the event shape changed so pull_request.number is null.

Common situations: A workflow's 'on:' block widened to include pushes/schedules while reusing this script; manual workflow_dispatch runs without a number input; event payloads from fork PRs where some fields are trimmed.

Related errors


AI-assisted analysis of fastapi/fastapi@a1fa70d423 (2026-08-14). Data as JSON: /api/errors/24fd10f0316e3077. Report an issue: GitHub.