Skip to content

chore(utils): relax GitHub PAT validation (#380) #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/gitingest/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,3 @@ class InvalidNotebookError(Exception):

def __init__(self, message: str) -> None:
super().__init__(message)


class InvalidGitHubTokenError(ValueError):
"""Exception raised when a GitHub Personal Access Token is malformed."""

def __init__(self) -> None:
msg = (
"Invalid GitHub token format. To generate a token, go to "
"https://github.com/settings/tokens/new?description=gitingest&scopes=repo."
)
super().__init__(msg)
14 changes: 7 additions & 7 deletions src/gitingest/utils/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import base64
import os
import re
import warnings
from typing import Final
from urllib.parse import urlparse

Expand All @@ -19,7 +20,6 @@
)

from gitingest.utils.compat_func import removesuffix
from gitingest.utils.exceptions import InvalidGitHubTokenError

# GitHub Personal-Access tokens (classic + fine-grained).
# - ghp_ / gho_ / ghu_ / ghs_ / ghr_ → 36 alphanumerics
Expand Down Expand Up @@ -319,11 +319,11 @@ def validate_github_token(token: str) -> None:
token : str
GitHub personal access token (PAT) for accessing private repositories.

Raises
------
InvalidGitHubTokenError
If the token format is invalid.

"""
if not re.fullmatch(_GITHUB_PAT_PATTERN, token):
raise InvalidGitHubTokenError
warnings.warn(
Copy link
Contributor

@ix-56h ix-56h Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to validate the token with pattern matching and the warning is useless here i guess.
When the token is invalid (malformed or just invalid) we correctly handle the error:
image

I don't see why we validate and log the validation result here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the validation is informative. Just a little message saying "this does not look like a proper token, tokens usually start with gh_ blablabla" because yeah, believe me, i've seen too many people struggle with access tokens and just put their plain password or something

"Invalid GitHub token format. To generate a token, go to "
"https://github.com/settings/tokens/new?description=gitingest&scopes=repo.",
UserWarning,
stacklevel=2,
)
14 changes: 7 additions & 7 deletions tests/test_git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import pytest

from gitingest.utils.exceptions import InvalidGitHubTokenError
from gitingest.utils.git_utils import (
create_git_auth_header,
create_git_command,
Expand All @@ -37,10 +36,10 @@
"gho_" + "E" * 36,
],
)
def test_validate_github_token_valid(token: str) -> None:
"""validate_github_token should accept properly-formatted tokens."""
# Should not raise any exception
def test_validate_github_token_valid(token: str, recwarn: pytest.WarningsRecorder) -> None:
"""``validate_github_token`` should silently accept well-formed tokens."""
validate_github_token(token)
assert not recwarn, "validate_github_token should not warn on valid tokens"


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


Expand Down
Loading