|
| 1 | +name: Dismiss Stale Bandit B101 Alerts |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + dry_run: |
| 7 | + description: "List matching alerts without dismissing them (set to 'false' to actually dismiss)" |
| 8 | + required: true |
| 9 | + default: "true" |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - "true" |
| 13 | + - "false" |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + security-events: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + dismiss: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Dismiss B101 alerts in test directories |
| 26 | + env: |
| 27 | + GH_TOKEN: ${{ github.token }} |
| 28 | + run: | |
| 29 | + set -euo pipefail |
| 30 | +
|
| 31 | + REPO="${{ github.repository }}" |
| 32 | + DRY_RUN="${{ github.event.inputs.dry_run }}" |
| 33 | + DISMISSED=0 |
| 34 | + SKIPPED=0 |
| 35 | + TOTAL=0 |
| 36 | +
|
| 37 | + echo "=== Fetching open Bandit alerts for ${REPO} ===" |
| 38 | +
|
| 39 | + while read -r line; do |
| 40 | + number=$(echo "$line" | jq -r '.number // empty') |
| 41 | + path=$(echo "$line" | jq -r '.path // empty') |
| 42 | + [ -z "$number" ] && continue |
| 43 | +
|
| 44 | + TOTAL=$((TOTAL + 1)) |
| 45 | +
|
| 46 | + # Only target test files |
| 47 | + case "$path" in |
| 48 | + *tests/*|*/test_*) |
| 49 | + ;; |
| 50 | + *) |
| 51 | + echo " SKIP [#${number}] ${path} — not a test file" |
| 52 | + SKIPPED=$((SKIPPED + 1)) |
| 53 | + continue |
| 54 | + ;; |
| 55 | + esac |
| 56 | +
|
| 57 | + if [ "$DRY_RUN" = "true" ]; then |
| 58 | + echo " DRY [#${number}] Would dismiss — ${path}" |
| 59 | + else |
| 60 | + echo " DISMISS [#${number}] ${path}" |
| 61 | + gh api -X PATCH "/repos/${REPO}/code-scanning/alerts/${number}" \ |
| 62 | + -f state=dismissed \ |
| 63 | + -f dismissed_reason="false positive" \ |
| 64 | + -f dismissed_comment="Bandit B101 flagged assert in test file. Tests are never executed under python -O, so the risk of assert removal does not apply. Test directories are now excluded via .bandit config." \ |
| 65 | + --silent |
| 66 | + fi |
| 67 | +
|
| 68 | + DISMISSED=$((DISMISSED + 1)) |
| 69 | + done < <( |
| 70 | + gh api "/repos/${REPO}/code-scanning/alerts?tool_name=Bandit&state=open" \ |
| 71 | + --paginate \ |
| 72 | + --jq '.[] | select(.rule.id == "B101") | {number, path: .most_recent_instance.location.path}' \ |
| 73 | + || echo "[]" |
| 74 | + ) |
| 75 | +
|
| 76 | + echo "" |
| 77 | + echo "=== Summary ===" |
| 78 | + echo " Total B101 alerts found: ${TOTAL}" |
| 79 | + echo " Test-file alerts processed: ${DISMISSED}" |
| 80 | + echo " Non-test alerts skipped: ${SKIPPED}" |
| 81 | + if [ "$DRY_RUN" = "true" ]; then |
| 82 | + echo "" |
| 83 | + echo "⚠️ Dry run completed — no alerts were actually dismissed." |
| 84 | + echo " Re-run with dry_run=false to dismiss." |
| 85 | + fi |
0 commit comments