|
| 1 | +name: Dependency Audit |
| 2 | + |
| 3 | +env: |
| 4 | + PYTHON_VERSION: "3.12" |
| 5 | + |
| 6 | +# pip-audit compares the lockfile against advisory databases that publish |
| 7 | +# continuously, so the result tracks the clock, not the commit. Hence the cron. |
| 8 | +on: |
| 9 | + schedule: |
| 10 | + - cron: "17 6 * * *" |
| 11 | + # Unfiltered: a required check behind a paths filter never reports on a |
| 12 | + # pull request that misses the filter, which blocks the merge. |
| 13 | + pull_request: |
| 14 | + push: |
| 15 | + branches: [main] |
| 16 | + paths: |
| 17 | + - "pyproject.toml" |
| 18 | + - "uv.lock" |
| 19 | + - ".github/workflows/dependency-audit.yml" |
| 20 | + workflow_dispatch: |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: read |
| 24 | + |
| 25 | +concurrency: |
| 26 | + group: dependency-audit-${{ github.event.pull_request.number || github.ref }} |
| 27 | + cancel-in-progress: true |
| 28 | + |
| 29 | +jobs: |
| 30 | + dependency-audit: |
| 31 | + runs-on: ubuntu-latest |
| 32 | + timeout-minutes: 10 |
| 33 | + permissions: |
| 34 | + contents: read |
| 35 | + issues: write |
| 36 | + steps: |
| 37 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 38 | + with: |
| 39 | + fetch-depth: 1 |
| 40 | + persist-credentials: false |
| 41 | + |
| 42 | + - name: 🐍 setup python |
| 43 | + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 |
| 44 | + with: |
| 45 | + python-version: ${{ env.PYTHON_VERSION }} |
| 46 | + |
| 47 | + - name: 🛠️ install uv |
| 48 | + run: | |
| 49 | + python -m pip install --upgrade pip |
| 50 | + pip install uv |
| 51 | +
|
| 52 | + - name: 🛡️ pip-audit (known CVEs in the locked deps) |
| 53 | + id: audit |
| 54 | + run: | |
| 55 | + uv export --no-hashes --no-emit-project --format requirements-txt > /tmp/req-audit.txt |
| 56 | +
|
| 57 | + set +e |
| 58 | + uvx pip-audit --strict --progress-spinner off --disable-pip --no-deps \ |
| 59 | + -r /tmp/req-audit.txt 2>&1 | tee /tmp/audit.log |
| 60 | + status=${PIPESTATUS[0]} |
| 61 | + set -e |
| 62 | +
|
| 63 | + # Written before the exit so the next step can quote it. |
| 64 | + { |
| 65 | + echo 'report<<AUDIT_REPORT_EOF' |
| 66 | + cat /tmp/audit.log |
| 67 | + echo 'AUDIT_REPORT_EOF' |
| 68 | + } >> "$GITHUB_OUTPUT" |
| 69 | +
|
| 70 | + exit "$status" |
| 71 | +
|
| 72 | + # A scheduled run has no pull request to turn red, so record it instead. |
| 73 | + - name: 📮 open or update the tracking issue |
| 74 | + if: always() && steps.audit.outcome == 'failure' && github.event_name == 'schedule' |
| 75 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 76 | + env: |
| 77 | + AUDIT_REPORT: ${{ steps.audit.outputs.report }} |
| 78 | + with: |
| 79 | + script: | |
| 80 | + const marker = '<!-- dependency-audit-tracking-issue -->'; |
| 81 | + const title = 'Dependency audit: known vulnerabilities in the locked dependencies'; |
| 82 | + const runUrl = |
| 83 | + `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}` + |
| 84 | + `/actions/runs/${context.runId}`; |
| 85 | + const body = [ |
| 86 | + marker, |
| 87 | + '`pip-audit` found known vulnerabilities in the locked dependency set.', |
| 88 | + '', |
| 89 | + 'Bump the affected pins in `pyproject.toml`, run `uv lock`, and open a PR.', |
| 90 | + '', |
| 91 | + '```', |
| 92 | + process.env.AUDIT_REPORT.trim(), |
| 93 | + '```', |
| 94 | + '', |
| 95 | + `Run: ${runUrl}`, |
| 96 | + `Last checked: ${new Date().toISOString()}`, |
| 97 | + ].join('\n'); |
| 98 | +
|
| 99 | + const existing = await github.paginate(github.rest.issues.listForRepo, { |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + state: 'open', |
| 103 | + per_page: 100, |
| 104 | + }); |
| 105 | + const tracking = existing.find( |
| 106 | + (issue) => !issue.pull_request && issue.body && issue.body.includes(marker), |
| 107 | + ); |
| 108 | +
|
| 109 | + if (tracking) { |
| 110 | + await github.rest.issues.update({ |
| 111 | + owner: context.repo.owner, |
| 112 | + repo: context.repo.repo, |
| 113 | + issue_number: tracking.number, |
| 114 | + body, |
| 115 | + }); |
| 116 | + core.notice(`Updated tracking issue #${tracking.number}`); |
| 117 | + } else { |
| 118 | + const created = await github.rest.issues.create({ |
| 119 | + owner: context.repo.owner, |
| 120 | + repo: context.repo.repo, |
| 121 | + title, |
| 122 | + body, |
| 123 | + labels: ['dependencies'], |
| 124 | + }); |
| 125 | + core.notice(`Opened tracking issue #${created.data.number}`); |
| 126 | + } |
| 127 | +
|
| 128 | + - name: ✅ close the tracking issue once the audit is clean |
| 129 | + if: always() && steps.audit.outcome == 'success' && github.event_name == 'schedule' |
| 130 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 131 | + with: |
| 132 | + script: | |
| 133 | + const marker = '<!-- dependency-audit-tracking-issue -->'; |
| 134 | + const existing = await github.paginate(github.rest.issues.listForRepo, { |
| 135 | + owner: context.repo.owner, |
| 136 | + repo: context.repo.repo, |
| 137 | + state: 'open', |
| 138 | + per_page: 100, |
| 139 | + }); |
| 140 | + const tracking = existing.find( |
| 141 | + (issue) => !issue.pull_request && issue.body && issue.body.includes(marker), |
| 142 | + ); |
| 143 | + if (!tracking) { |
| 144 | + return; |
| 145 | + } |
| 146 | + await github.rest.issues.createComment({ |
| 147 | + owner: context.repo.owner, |
| 148 | + repo: context.repo.repo, |
| 149 | + issue_number: tracking.number, |
| 150 | + body: 'The scheduled audit is clean again. Closing.', |
| 151 | + }); |
| 152 | + await github.rest.issues.update({ |
| 153 | + owner: context.repo.owner, |
| 154 | + repo: context.repo.repo, |
| 155 | + issue_number: tracking.number, |
| 156 | + state: 'closed', |
| 157 | + }); |
0 commit comments