|
| 1 | +name: pre-commit |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + |
| 5 | +jobs: |
| 6 | + pre-commit: |
| 7 | + runs-on: ubuntu-latest |
| 8 | + steps: |
| 9 | + - name: Set up Docker Buildx |
| 10 | + uses: docker/setup-buildx-action@v3 |
| 11 | + |
| 12 | + - name: Set up Docker Compose |
| 13 | + run: | |
| 14 | + sudo curl -L "https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-$(uname -s)-$(uname -m)" \ |
| 15 | + -o /usr/local/bin/docker-compose |
| 16 | + sudo chmod +x /usr/local/bin/docker-compose |
| 17 | +
|
| 18 | + - name: Get number of PR commits + 1 |
| 19 | + run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV |
| 20 | + |
| 21 | + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master |
| 22 | + with: |
| 23 | + ref: ${{ github.head_ref }} |
| 24 | + fetch_depth: ${{ env.PR_FETCH_DEPTH }} |
| 25 | + |
| 26 | + - name: Undo all PR commits and keep changes in staging area |
| 27 | + run: | |
| 28 | +
|
| 29 | + # some of our pre-commit hooks uses `git diff --name-only --cached` to get the list |
| 30 | + # changed files. This step undos the most recent commits in the checkout, |
| 31 | + # leaving the changes in the staging area for our `git diff ..` to find. |
| 32 | + git reset --soft HEAD~$(( ${{ env.PR_FETCH_DEPTH }} - 1 )) |
| 33 | +
|
| 34 | + - run: | |
| 35 | +
|
| 36 | + # Running pre-commit on the following files: |
| 37 | + target_files=$(git diff --name-only --cached) |
| 38 | + echo "Running pre-commit on the following files:" |
| 39 | + echo "$target_files" |
| 40 | +
|
| 41 | + - uses: actions/setup-python@v3 |
| 42 | + |
| 43 | + - uses: actions/setup-node@v4 |
| 44 | + with: |
| 45 | + node-version: 16 |
| 46 | + cache: 'npm' |
| 47 | + cache-dependency-path: zubhub_frontend/zubhub/package-lock.json |
| 48 | + |
| 49 | + - run: npm ci --prefix ./zubhub_frontend/zubhub/ |
| 50 | + |
| 51 | + - uses: pre-commit/[email protected] |
| 52 | + id: pre_commit |
| 53 | + with: |
| 54 | + # by default pre-commit/action runs `pre-commit run --all-files ...`. |
| 55 | + # Here we use empty extra_args to force it to run `pre-commit run ...` instead. |
| 56 | + # this is important because we want it to only run on the files that have changed |
| 57 | + extra_args: |
| 58 | + continue-on-error: true |
| 59 | + |
| 60 | + - name: Success comment |
| 61 | + if: steps.pre_commit.outcome == 'success' |
| 62 | + uses: unstructuredstudio/zubhub/.github/actions/comment_action@master |
| 63 | + with: |
| 64 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 65 | + issue_number: ${{ github.event.pull_request.number }} |
| 66 | + owner: ${{ github.repository_owner }} |
| 67 | + repo: ${{ github.event.repository.name }} |
| 68 | + message: | |
| 69 | + All pre-commit checks passed ✅✅✅ |
| 70 | +
|
| 71 | + - name: Failure comment |
| 72 | + if: steps.pre_commit.outcome != 'success' |
| 73 | + uses: unstructuredstudio/zubhub/.github/actions/comment_action@master |
| 74 | + with: |
| 75 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 76 | + issue_number: ${{ github.event.pull_request.number }} |
| 77 | + owner: ${{ github.repository_owner }} |
| 78 | + repo: ${{ github.event.repository.name }} |
| 79 | + message: | |
| 80 | + pre-commit checks failed ❌❌❌ |
| 81 | + make sure you have pre-commit set up locally (see the README.md for instructions). |
| 82 | + If you have set up pre-commit locally, you may need to undo this commit and |
| 83 | + run "pre-commit" locally to see what the errors are. |
| 84 | +
|
| 85 | + - name: Failure exit |
| 86 | + if: steps.pre_commit.outcome != 'success' |
| 87 | + run: exit 1 |
0 commit comments