Skip to content

Commit

Permalink
Merge pull request #43493 from ClickHouse/backport/22.9/43467
Browse files Browse the repository at this point in the history
Backport #43467 to 22.9: Use all parameters with prefixes from ssm
  • Loading branch information
tavplubix committed Nov 22, 2022
2 parents 19fac52 + 2f9fe65 commit 7dcd21d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions tests/ci/get_robot_token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python3
import logging

import boto3 # type: ignore
from github import Github # type: ignore

Expand All @@ -9,14 +11,30 @@ def get_parameter_from_ssm(name, decrypt=True, client=None):
return client.get_parameter(Name=name, WithDecryption=decrypt)["Parameter"]["Value"]


def get_best_robot_token(token_prefix_env_name="github_robot_token_", total_tokens=4):
def get_best_robot_token(token_prefix_env_name="github_robot_token_"):
client = boto3.client("ssm", region_name="us-east-1")
tokens = {}
for i in range(1, total_tokens + 1):
token_name = token_prefix_env_name + str(i)
token = get_parameter_from_ssm(token_name, True, client)
gh = Github(token, per_page=100)
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)
# 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
tokens[token] = rest
logging.info("Get token with %s remaining requests", rest)
if token["rest"] < rest:
token = {"user": user, "value": value, "rest": rest}

assert token["value"]
logging.info(
"User %s with %s remaining requests is used", token["user"].login, token["rest"]
)

return max(tokens.items(), key=lambda x: x[1])[0]
return token["value"]

0 comments on commit 7dcd21d

Please sign in to comment.