forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathget_robot_token.py
More file actions
114 lines (97 loc) · 3.48 KB
/
Copy pathget_robot_token.py
File metadata and controls
114 lines (97 loc) · 3.48 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
import logging
import random
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Union
import boto3 # type: ignore
from github import Github
from github.AuthenticatedUser import AuthenticatedUser
from github.GithubException import BadCredentialsException
from github.NamedUser import NamedUser
from env_helper import ROBOT_TOKEN
@dataclass
class Token:
user: Union[AuthenticatedUser, NamedUser]
value: str
rest: int
SAFE_REQUESTS_LIMIT = 1000
def get_parameter_from_ssm(
name: str, decrypt: bool = True, client: Optional[Any] = None
) -> str:
if not client:
client = boto3.client("ssm", region_name="us-east-1")
return client.get_parameter( # type:ignore
Name=name, WithDecryption=decrypt
)[
"Parameter"
]["Value"]
def get_parameters_from_ssm(
names: List[str], decrypt: bool = True, client: Optional[Any] = None
) -> Dict[str, str]:
if not client:
client = boto3.client("ssm", region_name="us-east-1")
names = list(set(names))
results = {} # type: Dict[str,str]
i = 0
while (i) * 10 < len(names):
# the get_parameters returns up to 10 values, so the call is split by 10
results.update(
**{
p["Name"]: p["Value"]
for p in client.get_parameters(
Names=names[i * 10 : (i + 1) * 10], WithDecryption=decrypt
)["Parameters"]
}
)
i += 1
return results
# NOTE(Arthur Passos): Original CI code uses the "_original" version of this method. Each robot token is rate limited
# and the original implementation selects the "best one". To make it simpler and iterate faster,
# we are using only one robot and keeping the method signature. In the future we might reconsider
# having multiple robot tokens
def get_best_robot_token():
return ROBOT_TOKEN
def get_best_robot_token_original(tokens_path: str = "/github-tokens") -> str:
global ROBOT_TOKEN
if ROBOT_TOKEN is not None:
return ROBOT_TOKEN.value
client = boto3.client("ssm", region_name="us-east-1")
tokens = {
p["Name"]: p["Value"]
for p in client.get_parameters_by_path(Path=tokens_path, WithDecryption=True)[
"Parameters"
]
}
assert tokens
token_items = list(tokens.items())
random.shuffle(token_items)
best_token: Optional[Token] = None
for name, value in token_items:
gh = Github(value, per_page=100)
try:
# Do not spend additional request to API by accessing user.login unless
# the token is chosen by the remaining requests number
user = gh.get_user()
rest, _ = gh.rate_limiting
except BadCredentialsException:
logging.error(
"The token %(name)s has expired, please update it\n"
"::error::Token %(name)s has expired, it must be updated",
{"name": name},
)
continue
logging.info("Get token with %s remaining requests", rest)
if best_token is None:
best_token = Token(user, value, rest)
elif best_token.rest < rest:
best_token = Token(user, value, rest)
if best_token.rest > SAFE_REQUESTS_LIMIT:
break
assert best_token
ROBOT_TOKEN = best_token
logging.info(
"User %s with %s remaining requests is used",
ROBOT_TOKEN.user.login,
ROBOT_TOKEN.rest,
)
return ROBOT_TOKEN.value