|
| 1 | +# This workflow is provided via the organization template repository |
| 2 | +# |
| 3 | +# https://github.com/nextcloud/.github |
| 4 | +# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization |
| 5 | +# |
| 6 | +# SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | +# SPDX-License-Identifier: MIT |
| 8 | + |
| 9 | +name: Selective npm audit fix |
| 10 | + |
| 11 | +on: |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + branch: |
| 15 | + description: 'Branch to run audit on' |
| 16 | + required: true |
| 17 | + default: main |
| 18 | + |
| 19 | +jobs: |
| 20 | + matrix: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + outputs: |
| 23 | + packages: ${{ steps.packages.outputs.vulns }} |
| 24 | + |
| 25 | + name: 'npm audit matrix' |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout |
| 29 | + id: checkout |
| 30 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 31 | + with: |
| 32 | + persist-credentials: false |
| 33 | + # ref: ${{ matrix.branches }} |
| 34 | + continue-on-error: true |
| 35 | + |
| 36 | + - name: Read package.json node and npm engines version |
| 37 | + uses: skjnldsv/read-package-engines-version-actions@06d6baf7d8f41934ab630e97d9e6c0bc9c9ac5e4 # v3 |
| 38 | + id: versions |
| 39 | + with: |
| 40 | + fallbackNode: '^20' |
| 41 | + fallbackNpm: '^10' |
| 42 | + |
| 43 | + - name: Set up node ${{ steps.versions.outputs.nodeVersion }} |
| 44 | + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 45 | + with: |
| 46 | + node-version: ${{ steps.versions.outputs.nodeVersion }} |
| 47 | + |
| 48 | + - name: Set up npm ${{ steps.versions.outputs.npmVersion }} |
| 49 | + run: npm i -g 'npm@${{ steps.versions.outputs.npmVersion }}' |
| 50 | + |
| 51 | + - name: Run npm ci and npm run build |
| 52 | + id: packages |
| 53 | + if: steps.checkout.outcome == 'success' |
| 54 | + env: |
| 55 | + CYPRESS_INSTALL_BINARY: 0 |
| 56 | + run: | |
| 57 | + npm ci |
| 58 | + npm audit --json --audit-level=none > "audit.json" |
| 59 | + vulns=$(jq -r '.vulnerabilities | to_entries[] | select(.value.fixAvailable != false) | .key' "audit.json") |
| 60 | + echo $vulns |
| 61 | + echo "vulns<<EOF" >> "$GITHUB_OUTPUT" |
| 62 | + echo "$vulns" >> "$GITHUB_OUTPUT" |
| 63 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 64 | +
|
| 65 | + fix: |
| 66 | + runs-on: ubuntu-latest |
| 67 | + |
| 68 | + needs: |
| 69 | + - matrix |
| 70 | + strategy: |
| 71 | + matrix: |
| 72 | + package: ${{ fromJson(needs.matrix.outputs.packages) }} |
| 73 | + branches: |
| 74 | + - ${{ github.event.repository.default_branch }} |
| 75 | + |
| 76 | + name: npm-audit-fix-${{ matrix.package }} |
| 77 | + |
| 78 | + steps: |
| 79 | + - name: Checkout |
| 80 | + id: checkout |
| 81 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 82 | + with: |
| 83 | + persist-credentials: false |
| 84 | + ref: ${{ matrix.branches }} |
| 85 | + continue-on-error: true |
| 86 | + |
| 87 | + - name: Read package.json node and npm engines version |
| 88 | + uses: skjnldsv/read-package-engines-version-actions@06d6baf7d8f41934ab630e97d9e6c0bc9c9ac5e4 # v3 |
| 89 | + id: versions |
| 90 | + with: |
| 91 | + fallbackNode: '^20' |
| 92 | + fallbackNpm: '^10' |
| 93 | + |
| 94 | + - name: Set up node ${{ steps.versions.outputs.nodeVersion }} |
| 95 | + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 96 | + with: |
| 97 | + node-version: ${{ steps.versions.outputs.nodeVersion }} |
| 98 | + |
| 99 | + - name: Set up npm ${{ steps.versions.outputs.npmVersion }} |
| 100 | + run: npm i -g 'npm@${{ steps.versions.outputs.npmVersion }}' |
| 101 | + |
| 102 | + - name: Run npm ci and npm run build |
| 103 | + if: steps.checkout.outcome == 'success' |
| 104 | + env: |
| 105 | + CYPRESS_INSTALL_BINARY: 0 |
| 106 | + run: | |
| 107 | + npm ci |
| 108 | + npm update ${{ matrix.package }} |
| 109 | +
|
| 110 | + - name: Create Pull Request |
| 111 | + if: steps.checkout.outcome == 'success' |
| 112 | + uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8 |
| 113 | + with: |
| 114 | + token: ${{ secrets.COMMAND_BOT_PAT }} |
| 115 | + commit-message: 'fix(deps): update ${{ matrix.package }}' |
| 116 | + committer: GitHub <noreply@github.com> |
| 117 | + author: nextcloud-command <nextcloud-command@users.noreply.github.com> |
| 118 | + signoff: true |
| 119 | + branch: fix/deps/${{ matrix.package }}-${{ matrix.package }}-npm-audit |
| 120 | + title: '[${{ matrix.branches }}] fix(deps): update ${{ matrix.package }}' |
| 121 | + body: 'Automated npm audit fix' |
| 122 | + labels: | |
| 123 | + dependencies |
| 124 | + 3. to review |
0 commit comments