From f46875a9d56c29fedfacaa0c158aa1fae9efc6df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Fri, 15 Sep 2023 16:55:06 +0200 Subject: [PATCH] Allow the use of github token --- .github/workflows/update_extensions.py | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/.github/workflows/update_extensions.py b/.github/workflows/update_extensions.py index 1f68671..44e0b95 100755 --- a/.github/workflows/update_extensions.py +++ b/.github/workflows/update_extensions.py @@ -3,7 +3,7 @@ Download extensions for the current mediawiki version, and update the conf files. """ -from typing import List, Optional +from typing import List, Optional, Any import hashlib import urllib import datetime @@ -14,6 +14,8 @@ import requests GITHUB_API_URL = "https://api.github.com/repos" +GITHUB_API_TOKEN = False + # Update this after updating mediawiki version. ACCEPTABLE_BRANCHES = [ "REL1_40", @@ -21,6 +23,24 @@ ACCEPTABLE_BRANCHES = [ ] +def github_get(path: str, *args, **kwargs) -> Any: + headers = kwargs.get("headers", {}) + if GITHUB_API_TOKEN: + headers["Authorization"] = f"Bearer {GITHUB_API_TOKEN}" + + kwargs["headers"] = headers + result = requests.get( + f"{GITHUB_API_URL}/{path}", + timeout=10, + *args, + **kwargs + ) + if result.status_code == requests.codes["forbidden"]: + raise RuntimeError(result.json().get("message"), result.json().get("documentation_url")) + + return result.json() + + def sha256sum_of_url(url: str) -> str: """Compute checksum without saving the file""" checksum = hashlib.sha256() @@ -57,18 +77,18 @@ def get_repo(url: str) -> str: def get_branches(repo: str) -> List[str]: - branches = requests.get(f"{GITHUB_API_URL}/{repo}/branches", timeout=10).json() + branches = github_get(f"{repo}/branches") names = [branch["name"] for branch in branches] return names def get_last_commit_of(repo: str, branch: str) -> str: - commit = requests.get(f"{GITHUB_API_URL}/{repo}/commits/{branch}", timeout=10).json() + commit = github_get(f"{repo}/commits/{branch}") return commit["sha"] def timestamp_of_commit(repo: str, sha: str) -> int: - commit = requests.get(f"{GITHUB_API_URL}/{repo}/commits/{sha}", timeout=10).json() + commit = github_get(f"{repo}/commits/{sha}") try: date = commit["commit"]["author"]["date"] except :