|
| 1 | +name: Delete Unauthorized Branches |
| 2 | + |
| 3 | +# Due to internal reasons, we cannot disable default write access for the repo. |
| 4 | +# As an alternative, this workflow is used to delete unauthorized branches that are created |
| 5 | +# without following the branch policy. |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - '**' |
| 11 | + |
| 12 | +jobs: |
| 13 | + enforce-branch-policy: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + |
| 16 | + # Run only for push events in the main repo (not forks) |
| 17 | + if: github.event.repository.fork == false && github.event_name == 'push' |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Extract branch name |
| 21 | + id: branch |
| 22 | + run: | |
| 23 | + BRANCH_REF="${GITHUB_REF#refs/heads/}" |
| 24 | + echo "branch=$BRANCH_REF" >> $GITHUB_OUTPUT |
| 25 | +
|
| 26 | + - name: Check if branch is legit |
| 27 | + id: check-branch |
| 28 | + run: | |
| 29 | + BRANCH="${{ steps.branch.outputs.branch }}" |
| 30 | +
|
| 31 | + # Allow release-dev/* and vx.y.z pattern |
| 32 | + if [[ "$BRANCH" =~ ^release-dev\/.* ]] || [[ "$BRANCH" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 33 | + echo "Legit branch: $BRANCH" |
| 34 | + echo "skip=true" >> $GITHUB_OUTPUT |
| 35 | + else |
| 36 | + echo "Custom branch: $BRANCH" |
| 37 | + echo "skip=false" >> $GITHUB_OUTPUT |
| 38 | + fi |
| 39 | + shell: bash |
| 40 | + |
| 41 | + - name: Delete unauthorized branch |
| 42 | + if: steps.check-branch.outputs.skip == 'false' |
| 43 | + run: | |
| 44 | + BRANCH="${{ steps.branch.outputs.branch }}" |
| 45 | + echo "Deleting unauthorized branch: $BRANCH" |
| 46 | + curl -s -X DELETE \ |
| 47 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 48 | + -H "Accept: application/vnd.github+json" \ |
| 49 | + https://api.github.com/repos/${{ github.repository }}/git/refs/heads/$BRANCH |
0 commit comments