Skip to content

Commit c519bbf

Browse files
chore(utils): relax GitHub PAT validation (#380)
* Drop `InvalidGitHubTokenError` * `validate_github_token` now emits a `UserWarning` instead of raising * Update tests to: * assert no warnings for valid tokens (via `recwarn`) * expect `UserWarning` for malformed tokens * Reduces false-positives and is future-proof if GitHub introduces new token formats Closes #380
1 parent dead917 commit c519bbf

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

src/gitingest/utils/exceptions.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,3 @@ class InvalidNotebookError(Exception):
3636

3737
def __init__(self, message: str) -> None:
3838
super().__init__(message)
39-
40-
41-
class InvalidGitHubTokenError(ValueError):
42-
"""Exception raised when a GitHub Personal Access Token is malformed."""
43-
44-
def __init__(self) -> None:
45-
msg = (
46-
"Invalid GitHub token format. To generate a token, go to "
47-
"https://github.com/settings/tokens/new?description=gitingest&scopes=repo."
48-
)
49-
super().__init__(msg)

src/gitingest/utils/git_utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import base64
77
import os
88
import re
9+
import warnings
910
from typing import Final
1011
from urllib.parse import urlparse
1112

@@ -19,7 +20,6 @@
1920
)
2021

2122
from gitingest.utils.compat_func import removesuffix
22-
from gitingest.utils.exceptions import InvalidGitHubTokenError
2323

2424
# GitHub Personal-Access tokens (classic + fine-grained).
2525
# - ghp_ / gho_ / ghu_ / ghs_ / ghr_ → 36 alphanumerics
@@ -319,11 +319,11 @@ def validate_github_token(token: str) -> None:
319319
token : str
320320
GitHub personal access token (PAT) for accessing private repositories.
321321
322-
Raises
323-
------
324-
InvalidGitHubTokenError
325-
If the token format is invalid.
326-
327322
"""
328323
if not re.fullmatch(_GITHUB_PAT_PATTERN, token):
329-
raise InvalidGitHubTokenError
324+
warnings.warn(
325+
"Invalid GitHub token format. To generate a token, go to "
326+
"https://github.com/settings/tokens/new?description=gitingest&scopes=repo.",
327+
UserWarning,
328+
stacklevel=2,
329+
)

tests/test_git_utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import pytest
1313

14-
from gitingest.utils.exceptions import InvalidGitHubTokenError
1514
from gitingest.utils.git_utils import (
1615
create_git_auth_header,
1716
create_git_command,
@@ -37,10 +36,10 @@
3736
"gho_" + "E" * 36,
3837
],
3938
)
40-
def test_validate_github_token_valid(token: str) -> None:
41-
"""validate_github_token should accept properly-formatted tokens."""
42-
# Should not raise any exception
39+
def test_validate_github_token_valid(token: str, recwarn: pytest.WarningsRecorder) -> None:
40+
"""``validate_github_token`` should silently accept well-formed tokens."""
4341
validate_github_token(token)
42+
assert not recwarn, "validate_github_token should not warn on valid tokens"
4443

4544

4645
@pytest.mark.parametrize(
@@ -54,9 +53,10 @@ def test_validate_github_token_valid(token: str) -> None:
5453
"", # Empty string
5554
],
5655
)
57-
def test_validate_github_token_invalid(token: str) -> None:
58-
"""Test that ``validate_github_token`` raises ``InvalidGitHubTokenError`` on malformed tokens."""
59-
with pytest.raises(InvalidGitHubTokenError):
56+
def test_validate_github_token_invalid_warns(token: str) -> None:
57+
"""Test that malformed tokens trigger a UserWarning carrying the helper message."""
58+
warn_msg = "Invalid GitHub token format"
59+
with pytest.warns(UserWarning, match=warn_msg):
6060
validate_github_token(token)
6161

6262

0 commit comments

Comments
 (0)