|
| 1 | +name: Semgrep Scan |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + build: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + pull-requests: write # Give write permission to PRs |
| 13 | + issues: write |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v3 |
| 17 | + |
| 18 | + - name: Install Semgrep and jq |
| 19 | + run: | |
| 20 | + sudo apt install python3-venv jq |
| 21 | + python3 -m venv .venv |
| 22 | + .venv/bin/pip install semgrep |
| 23 | +
|
| 24 | + - name: Run Semgrep |
| 25 | + run: | |
| 26 | + source .venv/bin/activate |
| 27 | + semgrep --config auto --severity ERROR --json-output=results.json --no-error |
| 28 | + cat results.json | jq .results > pretty-results.json |
| 29 | +
|
| 30 | + - name: Display Raw Semgrep JSON Output |
| 31 | + run: | |
| 32 | + echo "Displaying raw Semgrep results..." |
| 33 | + cat pretty-results.json |
| 34 | + |
| 35 | + - name: Add comment on PR if findings are found |
| 36 | + uses: actions/github-script@v6 |
| 37 | + with: |
| 38 | + script: | |
| 39 | + // Ensure the context has a pull_request |
| 40 | + if (context.payload.pull_request) { |
| 41 | + const prNumber = context.payload.pull_request.number; |
| 42 | + const fs = require('fs'); |
| 43 | + const results = JSON.parse(fs.readFileSync('pretty-results.json', 'utf8')); |
| 44 | + const highFindings = results.filter(result => result.extra && result.extra.severity === 'ERROR'); |
| 45 | +
|
| 46 | + // Comment if findings exist |
| 47 | + if (highFindings.length > 0) { |
| 48 | + const comment = `**Semgrep Findings:** Issues with Error level severity are found (Error is Highest severity in Semgrep), Please resolve the issues before merging.`; |
| 49 | + await github.rest.issues.createComment({ |
| 50 | + ...context.repo, |
| 51 | + issue_number: prNumber, |
| 52 | + body: comment |
| 53 | + }); |
| 54 | + } else { |
| 55 | + const noIssuesComment = "**Semgrep findings:** No issues found, Good to merge."; |
| 56 | + await github.rest.issues.createComment({ |
| 57 | + ...context.repo, |
| 58 | + issue_number: prNumber, |
| 59 | + body: noIssuesComment |
| 60 | + }); |
| 61 | + } |
| 62 | + } else { |
| 63 | + console.log("This workflow wasn't triggered by a pull request, so no comment will be added."); |
| 64 | + } |
0 commit comments