Skip to content

Commit a5625f4

Browse files
authored
Merge pull request #4332 from Rovanion/github-fine-grained-token
add support for using fine grained Github tokens
2 parents 273f443 + 3e94aaf commit a5625f4

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

easybuild/tools/github.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,15 +2250,18 @@ def install_github_token(github_user, silent=False):
22502250
def validate_github_token(token, github_user):
22512251
"""
22522252
Check GitHub token:
2253-
* see if it conforms expectations (only [a-f]+[0-9] characters, length of 40)
2254-
* see if it can be used for authenticated access
2253+
* see if it conforms expectations (classic GitHub token with only [0-9a-f] characters
2254+
and length of 40 starting with 'ghp_', or fine-grained GitHub token with only
2255+
alphanumeric ([a-zA-Z0-9]) characters + '_' and length of 93 starting with 'github_pat_'),
2256+
* see if it can be used for authenticated access.
22552257
"""
22562258
# cfr. https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
22572259
token_regex = re.compile('^ghp_[a-zA-Z0-9]{36}$')
22582260
token_regex_old_format = re.compile('^[0-9a-f]{40}$')
2261+
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-authentication-to-github#githubs-token-formats
2262+
token_regex_fine_grained = re.compile('github_pat_[a-zA-Z0-9_]{82}')
22592263

2260-
# token should be 40 characters long, and only contain characters in [0-9a-f]
2261-
sanity_check = bool(token_regex.match(token))
2264+
sanity_check = bool(token_regex.match(token)) or bool(token_regex_fine_grained.match(token))
22622265
if sanity_check:
22632266
_log.info("Sanity check on token passed")
22642267
else:

test/framework/github.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ def test_validate_github_token(self):
648648
if token_old_format:
649649
self.assertTrue(gh.validate_github_token(token_old_format, GITHUB_TEST_ACCOUNT))
650650

651+
# if a fine-grained token is available, test with that too
652+
finegrained_token = os.getenv('TEST_GITHUB_TOKEN_FINEGRAINED')
653+
if finegrained_token:
654+
self.assertTrue(gh.validate_github_token(finegrained_token, GITHUB_TEST_ACCOUNT))
655+
651656
def test_github_find_easybuild_easyconfig(self):
652657
"""Test for find_easybuild_easyconfig function"""
653658
if self.skip_github_tests:

0 commit comments

Comments
 (0)