|
| 1 | +name: 'Z-AUTOMATED: SBOM Repo Scan' |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + actions: read # Required for anchore/sbom-action |
| 9 | + contents: write # Required for anchore/sbom-action |
| 10 | + id-token: write # Required for requesting the JWT |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + sbom_scan: |
| 15 | + name: SBOM Repo Scan |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v5 |
| 19 | + with: |
| 20 | + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis |
| 21 | + |
| 22 | + - uses: anchore/sbom-action@v0 |
| 23 | + with: |
| 24 | + path: "." |
| 25 | + format: cyclonedx-json |
| 26 | + output-file: sbom-repo-${{ github.event.repository.name }}-${{ github.sha }}.cdx.json |
| 27 | + |
| 28 | + - uses: anchore/scan-action@v7 |
| 29 | + id: sbom-scan |
| 30 | + with: |
| 31 | + sbom: sbom-repo-${{ github.event.repository.name }}-${{ github.sha }}.cdx.json |
| 32 | + fail-build: true |
| 33 | + severity-cutoff: low |
| 34 | + only-fixed: true |
| 35 | + output-format: sarif |
| 36 | + |
| 37 | + - name: Upload Anchore scan SARIF report |
| 38 | + uses: github/codeql-action/upload-sarif@v3 |
| 39 | + if: always() |
| 40 | + with: |
| 41 | + sarif_file: ${{ steps.sbom-scan.outputs.sarif }} |
| 42 | + |
| 43 | + - name: Add/Update SBOM failure comment |
| 44 | + uses: actions/github-script@v8 |
| 45 | + if: always() && failure() |
| 46 | + with: |
| 47 | + script: | |
| 48 | + // 1. Retrieve existing bot comments for the PR |
| 49 | + const { data: comments } = await github.rest.issues.listComments({ |
| 50 | + owner: context.repo.owner, |
| 51 | + repo: context.repo.repo, |
| 52 | + issue_number: context.issue.number, |
| 53 | + }) |
| 54 | + |
| 55 | + const botComment = comments.find(comment => { |
| 56 | + return comment.user.type === 'Bot' && comment.body.includes('Code security issues found') |
| 57 | + }) |
| 58 | +
|
| 59 | + // 2. Prepare format of the comment |
| 60 | + const output = `### Code security issues found |
| 61 | + |
| 62 | + View full details [here](https://github.com/${{ github.repository }}/security/code-scanning?query=is%3Aopen+pr%3A${{ github.event.pull_request.number }}).`; |
| 63 | +
|
| 64 | + // 3. If we have a comment, update it, otherwise create a new one |
| 65 | + if (botComment) { |
| 66 | + github.rest.issues.deleteComment({ |
| 67 | + owner: context.repo.owner, |
| 68 | + repo: context.repo.repo, |
| 69 | + comment_id: botComment.id, |
| 70 | + body: output |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + github.rest.issues.createComment({ |
| 75 | + issue_number: context.issue.number, |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + body: output |
| 79 | + }) |
| 80 | +
|
| 81 | + - name: Delete SBOM failure comment |
| 82 | + uses: actions/github-script@v8 |
| 83 | + if: always() && success() |
| 84 | + with: |
| 85 | + script: | |
| 86 | + // 1. Retrieve existing bot comments for the PR |
| 87 | + const { data: comments } = await github.rest.issues.listComments({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + issue_number: context.issue.number, |
| 91 | + }) |
| 92 | + |
| 93 | + const botComment = comments.find(comment => { |
| 94 | + return comment.user.type === 'Bot' && comment.body.includes('Code security issues found') |
| 95 | + }) |
| 96 | +
|
| 97 | + // 2. If we have a comment, update it, otherwise create a new one |
| 98 | + if (botComment) { |
| 99 | + github.rest.issues.deleteComment({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + comment_id: botComment.id |
| 103 | + }) |
| 104 | + } |
0 commit comments