diff --git a/.github/workflows/force_push_check.yaml b/.github/workflows/force_push_check.yaml new file mode 100644 index 000000000..51981b69b --- /dev/null +++ b/.github/workflows/force_push_check.yaml @@ -0,0 +1,49 @@ +name: PR Force Push Check + +on: + pull_request_target: + types: + - synchronize + +permissions: + pull-requests: write + + +jobs: + detect-force-push: + name: Detect Force push + runs-on: ubuntu-latest + if: github.actor != 'dependabot[bot]' &&!github.event.pull_request.draft &&!contains(github.event.pull_request.title, 'WIP') + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Check for force push + id: check_force_push + run: | + # The first push is always empty, so skip it. + BEFORE_SHA=${{ github.event.before }} + [[ -z "${BEFORE_SHA}" ]] && echo "force_push=false" >> $GITHUB_ENV + + AFTER_SHA=${{ github.event.after }} + if ! git merge-base --is-ancestor $BEFORE_SHA $AFTER_SHA 2>/dev/null; then + echo "force_push=true" >> $GITHUB_ENV + else + echo "force_push=false" >> $GITHUB_ENV + fi + + - name: Comment on PR if force push detected + if: env.force_push == 'true' + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const comment = "We’ve detected a force push. Please review the [contribution guidelines](https://github.com/envoyproxy/ai-gateway/blob/main/CONTRIBUTING.md), and avoid force pushing unless you’re addressing a DCO issue after marking your PR as ready for review." + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: comment + });