4
4
import os
5
5
import re
6
6
from enum import Enum
7
- from typing import Callable
7
+ from typing import Callable , Optional
8
8
9
9
import pandas as pd
10
10
import requests
@@ -22,18 +22,37 @@ class Platform(Enum):
22
22
GITLAB = 2
23
23
24
24
25
- def identify_token (token : str ) -> Platform :
25
+ def identify_token (token : str , repo : Optional [ str ] = None ) -> Platform :
26
26
"""
27
27
Identifies whether a token belongs to GitHub or GitLab.
28
28
29
29
Parameters:
30
30
token (str): The personal access token to check.
31
+ repo (str): Repository in format "owner/repo" for GitHub Actions token validation.
31
32
32
33
Returns:
33
34
Platform: "GitHub" if the token is valid for GitHub,
34
35
"GitLab" if the token is valid for GitLab,
35
36
"Invalid" if the token is not recognized by either.
36
37
"""
38
+ # Try GitHub Actions token format (Bearer) with repo endpoint if repo is provided
39
+ if repo :
40
+ github_repo_url = f'https://api.github.com/repos/{ repo } '
41
+ github_bearer_headers = {
42
+ 'Authorization' : f'Bearer { token } ' ,
43
+ 'Accept' : 'application/vnd.github+json' ,
44
+ }
45
+
46
+ try :
47
+ github_repo_response = requests .get (
48
+ github_repo_url , headers = github_bearer_headers , timeout = 5
49
+ )
50
+ if github_repo_response .status_code == 200 :
51
+ return Platform .GITHUB
52
+ except requests .RequestException as e :
53
+ print (f'Error connecting to GitHub API (repo check): { e } ' )
54
+
55
+ # Try GitHub PAT format (token)
37
56
github_url = 'https://api.github.com/user'
38
57
github_headers = {'Authorization' : f'token { token } ' }
39
58
@@ -44,6 +63,7 @@ def identify_token(token: str) -> Platform:
44
63
except requests .RequestException as e :
45
64
print (f'Error connecting to GitHub API: { e } ' )
46
65
66
+ # Try GitLab token
47
67
gitlab_url = 'https://gitlab.com/api/v4/user'
48
68
gitlab_headers = {'Authorization' : f'Bearer { token } ' }
49
69
0 commit comments