{"record":{"id":"1e8b214d0f0a3661","repo":"google-gemini/gemini-cli","slug":"github-token-is-missing-cannot-authorize-pull-req","errorCode":null,"errorMessage":"GitHub token is missing. Cannot authorize Pull Request creation.","messagePattern":"GitHub token is missing\\. Cannot authorize Pull Request creation\\.","errorType":"exception","errorClass":"GitHubClientError","httpStatus":null,"severity":"critical","filePath":"tools/caretaker-agent/cloudrun/pr-generator/workflow/github_client.py","lineNumber":50,"sourceCode":"\n    def create_pull_request(\n        self, branch_name: str, title: str, body: str\n    ) -> str:\n        \"\"\"Submits a POST request to GitHub to create a new Pull Request.\n\n        Args:\n            branch_name: The feature branch to be merged.\n            title: Title of the Pull Request.\n            body: Body description markdown of the Pull Request.\n\n        Returns:\n            The PR number of the successfully created Pull Request as a string.\n\n        Raises:\n            GitHubClientError: If the HTTP request fails or token is missing.\n        \"\"\"\n        if not self._token:\n            raise GitHubClientError(\n                \"GitHub token is missing. Cannot authorize Pull Request creation.\"\n            )\n\n        data = {\n            \"title\": title,\n            \"body\": body,\n            \"head\": branch_name,\n            \"base\": \"main\",\n        }\n\n        req = urllib.request.Request(\n            self._base_url,\n            data=json.dumps(data).encode(\"utf-8\"),\n            headers={\n                \"Accept\": \"application/vnd.github+json\",\n                \"Authorization\": f\"Bearer {self._token}\",\n                \"X-GitHub-Api-Version\": \"2022-11-28\",\n                \"Content-Type\": \"application/json\",","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/google-gemini/gemini-cli/blob/5024443c7217464a66e98f80d73172a26440bd8f/tools/caretaker-agent/cloudrun/pr-generator/workflow/github_client.py#L32-L68","documentation":"GitHubClientError 'GitHub token is missing. Cannot authorize Pull Request creation.' is raised by create_pull_request when self._token is falsy. The PR endpoint requires a Bearer token; without one the request would 401, so the client fails fast with a clear message before any network call.","triggerScenarios":"GitHubClient(token=None or '') constructed -> create_pull_request(...) -> `if not self._token: raise GitHubClientError(...)` at line 49-52, before building the urllib Request.","commonSituations":"GH_TOKEN/GITHUB_TOKEN env var not injected into the CloudRun service; secret name typo in the deploy config; token injected as empty string; the workflow's token-load step ran before the env var was exported.","solutions":["Set GH_TOKEN (or whichever var the GitHubClient is constructed from) in the CloudRun service env / secret mount.","Construct GitHubClient with a non-empty token: GitHubClient(token=os.environ['GH_TOKEN'], owner=..., repo=...).","Add a startup assertion that the token is present so the failure surfaces at boot, not at first PR."],"exampleFix":"# before\nclient = GitHubClient(token=os.environ.get('GH_TOKEN'), owner=o, repo=r)\n# after\ntoken = os.environ['GH_TOKEN']  # KeyError surfaces the missing secret early\nclient = GitHubClient(token=token, owner=o, repo=r)","handlingStrategy":"validation","validationCode":"token = os.environ.get('GH_TOKEN') or os.environ.get('GITHUB_TOKEN')\nif not token:\n    raise SystemExit('GH_TOKEN env var required to create PRs')","typeGuard":"def is_missing_github_token(e: Exception) -> bool:\n    return isinstance(e, GitHubClientError) and 'token is missing' in str(e).lower()","tryCatchPattern":"try:\n    pr_number = client.create_pull_request(branch, title, body)\nexcept GitHubClientError as e:\n    if 'token is missing' in str(e): raise SystemExit('configure GH_TOKEN secret')\n    raise","preventionTips":["Mount the GitHub token as a Cloud Run secret and assert it at startup.","Construct GitHubClient only after confirming the token is non-empty."],"tags":["github","auth","missing-token","python"],"backgroundTag":null,"analyzedSha":"5024443c7217464a66e98f80d73172a26440bd8f","analyzedAt":"2026-08-12T06:01:53.711Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}