name: Check for Undesirable Code on: pull_request: types: [edited, opened, synchronize, reopened] permissions: contents: read jobs: check_undesirable_code: if: "!contains(github.event.pull_request.labels.*.name, 'ci:allow-undesirable')" runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false fetch-depth: 0 # Fetch all history for comparison sparse-checkout: true ref: ${{ github.event.pull_request.head.ref }} repository: ${{ github.event.pull_request.head.repo.full_name }} - name: Check for undesirable code run: | #!/bin/bash set -e BASE_BRANCH=${{ github.event.pull_request.base.ref }} echo "Base branch is: $BASE_BRANCH" BASE_BRANCH_REF=${{ github.event.pull_request.base.sha }} echo "Base branch ref for this PR is: $BASE_BRANCH_REF" echo "### Calculating diff for '*.spec.ts' files..." git diff $BASE_BRANCH_REF -- '*.spec.ts' > diff_spec_ts.txt echo "### Counting additions and deletions of snapshots..." ADDED=$(grep '^+' diff_spec_ts.txt | grep -v '^+++' | grep -c -E '\.toMatch(Snapshot|InlineSnapshot)\(' || true) DELETED=$(grep '^-' diff_spec_ts.txt | grep -v '^---' | grep -c -E '\.toMatch(Snapshot|InlineSnapshot)\(' || true) if [ "$ADDED" -gt "$DELETED" ]; then echo "❌ Error: Snapshots have been added in this PR. Use toMatch instead." exit 1 else echo "✅ Snapshot code check passed." fi echo "### Calculating file changes in '__fixtures__' directories..." git diff --name-status $BASE_BRANCH_REF > diff_name_status.txt echo "### Processing added files in '__fixtures__' directories..." ADDED_FIXTURES=$(grep '^A' diff_name_status.txt | awk '{print $2}' | grep '/__fixtures__/' || true) NUM_ADDED_FIXTURES=$(echo "$ADDED_FIXTURES" | grep -c . || echo 0) echo "Added files in '__fixtures__':" echo "$ADDED_FIXTURES" echo "Total number of added files in '__fixtures__': $NUM_ADDED_FIXTURES" echo "### Processing deleted files in '__fixtures__' directories..." DELETED_FIXTURES=$(grep '^D' diff_name_status.txt | awk '{print $2}' | grep '/__fixtures__/' || true) NUM_DELETED_FIXTURES=$(echo "$DELETED_FIXTURES" | grep -c . || echo 0) echo "Deleted files in '__fixtures__':" echo "$DELETED_FIXTURES" echo "Total number of deleted files in '__fixtures__': $NUM_DELETED_FIXTURES" echo "### Processing renamed files involving '__fixtures__' directories..." RENAME_ENTRIES=$(grep '^R' diff_name_status.txt || true) echo "$RENAME_ENTRIES" while read -r line; do if [ -z "$line" ]; then continue fi OLD_PATH=$(echo "$line" | awk '{print $2}') NEW_PATH=$(echo "$line" | awk '{print $3}') echo "Processing rename: $OLD_PATH -> $NEW_PATH" OLD_IN_FIXTURES=0 NEW_IN_FIXTURES=0 if echo "$OLD_PATH" | grep -q '/__fixtures__/'; then OLD_IN_FIXTURES=1 fi if echo "$NEW_PATH" | grep -q '/__fixtures__/'; then NEW_IN_FIXTURES=1 fi if [ "$OLD_IN_FIXTURES" -eq 1 ] && [ "$NEW_IN_FIXTURES" -eq 0 ]; then NUM_DELETED_FIXTURES=$((NUM_DELETED_FIXTURES + 1)) echo "File moved out of '__fixtures__': $OLD_PATH -> $NEW_PATH" elif [ "$OLD_IN_FIXTURES" -eq 0 ] && [ "$NEW_IN_FIXTURES" -eq 1 ]; then NUM_ADDED_FIXTURES=$((NUM_ADDED_FIXTURES + 1)) echo "File moved into '__fixtures__': $OLD_PATH -> $NEW_PATH" else echo "File renamed within the same directory: $OLD_PATH -> $NEW_PATH" fi done <<< "$RENAME_ENTRIES" echo "Updated total number of added files in '__fixtures__': $NUM_ADDED_FIXTURES" echo "Updated total number of deleted files in '__fixtures__': $NUM_DELETED_FIXTURES" if [ "$NUM_ADDED_FIXTURES" -gt "$NUM_DELETED_FIXTURES" ]; then echo "❌ Error: More files have been added to '__fixtures__' directories than deleted." exit 1 else echo "✅ Fixtures files check passed." fi echo "🎉 All checks passed successfully." - name: Check TODOs for references resolved by this PR uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | const body = context.payload.pull_request.body ?? ''; const closingPattern = /\b(?:close[sd]?|fix(?:es|ed)?|resolve[sd]?):?\s+(?:https:\/\/github\.com\/renovatebot\/renovate\/(?:issues|discussions)\/|#)(?\d+)\b/gi; const referenceNumbers = new Set(); for (const match of body.matchAll(closingPattern)) { referenceNumbers.add(match.groups.referenceNumber); } if (referenceNumbers.size === 0) { core.info('No issues or discussions are resolved by this pull request'); return; } const references = [...referenceNumbers].join('|'); const todoPattern = `TODO.*#(${references})([^0-9]|$)`; const result = await exec.getExecOutput( 'git', ['grep', '-n', '-E', todoPattern, '--', '.'], { ignoreReturnCode: true, silent: true }, ); if (result.exitCode === 1) { core.info( 'No TODOs reference issues or discussions resolved by this pull request', ); return; } if (result.exitCode !== 0) { core.setFailed(`git grep failed: ${result.stderr}`); return; } core.setFailed( `Remove or update TODOs that reference issues or discussions resolved by this pull request:\n${result.stdout}`, );