"""Candidate compilation may read store version state but cannot claim a store upload."""
import json
import os
from pathlib import Path


def active() -> bool:
    if os.environ.get("MOBILE_CANDIDATE_BUILD") != "true":
        return False
    event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text())
    pull = event.get("pull_request", {})
    head, base = pull.get("head", {}), pull.get("base", {})
    if (os.environ.get("GITHUB_EVENT_NAME") != "pull_request"
            or head.get("repo", {}).get("full_name") != os.environ.get("GITHUB_REPOSITORY")
            or not str(head.get("ref", "")).startswith("task/")
            or base.get("ref") != event.get("repository", {}).get("default_branch")):
        raise ValueError("mobile_release_candidate_scope_invalid")
    return True
