diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2022-12-30 00:56:27 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-30 00:56:27 -0500 |
commit | 21a21b86c0bbfd02ae21e0602a02d1c30cb264fb (patch) | |
tree | 60e5eedc45e7bd943958e05e0caca4a10feb0b6d /scripts | |
parent | 32da5f39bb2ee74543bce607249df15885d3cf47 (diff) |
ci: clean all workflow caches to script (#936)
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/clear_cache.py | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/scripts/clear_cache.py b/scripts/clear_cache.py index c2af1359..ac1c99b1 100644 --- a/scripts/clear_cache.py +++ b/scripts/clear_cache.py @@ -33,15 +33,32 @@ def main(): env = os.environ key = env["GITHUB_TOKEN"] - pr_id = int(args[1]) - ref = "refs/pull/{}/merge".format(pr_id) - - print("Clearing any caches generated by PR {}".format(pr_id)) - with urlopen(cache_list_request(key)) as response: - response = json.load(response) - caches = response["actions_caches"] - for cache in caches: - if cache["ref"] == ref: + if args[1].isnumeric(): + pr_id = int(args[1]) + ref = "refs/pull/{}/merge".format(pr_id) + + print("Clearing any caches generated by PR {}".format(pr_id)) + with urlopen(cache_list_request(key)) as response: + response = json.load(response) + caches = response["actions_caches"] + for cache in caches: + if cache["ref"] == ref: + id = cache["id"] + try: + print("Deleting ID {}...".format(id)) + urlopen(delete_cache_request(key, id)) + except HTTPError as e: + print("HTTPError with delete, error code {}.".format(e.code)) + except URLError as _: + print("URLError with delete.") + else: + print("Successfully deleted cache ID {}!".format(id)) + elif args[1] == "main" or args[1] == "master" or args[1] == "all": + print("Clearing all caches.") + with urlopen(cache_list_request(key)) as response: + response = json.load(response) + caches = response["actions_caches"] + for cache in caches: id = cache["id"] try: print("Deleting ID {}...".format(id)) @@ -52,6 +69,8 @@ def main(): print("URLError with delete.") else: print("Successfully deleted cache ID {}!".format(id)) + else: + print(f"Skipping, given argument {args[1]}.") if __name__ == "__main__": |