From 121c55d8d12a878b12eab00a7cebae2e2fa47ee7 Mon Sep 17 00:00:00 2001 From: Catherine Lee Date: Fri, 31 May 2024 18:54:54 +0000 Subject: [PATCH] Old branch deletion script to also delete old ciflow tags (#127625) Change branch deletion script to also delete left over ciflow tags that the bot doesn't get to, as well as the one created by triggering a workflow on HUD Example run https://github.com/pytorch/pytorch/actions/runs/9322082915/job/25662376463?pr=127625 (didn't actually delete the tag, but lists what tags it would delete) Pull Request resolved: https://github.com/pytorch/pytorch/pull/127625 Approved by: https://github.com/huydhn --- .github/scripts/delete_old_branches.py | 58 ++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/.github/scripts/delete_old_branches.py b/.github/scripts/delete_old_branches.py index 21b86fefa1a..c2676ae09ea 100644 --- a/.github/scripts/delete_old_branches.py +++ b/.github/scripts/delete_old_branches.py @@ -2,6 +2,7 @@ import os import re from datetime import datetime +from functools import lru_cache from pathlib import Path from typing import Any, Callable, Dict, List, Set @@ -187,6 +188,17 @@ def get_recent_prs() -> Dict[str, Any]: return prs_by_branch_base +@lru_cache(maxsize=1) +def get_open_prs() -> List[Dict[str, Any]]: + return paginate_graphql( + GRAPHQL_OPEN_PRS, + {"owner": "pytorch", "repo": "pytorch"}, + lambda data: False, + lambda res: res["data"]["repository"]["pullRequests"]["nodes"], + lambda res: res["data"]["repository"]["pullRequests"]["pageInfo"], + ) + + def get_branches_with_magic_label_or_open_pr() -> Set[str]: pr_infos: List[Dict[str, Any]] = paginate_graphql( GRAPHQL_NO_DELETE_BRANCH_LABEL, @@ -196,15 +208,7 @@ def get_branches_with_magic_label_or_open_pr() -> Set[str]: lambda res: res["data"]["repository"]["label"]["pullRequests"]["pageInfo"], ) - pr_infos.extend( - paginate_graphql( - GRAPHQL_OPEN_PRS, - {"owner": "pytorch", "repo": "pytorch"}, - lambda data: False, - lambda res: res["data"]["repository"]["pullRequests"]["nodes"], - lambda res: res["data"]["repository"]["pullRequests"]["pageInfo"], - ) - ) + pr_infos.extend(get_open_prs()) # Get the most recent PR for each branch base (group gh together) branch_bases = set() @@ -270,5 +274,41 @@ def delete_branches() -> None: delete_branch(git_repo, branch) +def delete_old_ciflow_tags() -> None: + # Deletes ciflow tags if they are associated with a closed PR or a specific + # commit. Lightweight tags don't have information about the date they were + # created, so we can't check how old they are. The script just assumes that + # ciflow tags should be deleted regardless of creation date. + git_repo = GitRepo(str(REPO_ROOT), "origin", debug=True) + + def delete_tag(tag: str) -> None: + print(f"Deleting tag {tag}") + ESTIMATED_TOKENS[0] += 1 + delete_branch(git_repo, f"refs/tags/{tag}") + + tags = git_repo._run_git("tag").splitlines() + open_pr_numbers = [x["number"] for x in get_open_prs()] + + for tag in tags: + try: + if ESTIMATED_TOKENS[0] > 400: + print("Estimated tokens exceeded, exiting") + break + if not tag.startswith("ciflow/"): + continue + re_match_pr = re.match(r"^ciflow\/.*\/(\d{5,6})$", tag) + re_match_sha = re.match(r"^ciflow\/.*\/([0-9a-f]{40})$", tag) + if re_match_pr: + pr_number = int(re_match_pr.group(1)) + if pr_number in open_pr_numbers: + continue + delete_tag(tag) + elif re_match_sha: + delete_tag(tag) + except Exception as e: + print(f"Failed to check tag {tag}: {e}") + + if __name__ == "__main__": delete_branches() + delete_old_ciflow_tags()