branch-autodelete.yml 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Deletes branches that are done, in two ways:
  2. # - delete: when a pull request from a branch of this repository is merged, its branch is deleted. Pull requests from
  3. # forks are ignored. A branch closed without merging stays.
  4. # - merged-into-main: on every push to main, every branch whose commits are all in main and whose tip came into main
  5. # through a merge is deleted. This covers branches merged locally (git merge + git push) without a pull request.
  6. # A branch that points at a commit of main's own line (a new branch with no commits yet, or one merged as a fast
  7. # forward) stays, so a freshly created branch is not deleted before its first commit.
  8. # main and backup/* are never touched, and neither is a branch that is still the base of an open pull request (deleting
  9. # it through the API would close that pull request instead of retargeting it). No commit is ever lost: a branch is only
  10. # deleted when main already contains all of its commits.
  11. name: Delete merged branch
  12. on:
  13. pull_request:
  14. types: [closed]
  15. push:
  16. branches: [main]
  17. permissions:
  18. contents: write
  19. pull-requests: read
  20. jobs:
  21. delete:
  22. if: github.event_name == 'pull_request' && github.event.pull_request.merged == true && github.event.pull_request.head.repo.full_name == github.repository
  23. runs-on: ubuntu-latest
  24. steps:
  25. - name: Delete the merged branch
  26. env:
  27. GH_TOKEN: ${{ github.token }}
  28. BRANCH: ${{ github.event.pull_request.head.ref }}
  29. REPO: ${{ github.repository }}
  30. run: |
  31. # the branch name comes from the pull request: validate it as a plain name before using it anywhere
  32. if ! [[ $BRANCH =~ ^[A-Za-z0-9._/-]+$ ]] || [[ $BRANCH == -* || $BRANCH == *..* ]]; then
  33. echo "::error::Refusing '$BRANCH': not a plain branch name"; exit 1
  34. fi
  35. case "$BRANCH" in
  36. main|backup/*) echo "Keeping the long-lived branch $BRANCH"; exit 0 ;;
  37. esac
  38. open=$(gh pr list -R "$REPO" --base "$BRANCH" --state open --json number --jq 'length')
  39. if [ "$open" != 0 ]; then
  40. echo "Keeping $BRANCH: it is the base of $open open pull request(s)"; exit 0
  41. fi
  42. if ! gh api "repos/$REPO/git/refs/heads/$BRANCH" --silent 2>/dev/null; then
  43. echo "$BRANCH is already gone"; exit 0
  44. fi
  45. echo "Deleting $BRANCH"
  46. gh api -X DELETE "repos/$REPO/git/refs/heads/$BRANCH"
  47. merged-into-main:
  48. if: github.event_name == 'push'
  49. runs-on: ubuntu-latest
  50. # two pushes to main in a row: the second run waits instead of deleting in parallel
  51. concurrency:
  52. group: branch-autodelete-main
  53. cancel-in-progress: false
  54. steps:
  55. # full history of every branch, but no file contents: only the commit graph is needed
  56. - uses: actions/checkout@v5
  57. with:
  58. fetch-depth: 0
  59. filter: blob:none
  60. sparse-checkout: .github
  61. - name: Delete the branches merged into main
  62. env:
  63. GH_TOKEN: ${{ github.token }}
  64. REPO: ${{ github.repository }}
  65. run: |
  66. set -euo pipefail
  67. git rev-list --first-parent origin/main > "$RUNNER_TEMP/main-line"
  68. gh pr list -R "$REPO" --state open --limit 1000 --json baseRefName,headRefName,isCrossRepository \
  69. --jq '.[] | .baseRefName, (select(.isCrossRepository | not) | .headRefName)' | sort -u > "$RUNNER_TEMP/open-pr-branches"
  70. refspecs=()
  71. while read -r branch sha; do
  72. case "$branch" in
  73. HEAD|main|backup/*) continue ;;
  74. esac
  75. if grep -Fxq -- "$branch" "$RUNNER_TEMP/open-pr-branches"; then
  76. echo "Keeping $branch: it belongs to an open pull request"; continue
  77. fi
  78. git merge-base --is-ancestor "$sha" origin/main || continue
  79. if grep -Fxq "$sha" "$RUNNER_TEMP/main-line"; then
  80. echo "Keeping $branch: it points at a commit of main itself (new branch or fast forward)"; continue
  81. fi
  82. echo "Deleting $branch (${sha:0:9}, merged into main)"
  83. # the lease makes the delete fail if someone pushed to the branch since the fetch
  84. refspecs+=("--force-with-lease=refs/heads/$branch:$sha" ":refs/heads/$branch")
  85. done < <(git for-each-ref --format='%(refname:lstrip=3) %(objectname)' refs/remotes/origin)
  86. if [ ${#refspecs[@]} = 0 ]; then
  87. echo "Nothing to delete"; exit 0
  88. fi
  89. # one push per 50 branches (100 arguments) keeps the command line short; a rejected branch (lease or rule) does not stop the rest
  90. failed=0
  91. for ((i = 0; i < ${#refspecs[@]}; i += 100)); do
  92. git push origin "${refspecs[@]:i:100}" || failed=1
  93. done
  94. exit $failed