Commit JSON files to a branch. Args: files: list of (file_path, data_dict) tuples label: suffix for commit messages, e.g. "for 2026-02-12"
(
files: List[tuple],
branch: str,
github_token: str,
owner: str,
repo: str,
label: str = "",
)
| 916 | |
| 917 | |
| 918 | def commit_files_to_branch( |
| 919 | files: List[tuple], |
| 920 | branch: str, |
| 921 | github_token: str, |
| 922 | owner: str, |
| 923 | repo: str, |
| 924 | label: str = "", |
| 925 | ) -> None: |
| 926 | """Commit JSON files to a branch. |
| 927 | |
| 928 | Args: |
| 929 | files: list of (file_path, data_dict) tuples |
| 930 | label: suffix for commit messages, e.g. "for 2026-02-12" |
| 931 | """ |
| 932 | import base64 as _b64 |
| 933 | |
| 934 | headers = _github_headers(github_token) |
| 935 | |
| 936 | for file_path, data in files: |
| 937 | if data is None: |
| 938 | continue |
| 939 | content = data if isinstance(data, str) else json.dumps(data, indent=2, ensure_ascii=False) |
| 940 | encoded = _b64.b64encode(content.encode()).decode() |
| 941 | |
| 942 | sha = get_file_sha(github_token, owner, repo, file_path, branch) |
| 943 | if not sha and branch != GISTS_BRANCH: |
| 944 | sha = get_file_sha(github_token, owner, repo, file_path, "main") |
| 945 | |
| 946 | msg = f"news: add {file_path.split('/')[-1]}" |
| 947 | if label: |
| 948 | msg += f" {label}" |
| 949 | |
| 950 | payload = { |
| 951 | "message": msg, |
| 952 | "content": encoded, |
| 953 | "branch": branch, |
| 954 | } |
| 955 | if sha: |
| 956 | payload["sha"] = sha |
| 957 | |
| 958 | resp = github_api_request( |
| 959 | "PUT", |
| 960 | f"{GITHUB_API_BASE}/repos/{owner}/{repo}/contents/{file_path}", |
| 961 | headers=headers, |
| 962 | json=payload, |
| 963 | ) |
| 964 | if resp.status_code in [200, 201]: |
| 965 | print(f" Committed {file_path}") |
| 966 | else: |
| 967 | print(f" Error committing {file_path}: {resp.status_code} {resp.text[:200]}") |
| 968 | |
| 969 | |
| 970 | # ── VPS deployment ─────────────────────────────────────────────────── |
no test coverage detected