From c34c02adec9eef6178fbbdc6c63ffc03c7ef9d87 Mon Sep 17 00:00:00 2001 From: Florian Asche Date: Sat, 21 Nov 2026 07:13:54 +0100 Subject: [PATCH] fix(imager): authenticate GitHub API requests to avoid the rate limit Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/create-rpi-image-json.yml | 2 ++ scripts/generate-imager-json.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/create-rpi-image-json.yml b/.github/workflows/create-rpi-image-json.yml index 43c82e1..237337c 100644 --- a/.github/workflows/create-rpi-image-json.yml +++ b/.github/workflows/create-rpi-image-json.yml @@ -17,6 +17,8 @@ jobs: run: | chmod +x scripts/generate-imager-json.py python3 scripts/generate-imager-json.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Delete existing RPI Imager JSON release uses: actions/github-script@v9 diff --git a/scripts/generate-imager-json.py b/scripts/generate-imager-json.py index 09b2cd7..2d3c2ff 100755 --- a/scripts/generate-imager-json.py +++ b/scripts/generate-imager-json.py @@ -4,6 +4,7 @@ import io import json +import os import sys import re import urllib.request @@ -16,6 +17,15 @@ OUTPUT_FILE = "rpi-imager.json" API_URL = f"https://api.github.com/repos/{OWNER}/{REPO}/releases?per_page=100" +def get_api_headers(): + """Build the GitHub API headers, authenticated when GITHUB_TOKEN is set to avoid the anonymous rate limit.""" + headers = {'Accept': 'application/vnd.github+json'} + token = os.environ.get('GITHUB_TOKEN') + if token: + headers['Authorization'] = f'Bearer {token}' + return headers + + def fetch_releases(): """Fetch releases from GitHub API.""" print("Fetching releases from GitHub...") @@ -23,7 +33,8 @@ def fetch_releases(): try: page = 1 while True: - with urllib.request.urlopen(f"{API_URL}&page={page}") as response: + request = urllib.request.Request(f"{API_URL}&page={page}", headers=get_api_headers()) + with urllib.request.urlopen(request) as response: page_data = json.loads(response.read().decode()) if not page_data: break