Make Gitea auth token optional

For e.g. testing when running with `--dry-run`, the token may not be
necessary.
master
Dustin 2024-08-27 20:19:17 -05:00
parent 5f8db2fa47
commit 8126e5de21
1 changed files with 8 additions and 6 deletions

View File

@ -166,7 +166,7 @@ Project = Union[
class RepoConfig(pydantic.BaseModel): class RepoConfig(pydantic.BaseModel):
url: str url: str
token_file: Path token_file: Optional[Path] = None
branch: str = 'master' branch: str = 'master'
@functools.cached_property @functools.cached_property
@ -176,16 +176,18 @@ class RepoConfig(pydantic.BaseModel):
return urllib.parse.urlunsplit(urlparts) return urllib.parse.urlunsplit(urlparts)
@functools.cached_property @functools.cached_property
def auth_token(self) -> str: def auth_token(self) -> Optional[str]:
if self.token_file:
return self.token_file.read_text().strip() return self.token_file.read_text().strip()
def get_git_url(self) -> str: def get_git_url(self) -> str:
session = _get_session() session = _get_session()
headers = {}
if token := self.auth_token:
headers['Authorization'] = f'Bearer {token}'
r = session.get( r = session.get(
self.repo_api_url, self.repo_api_url,
headers={ headers=headers,
'Authorization': f'token {self.auth_token}',
},
) )
data = r.json() data = r.json()
if ssh_url := data.get('ssh_url'): if ssh_url := data.get('ssh_url'):