|
| 1 | +name: Lint |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + lint: |
| 15 | + runs-on: depot-ubuntu-24.04-16 |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@v3 |
| 20 | + with: |
| 21 | + persist-credentials: false |
| 22 | + # Ensure we are on an actual branch (not a detached HEAD) |
| 23 | + ref: ${{ github.head_ref || github.ref }} |
| 24 | + |
| 25 | + - uses: actions/setup-node@v4 |
| 26 | + with: |
| 27 | + node-version: '22' |
| 28 | + |
| 29 | + # Caching node_modules is faster than using setup-node's cache: 'npm' (https://www.voorhoede.nl/en/blog/super-fast-npm-install-on-github-actions/) |
| 30 | + - id: cache |
| 31 | + name: Cache dependencies |
| 32 | + uses: actions/cache@v4 |
| 33 | + with: |
| 34 | + path: ./node_modules |
| 35 | + key: modules-v2-${{ hashFiles('package-lock.json') }} |
| 36 | + |
| 37 | + - name: Install dependencies |
| 38 | + if: steps.cache.outputs.cache-hit != 'true' |
| 39 | + run: npm ci --ignore-scripts |
| 40 | + |
| 41 | + - name: Run linter |
| 42 | + id: lint |
| 43 | + continue-on-error: true |
| 44 | + run: npm run lint |
| 45 | + |
| 46 | + - name: Auto-fix with linter |
| 47 | + if: steps.lint.outcome == 'failure' |
| 48 | + run: npm run lint:fix |
| 49 | + |
| 50 | + - name: Show diff, commit and push fixes (non-master/main only) |
| 51 | + if: | |
| 52 | + steps.lint.outcome == 'failure' && |
| 53 | + github.ref != 'refs/heads/master' && |
| 54 | + github.ref != 'refs/heads/main' && |
| 55 | + (github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)) |
| 56 | + run: | |
| 57 | + BRANCH="${{ github.head_ref || github.ref_name }}" |
| 58 | + echo "Preparing to push fixes to $BRANCH" |
| 59 | +
|
| 60 | + # Ensure we're on a branch, not detached HEAD |
| 61 | + git checkout -B "$BRANCH" |
| 62 | +
|
| 63 | + git config user.name "codecrafters-bot" |
| 64 | + git config user.email "[email protected]" |
| 65 | +
|
| 66 | + # Only commit if there are changes |
| 67 | + if ! git diff --quiet; then |
| 68 | + echo "Auto-fix diff (before commit):" |
| 69 | + git --no-pager diff |
| 70 | + git add -A |
| 71 | + git commit -m "chore(lint): auto-fix lint offenses" |
| 72 | + git remote set-url origin "https://x-access-token:${{ secrets.CODECRAFTERS_BOT_TOKEN_FOR_LINT_FIXES }}@github.com/${{ github.repository }}.git" |
| 73 | + git push origin HEAD:"$BRANCH" |
| 74 | + else |
| 75 | + echo "No changes to commit" |
| 76 | + fi |
| 77 | +
|
| 78 | + - name: Fail if linting failed |
| 79 | + if: steps.lint.outcome == 'failure' |
| 80 | + run: | |
| 81 | + echo "Linting failed. Auto-fixes (if any) have been pushed back to the branch. Failing the job to keep visibility." |
| 82 | + exit 1 |
| 83 | +
|
0 commit comments