Skip to content

Commit d9ee761

Browse files
authored
semgrep github workflow updated to show style guide warnings in ./semgrep as annotations and error
* semgrep github workflow updated to show warnings * Add explicit messaging for how to skip semgrep check and intensify messaging to produce errors * adding passthrough error code handling to jq and model the same in the semgrep-repo-rules tool * Use $PIPESTATUS to get error code of item in piped command list * show the error code values along pipeline to seek issue * Specify bash in shell config as sh is the default inside a container * Exit with correct error code * show semgrep messages as warning annotations to distinguish from semgrep error code * add use of [skip style guide check] in commit message * Set COMMIT_MESSAGE environment variable with last commit message * COMMIT_MESSAGE needs to set to the second to last message to skip the autogenerated merge message * Grabbing commit SHA from the pull_request event * Add explantory message inside configure step * Show commit message cleanly in configure step * Use tee to set the environment variable and show the value it is set to * keep semgrep return code intact from local tool run
1 parent 37ad63e commit d9ee761

File tree

4 files changed

+88
-49
lines changed

4 files changed

+88
-49
lines changed

.github/workflows/semgrep.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@ jobs:
2525
# fetch full history so Semgrep can compare against the base branch
2626
fetch-depth: 0
2727

28+
# Configure
29+
# add git safe directory to enable git commands on checkout path
30+
# set COMMIT_MESSAGE environment variable to be able to skip semgrep if requested
31+
- name: Configure
32+
run: |
33+
git config --global --add safe.directory $PWD
34+
echo "COMMIT_MESSAGE=\"$(git log --format=%B -n 1 ${{ github.event.pull_request.head.sha }})\"" | tee /dev/stderr >> "$GITHUB_ENV"
35+
echo "(if the last commit message contains '[skip style guide checks]' Semgrep style guide rule checks will be skipped)"
36+
2837
# Semgrep CI to run on Schedule (Cron) or Manual Dispatch
2938
# scans using managed rules at cloudflare.semgrep.dev
30-
- name: Semgrep CI Rules (Managed rules at cloudflare.semgrep.dev)
39+
- name: Semgrep managed rules (managed at cloudflare.semgrep.dev)
3140
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
3241
run: semgrep ci
3342

3443
# Semgrep Scan to run on Pull Request events
3544
# scans using rules inside the .semgrep/ folder and fails on error
3645
# include [skip semgrep] in top-most commit message to skip scan
37-
- name: Semgrep Repo Rules (Custom rules found in .semgrep/)
38-
if: github.event_name == 'pull_request' && !contains(github.event.head_commit.message, '[skip semgrep]')
46+
- name: Semgrep style guide rules (stored in .semgrep/)
47+
shell: bash
48+
if: github.event_name == 'pull_request' && !contains(env.COMMIT_MESSAGE, '[skip style guide checks]')
3949
run: |
4050
41-
git config --global --add safe.directory $PWD
51+
echo "env.COMMIT_MESSAGE: ${{ env.COMMIT_MESSAGE }}"
52+
4253
base_commit=$(git merge-base HEAD origin/$GITHUB_BASE_REF)
4354
git diff $base_commit... --diff-filter=ACMRT --name-only | grep -E '\.(htm|html|yaml|yml|md|mdx)$' > tools/relevant_changed_files.txt || true
4455
@@ -48,8 +59,12 @@ jobs:
4859
semgrep scan \
4960
--config .semgrep --metrics=off \
5061
--include "*.mdx" --include "*.mdx" \
51-
$list_of_files
52-
# add '--error' to return error code to workflow
62+
--error \
63+
--json \
64+
$list_of_files \
65+
| jq --raw-output ".results[] | \"::warning file=\(.path),line=\(.start.line),title=\(.check_id)::\(.extra.message)\""
66+
exit ${PIPESTATUS[0]}
5367
else
54-
echo "No relevant files changed."
68+
echo "No relevant files changed"
5569
fi
70+

.semgrep/dates-in-docs.yaml

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
rules:
2+
- id: style-guide-coming-soon
3+
languages: [generic]
4+
message: "Found forbidden string 'coming soon'. Too often we set expectations unfairly by attaching this phrase to a feature that may not actually arrive soon. (add [skip style guide checks] to commit message to skip)"
5+
severity: MEDIUM
6+
paths:
7+
include:
8+
- "*.htm"
9+
- "*.html"
10+
- "*.md"
11+
- "*.mdx"
12+
- "*.yaml"
13+
- "*.yml"
14+
exclude:
15+
- "/src/content/changelog/**"
16+
- "/src/content/release-notes/**"
17+
- "/.semgrep/**"
18+
- "/.github/**"
19+
patterns:
20+
- pattern-regex: "[Cc]oming [Ss]oon"
21+
22+
- id: style-guide-potential-date-month
23+
languages: [generic]
24+
message: "Potential month found. Documentation should strive to represent universal truth, not something time-bound. (add [skip style guide checks] to commit message to skip)"
25+
severity: MEDIUM
26+
paths:
27+
include:
28+
- "*.htm"
29+
- "*.html"
30+
- "*.md"
31+
- "*.mdx"
32+
- "*.yaml"
33+
- "*.yml"
34+
exclude:
35+
- "/src/content/changelog/**"
36+
- "/src/content/release-notes/**"
37+
- "/.semgrep/**"
38+
- "/.github/**"
39+
patterns:
40+
- pattern-regex: "Jan|Feb|Mar[^k]|Apr|May[^b]|Jun[^k]|Jul|Aug|Sep|Nov|Dec[^i]"
41+
42+
- id: style-guide-potential-date-year
43+
languages: [generic]
44+
message: "Potential year found. Documentation should strive to represent universal truth, not something time-bound. (add [skip style guide checks] to commit message to skip)"
45+
severity: MEDIUM
46+
paths:
47+
include:
48+
- "*.htm"
49+
- "*.html"
50+
- "*.md"
51+
- "*.mdx"
52+
- "*.yaml"
53+
- "*.yml"
54+
exclude:
55+
- "/src/content/changelog/**"
56+
- "/src/content/release-notes/**"
57+
- "/.semgrep/**"
58+
- "/.github/**"
59+
patterns:
60+
# ignore 2xxx- with a - at the end (double-escape because in string and not a range operator!)
61+
- pattern-regex: "20[0-9][0-9][^\\-]"

tools/semgrep-repo-rules

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#! /bin/bash
22

3+
34
repo_root_dir="$(git rev-parse --show-toplevel)"
45

56
pushd "${repo_root_dir}" > /dev/null || return
@@ -17,8 +18,11 @@ if [ -s tools/relevant_changed_files.txt ]; then
1718
semgrep scan \
1819
--config .semgrep --metrics=off \
1920
--include "*.mdx" --include "*.mdx" \
20-
--force-color \
21+
--error \
2122
$list_of_files
23+
semgrep_return_code=$?
24+
echo "return code: $semgrep_return_code"
25+
exit $semgrep_return_code
2226
else
2327
echo "No relevant files changed."
2428
fi

0 commit comments

Comments
 (0)