Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions .github/workflows/semgrep.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
name: Semgrep
name: Semgrep Scan

# Run workflow each time code is pushed to your repository.
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: macos-latest
runs-on: ubuntu-latest
permissions:
pull-requests: write # Give write permission to PRs
issues: write
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install Semgrep
run: pip install semgrep
- name: Install Semgrep and jq
run: |
sudo apt install python3-venv jq
python3 -m venv .venv
.venv/bin/pip install semgrep

- name: Run Semgrep
run: |
semgrep --config .semgreprules/customRule.yml --config auto --severity ERROR --sarif . > results.sarif
source .venv/bin/activate
semgrep --config auto --severity ERROR --json-output=results.json --no-error
cat results.json | jq .results > pretty-results.json

- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v3
- name: Display Raw Semgrep JSON Output
run: |
echo "Displaying raw Semgrep results..."
cat pretty-results.json

- name: Add comment on PR if findings are found
uses: actions/github-script@v6
with:
# Path to SARIF file relative to the root of the repository
sarif_file: results.sarif
script: |
// Ensure the context has a pull_request
if (context.payload.pull_request) {
const prNumber = context.payload.pull_request.number;
const fs = require('fs');
const results = JSON.parse(fs.readFileSync('pretty-results.json', 'utf8'));
const highFindings = results.filter(result => result.extra && result.extra.severity === 'ERROR');

- name: Upload results
uses: actions/upload-artifact@v4
with:
name: semgrep-results
path: results.sarif
// Comment if findings exist
if (highFindings.length > 0) {
const comment = `**Semgrep Findings:** Issues with Error level severity are found (Error is Highest severity in Semgrep), Please resolve the issues before merging.`;
await github.rest.issues.createComment({
...context.repo,
issue_number: prNumber,
body: comment
});
} else {
const noIssuesComment = "**Semgrep findings:** No issues found, Good to merge.";
await github.rest.issues.createComment({
...context.repo,
issue_number: prNumber,
body: noIssuesComment
});
}
} else {
console.log("This workflow wasn't triggered by a pull request, so no comment will be added.");
}
Loading