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
|
|
|
|
2021-10-20 11:48:27 +00:00
|
|
|
def get_parameter_from_ssm(name, decrypt=True, client=None):
|
|
|
|
if not client:
|
2021-10-20 11:52:03 +00:00
|
|
|
client = boto3.client('ssm', region_name='us-east-1')
|
2021-10-20 11:48:27 +00:00
|
|
|
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):
|
2021-10-20 11:52:03 +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)
|
2021-10-20 10:31:48 +00:00
|
|
|
gh = Github(token)
|
|
|
|
rest, _ = gh.rate_limiting
|
|
|
|
tokens[token] = rest
|
|
|
|
|
|
|
|
return max(tokens.items(), key=lambda x: x[1])[0]
|