|
1 | | -name: Semgrep |
| 1 | +name: Semgrep Scan |
2 | 2 |
|
3 | | -# Run workflow each time code is pushed to your repository. |
4 | 3 | on: |
5 | | - push: |
6 | | - branches: |
7 | | - - main |
8 | 4 | pull_request: |
9 | 5 | branches: |
10 | 6 | - main |
| 7 | + |
11 | 8 | jobs: |
12 | 9 | build: |
13 | 10 | runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + pull-requests: write # Give write permission to PRs |
| 13 | + issues: write |
14 | 14 | steps: |
15 | 15 | - name: Checkout code |
16 | 16 | uses: actions/checkout@v3 |
17 | 17 |
|
18 | | - - name: Install Semgrep |
19 | | - run: pip install semgrep |
| 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 |
20 | 23 |
|
21 | 24 | - name: Run Semgrep |
22 | 25 | run: | |
23 | | - semgrep --config .semgreprules/customRule.yml --config auto --severity ERROR --sarif . > results.sarif |
| 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 |
24 | 29 |
|
25 | | - - name: Upload SARIF file |
26 | | - uses: github/codeql-action/upload-sarif@v3 |
| 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 |
27 | 37 | with: |
28 | | - # Path to SARIF file relative to the root of the repository |
29 | | - sarif_file: results.sarif |
| 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'); |
30 | 45 |
|
31 | | - - name: Upload results |
32 | | - uses: actions/upload-artifact@v4 |
33 | | - with: |
34 | | - name: semgrep-results |
35 | | - path: results.sarif |
| 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