2021-10-20 10:31:48 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-12-22 10:38:27 +00:00
|
|
|
import boto3 # type: ignore
|
|
|
|
from github import Github # type: ignore
|
2021-10-20 10:31:48 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2021-10-20 11:48:27 +00:00
|
|
|
def get_parameter_from_ssm(name, decrypt=True, client=None):
|
|
|
|
if not client:
|
2022-03-22 16:39:58 +00:00
|
|
|
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_", total_tokens=4):
|
2022-03-22 16:39:58 +00:00
|
|
|
client = boto3.client("ssm", region_name="us-east-1")
|
2021-10-20 10:31:48 +00:00
|
|
|
tokens = {}
|
2021-10-20 11:48:27 +00:00
|
|
|
for i in range(1, total_tokens + 1):
|
2021-10-20 10:31:48 +00:00
|
|
|
token_name = token_prefix_env_name + str(i)
|
2021-10-20 11:48:27 +00:00
|
|
|
token = get_parameter_from_ssm(token_name, True, client)
|
2022-07-30 05:07:22 +00:00
|
|
|
gh = Github(token, per_page=100)
|
2021-10-20 10:31:48 +00:00
|
|
|
rest, _ = gh.rate_limiting
|
|
|
|
tokens[token] = rest
|
|
|
|
|
|
|
|
return max(tokens.items(), key=lambda x: x[1])[0]
|