Skip to content
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

[Bugfix] Add github_token verification in resolver utils #7065

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion openhands/resolver/resolve_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ def int_or_none(value: str) -> int | None:
if not token:
raise ValueError('Token is required.')

platform = identify_token(token)
platform = identify_token(token, repo)
if platform == Platform.INVALID:
raise ValueError('Token is invalid.')

Expand Down
17 changes: 16 additions & 1 deletion openhands/resolver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@ class Platform(Enum):
GITLAB = 2


def identify_token(token: str) -> Platform:
def identify_token(token: str, repo: str = None) -> Platform:
"""
Identifies whether a token belongs to GitHub or GitLab.

Parameters:
token (str): The personal access token to check.
repo (str): Repository in format "owner/repo" for GitHub Actions token validation.

Returns:
Platform: "GitHub" if the token is valid for GitHub,
"GitLab" if the token is valid for GitLab,
"Invalid" if the token is not recognized by either.
"""
# Try GitHub Actions token format (Bearer) with repo endpoint if repo is provided
if repo:
github_repo_url = f'https://api.github.com/repos/{repo}'
github_bearer_headers = {'Authorization': f'Bearer {token}', 'Accept': 'application/vnd.github+json'}

try:
github_repo_response = requests.get(github_repo_url, headers=github_bearer_headers, timeout=5)
if github_repo_response.status_code == 200:
return Platform.GITHUB
except requests.RequestException as e:
print(f'Error connecting to GitHub API (repo check): {e}')1

# Try GitHub PAT format (token)
github_url = 'https://api.github.com/user'
github_headers = {'Authorization': f'token {token}'}

Expand All @@ -44,6 +58,7 @@ def identify_token(token: str) -> Platform:
except requests.RequestException as e:
print(f'Error connecting to GitHub API: {e}')

# Try GitLab token
gitlab_url = 'https://gitlab.com/api/v4/user'
gitlab_headers = {'Authorization': f'Bearer {token}'}

Expand Down
Loading