ClickHouse/tests/ci/get_robot_token.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import logging
2022-11-10 16:11:23 +00:00
from dataclasses import dataclass
import boto3 # type: ignore
2022-11-10 16:11:23 +00:00
from github import Github
from github.AuthenticatedUser import AuthenticatedUser
@dataclass
class Token:
user: AuthenticatedUser
value: str
rest: int
2021-10-20 11:48:27 +00:00
def get_parameter_from_ssm(name, decrypt=True, client=None):
if not client:
client = boto3.client("ssm", region_name="us-east-1")
return client.get_parameter(Name=name, WithDecryption=decrypt)["Parameter"]["Value"]
2021-10-20 11:48:27 +00:00
def get_best_robot_token(token_prefix_env_name="github_robot_token_"):
client = boto3.client("ssm", region_name="us-east-1")
parameters = client.describe_parameters(
ParameterFilters=[
{"Key": "Name", "Option": "BeginsWith", "Values": [token_prefix_env_name]}
]
)["Parameters"]
assert parameters
2022-11-10 16:11:23 +00:00
token = None
for token_name in [p["Name"] for p in parameters]:
value = get_parameter_from_ssm(token_name, True, client)
gh = Github(value, per_page=100)
2022-11-22 12:37:08 +00:00
# Do not spend additional request to API by accessin user.login unless
# the token is chosen by the remaining requests number
user = gh.get_user()
rest, _ = gh.rate_limiting
2022-11-22 12:37:08 +00:00
logging.info("Get token with %s remaining requests", rest)
2022-11-10 16:11:23 +00:00
if token is None:
token = Token(user, value, rest)
continue
if token.rest < rest:
token.user, token.value, token.rest = user, value, rest
2022-11-10 16:11:23 +00:00
assert token
logging.info(
2022-11-10 16:11:23 +00:00
"User %s with %s remaining requests is used", token.user.login, token.rest
)
2022-11-10 16:11:23 +00:00
return token.value