From dbb0faf0833b6f72706e13e5a4d275a9c6fd614e Mon Sep 17 00:00:00 2001 From: moleyi Date: Fri, 13 Sep 2024 08:17:49 +0000 Subject: [PATCH] test ci --- .github/workflows/clear-branch.yml | 36 +++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/.github/workflows/clear-branch.yml b/.github/workflows/clear-branch.yml index 0d24c93..b738f37 100644 --- a/.github/workflows/clear-branch.yml +++ b/.github/workflows/clear-branch.yml @@ -2,7 +2,8 @@ name: Clear Branch on: push: - - ci-test + branches: + - ci-test workflow_dispatch: inputs: ref: @@ -13,22 +14,41 @@ jobs: clear_branch: runs-on: ubuntu-latest steps: - - name: Checkout the repository + - name: Checkout uses: actions/checkout@v3 + with: + ref: 'ci-test' - - name: Authenticate GitHub CLI + - name: Configure Git run: | - echo "$GITHUB_TOKEN" | gh auth login --with-token + git config user.name "$GITHUB_ACTOR" + git config user.email "$GITHUB_ACTOR@users.noreply.github.com" + + - name: Get protected branches + id: get_protected_branches + run: | + # Get the list of protected branches + protected_branches=$(gh api /repos/${{ github.repository }}/branches --jq '.[] | select(.protected == true) | .name' | tr '\n' ' ') + echo "123 ${protected_branches}" + echo "PROTECTED_BRANCHES=${protected_branches}" >> $GITHUB_ENV env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: List all branches run: | git fetch --prune - for branch in $(git branch -r | grep -v 'HEAD\|main\|master\|ci-test' | sed 's/origin\///'); do + echo "Protected branches are: ${{ env.PROTECTED_BRANCHES }}" + # Convert protected branches string to an array + # IFS=' ' read -r -a protected_branches <<< "${{ env.PROTECTED_BRANCHES }}" + echo ${protected_branches[@]} + 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 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/jiumos/draw/ | grep $branch; then + if [[ $(date -d "$last_commit_date" +%s) -lt $(date -d "10 minutes 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