ClickHouse/tests/ci/get_robot_token.py

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

41 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import logging
import boto3 # type: ignore
from github import Github # type: ignore
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
token = {"login": "", "value": "", "rest": 0}
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)
if token["rest"] < rest:
2022-11-22 12:37:08 +00:00
token = {"user": user, "value": value, "rest": rest}
2022-11-22 12:37:08 +00:00
assert token["value"]
logging.info(
2022-11-22 12:37:08 +00:00
"User %s with %s remaining requests is used", token["user"].login, token["rest"]
)
return token["value"]