From 8126e5de21c9e4985a9d1433326260d8662bd9db Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Tue, 27 Aug 2024 20:19:17 -0500 Subject: [PATCH] Make Gitea auth token optional For e.g. testing when running with `--dry-run`, the token may not be necessary. --- updatebot.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/updatebot.py b/updatebot.py index 953923c..c9f477e 100644 --- a/updatebot.py +++ b/updatebot.py @@ -166,7 +166,7 @@ Project = Union[ class RepoConfig(pydantic.BaseModel): url: str - token_file: Path + token_file: Optional[Path] = None branch: str = 'master' @functools.cached_property @@ -176,16 +176,18 @@ class RepoConfig(pydantic.BaseModel): return urllib.parse.urlunsplit(urlparts) @functools.cached_property - def auth_token(self) -> str: - return self.token_file.read_text().strip() + def auth_token(self) -> Optional[str]: + if self.token_file: + return self.token_file.read_text().strip() def get_git_url(self) -> str: session = _get_session() + headers = {} + if token := self.auth_token: + headers['Authorization'] = f'Bearer {token}' r = session.get( self.repo_api_url, - headers={ - 'Authorization': f'token {self.auth_token}', - }, + headers=headers, ) data = r.json() if ssh_url := data.get('ssh_url'):