summaryrefslogtreecommitdiff
path: root/.github/workflows/security-scan.yml
blob: a8121cfdb622f338e919df80268d3cfb26296088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
name: Security Scan

on:
  pull_request:
    branches: [main]
    types: [opened, synchronize, reopened, labeled]

permissions:
  contents: read
  pull-requests: write

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  security-scan:
    name: Plugin security scan
    runs-on: ubuntu-latest
    steps:
      - name: Check if scan needed
        id: trigger
        env:
          GH_TOKEN: ${{ github.token }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          EVENT_ACTION: ${{ github.event.action }}
          HAS_LABEL: ${{ contains(github.event.pull_request.labels.*.name, 'security-scan') }}
          REPO: ${{ github.repository }}
        run: |
          set -euo pipefail
          # Get changed files and extract unique plugin directories
          changed=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}/files" \
            --paginate --jq '.[].filename')
          changed_plugins=$(echo "$changed" \
            | sed -n 's|^\(plugins/[^/]*\)/.*|\1|p' \
            | sort -u) || true

          # Always run if security-scan label is present
          if [[ "$HAS_LABEL" == 'true' ]]; then
            if [[ -n "$changed_plugins" ]]; then
              echo "$changed_plugins" > changed-plugins.txt
            else
              echo "plugins/" > changed-plugins.txt
            fi
            echo "run=true" >> "$GITHUB_OUTPUT"
            exit 0
          fi
          # Skip other label events (unrelated labels)
          if [[ "$EVENT_ACTION" == 'labeled' ]]; then
            echo "run=false" >> "$GITHUB_OUTPUT"
            exit 0
          fi
          # Run if any plugins/ files changed
          if [[ -n "$changed_plugins" ]]; then
            echo "$changed_plugins" > changed-plugins.txt
            echo "run=true" >> "$GITHUB_OUTPUT"
          else
            echo "run=false" >> "$GITHUB_OUTPUT"
          fi

      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2
        if: steps.trigger.outputs.run == 'true'
        with:
          persist-credentials: false

      - uses: astral-sh/setup-uv@eac588ad8def6316056a12d4907a9d4d84ff7a3b  # v7.3.0
        if: steps.trigger.outputs.run == 'true'

      - name: Run security scanner
        if: steps.trigger.outputs.run == 'true'
        id: scan
        run: |
          set +e
          xargs uv run scripts/scan_plugin.py --format=markdown \
            < changed-plugins.txt > scan-report.md
          echo "exit_code=$?" >> "$GITHUB_OUTPUT"

      - name: Find existing comment
        if: steps.trigger.outputs.run == 'true'
        uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad  # v4.0.0
        id: find-comment
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: github-actions[bot]
          body-includes: <!-- security-scan -->

      - name: Post or update comment
        if: steps.trigger.outputs.run == 'true' && steps.scan.outputs.exit_code != '0' && steps.scan.outputs.exit_code != '1'
        uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9  # v5.0.0
        with:
          comment-id: ${{ steps.find-comment.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body-path: scan-report.md
          edit-mode: replace

      - name: Delete comment if clean
        if: steps.trigger.outputs.run == 'true' && steps.scan.outputs.exit_code == '0' && steps.find-comment.outputs.comment-id != ''
        uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9  # v5.0.0
        with:
          comment-id: ${{ steps.find-comment.outputs.comment-id }}
          body: |
            <!-- security-scan -->
            No security findings.
          edit-mode: replace