Skip to content

Commit

Permalink
feat: add clear invalid branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jiumos committed Sep 13, 2024
1 parent ecaf35d commit ea8b885
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/clear-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Clear Branch

on:
schedule:
- cron: '0 8 * * *' # Runs every day at 0:00 Beijing Time (8:00 UTC)
workflow_dispatch:
inputs:
ref:
description: "Why trigger?"
required: true
type: string
jobs:
clear_branch:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Configure Git
run: |
git config user.name "$GITHUB_ACTOR"
git config user.email "[email protected]"
- name: List all branches
run: |
git fetch --prune
current_branch=$(git rev-parse --abbrev-ref HEAD)
protected_branches=$(gh api /repos/${{ github.repository }}/branches --jq '.[] | select(.protected == true) | .name' | tr '\n' ' ')
for branch in $(git branch -r | sed 's/origin\///'|grep -v origin); do
if [[ " ${protected_branches[@]} " =~ " ${branch} " ]]; then
echo "Skipping protected branch: $branch"
continue
fi
if [[ "$branch" == "$current_branch" ]]; then
echo "Skipping current branch: $branch"
continue
fi
# Skip branches with submitted PR
last_commit_date=$(git log -1 --format=%ci origin/$branch)
if [[ $(date -d "$last_commit_date" +%s) -lt $(date -d "1 month ago" +%s) ]]; then
if ! gh pr list --head $branch --repo https://github.com//${{ github.repository }} | grep $branch; then
echo "Deleting branch: $branch"
git push origin --delete $branch
fi
fi
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit ea8b885

Please sign in to comment.