fix(ntn-files): propagate NOTION_API_KEY into ntn subprocesses
Some checks failed
Verify Skills / verify-skills (push) Has been cancelled

Force ntn to authenticate as the same integration that writes the page by
injecting NOTION_API_TOKEN=$NOTION_API_KEY into every ntn subprocess env.
Notion scopes file uploads to the creating integration, so a mismatch caused
"Could not find file_upload with ID …" when attaching uploaded images.

Added test_upload_passes_token_env (9 total in test_ntn_files.py) and updated
code/CLAUDE.md to reflect that a separate ntn login is no longer required.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-27 11:00:56 +09:00
parent 759bb20911
commit 50c6741c76
3 changed files with 30 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ lifecycle (create -> send bytes -> complete) and prints the upload ID.
from __future__ import annotations
import json
import os
import shutil
import subprocess
import sys
@@ -17,6 +18,19 @@ from typing import Dict, List, Optional, Callable, Any
_WORKSPACE: Optional[Dict[str, str]] = None
def _ntn_env():
"""Env for ntn subprocesses: force ntn to authenticate as the SAME
integration the script uses (NOTION_API_KEY/NOTION_TOKEN), so an uploaded
file and the page that references it share one identity. Notion scopes
file uploads to the creating integration, so a mismatch makes the upload
un-attachable."""
env = dict(os.environ)
token = os.environ.get("NOTION_API_KEY") or os.environ.get("NOTION_TOKEN")
if token:
env["NOTION_API_TOKEN"] = token
return env
class NtnUploadError(Exception):
"""Raised when `ntn` is unavailable or a file upload fails."""
@@ -45,7 +59,7 @@ def preflight() -> Dict[str, str]:
result = subprocess.run(
["ntn", "api", "v1/users/me"],
capture_output=True, text=True,
capture_output=True, text=True, env=_ntn_env(),
)
if result.returncode != 0:
raise NtnUploadError(
@@ -73,7 +87,7 @@ def upload(path: Path) -> str:
with open(path, "rb") as fh:
result = subprocess.run(
["ntn", "files", "create", "--plain"],
stdin=fh, capture_output=True, text=True,
stdin=fh, capture_output=True, text=True, env=_ntn_env(),
)
if result.returncode != 0:
raise NtnUploadError(