diff options
| author | Franck Cuny <franck@fcuny.net> | 2022-10-31 11:29:11 -0700 |
|---|---|---|
| committer | Franck Cuny <franck@fcuny.net> | 2022-10-31 11:29:11 -0700 |
| commit | 4dd250dc04e8c44fa91627fb051a9670b7b01e63 (patch) | |
| tree | 03ef67d6ff1b1f10096bf2b58795214fc5e17819 /tools/gha-billing/gha-billing.py | |
| parent | ref(home/python): install a few more packages (diff) | |
| download | infra-4dd250dc04e8c44fa91627fb051a9670b7b01e63.tar.gz | |
feat(tools/gha-billing): a CLI to report minutes left/used on GHA
Diffstat (limited to 'tools/gha-billing/gha-billing.py')
| -rwxr-xr-x | tools/gha-billing/gha-billing.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tools/gha-billing/gha-billing.py b/tools/gha-billing/gha-billing.py new file mode 100755 index 0000000..c9c09ba --- /dev/null +++ b/tools/gha-billing/gha-billing.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +import argparse +import os +import sys + +import requests + +API_URL = "https://api.github.com" + + +def main(api_token: str, user: str) -> None: + s = requests.Session() + s.headers.update({"Authorization": f"token {api_token}"}) + s.headers.update({"Accept": "application/vnd.github.v3+json"}) + + res = s.get(f"{API_URL}/users/{user}/settings/billing/actions", timeout=5) + res.raise_for_status() + + billing = res.json() + + time_remaining = billing["included_minutes"] - billing["total_minutes_used"] + print( + f"this cycle, {billing['total_minutes_used']} minutes have been used, and {time_remaining} minutes are remaining" + ) + + +if __name__ == "__main__": + argp = argparse.ArgumentParser() + argp.add_argument("-t", "--token-file", nargs=1, type=argparse.FileType("r")) + argp.add_argument("-u", "--user", type=str, default="fcuny") + args = argp.parse_args() + + if args.token_file: + api_token = args.token_file[0].readline().strip() + else: + print("Must pass token file with -t/--token_file", file=sys.stderr) + sys.exit(os.EX_USAGE) + + main(api_token, args.user) |
