-
Notifications
You must be signed in to change notification settings - Fork 17
/
gitTokenHelper.py
53 lines (47 loc) · 1.86 KB
/
gitTokenHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
import datetime
from github import Github, GithubException
class GithubPersonalAccessTokenHelper():
def __init__(self, pats):
if not isinstance(pats, list):
raise Exception("PATs must be an array")
self.pats = []
self._initialize_pats(pats)
self.last_sent_token = 'fff'
def _initialize_pats(self, pats):
for (_, pat) in enumerate(pats):
try:
gh = Github(pat)
rate_limit = gh.get_rate_limit()
self.pats.append(pat)
except GithubException as e:
# Probably a bad access token
print("Error while querying for personal access token")
print(e)
continue
# Need atleast one valid personal access token
assert len(self.pats) > 0
def get_access_token(self):
min_sleep_time_secs = None
for (_, token) in enumerate(self.pats):
gh = Github(token)
rate_limit = gh.get_rate_limit()
if rate_limit.core.remaining > 0 and self.last_sent_token != token:
self.last_sent_token = token
return {
'token': token
}
rate_limit_reset_time = rate_limit.core.reset
time_delta = abs(rate_limit_reset_time -
datetime.datetime.utcnow())
if min_sleep_time_secs is None:
min_sleep_time_secs = time_delta.total_seconds()
continue
if min_sleep_time_secs < time_delta.total_seconds():
min_sleep_time_secs = time_delta.total_seconds()
print("All access tokens have been rate limited")
print("Min sleep time: ", min_sleep_time_secs)
return {
'token': None,
'sleep_time_secs': min_sleep_time_secs
}