[Bugfix] Add github_token verification in resolver utils (#7065)

This commit is contained in:
tawago 2025-03-03 23:59:16 +09:00 committed by GitHub
parent 285010b48f
commit 6d75647c40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View File

@ -651,7 +651,7 @@ def main() -> 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.')

View File

@ -4,7 +4,7 @@ import multiprocessing as mp
import os
import re
from enum import Enum
from typing import Callable
from typing import Callable, Optional
import pandas as pd
import requests
@ -22,18 +22,37 @@ class Platform(Enum):
GITLAB = 2
def identify_token(token: str) -> Platform:
def identify_token(token: str, repo: Optional[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}')
# Try GitHub PAT format (token)
github_url = 'https://api.github.com/user'
github_headers = {'Authorization': f'token {token}'}
@ -44,6 +63,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}'}