Skip to content

Commit 3050c54

Browse files
authored
Add super-linter to pre-commit (#30)
* add super-linter to pre-commit * add superlinter to usual linting pre-commit script * enhance config * finalize
1 parent 821b1ab commit 3050c54

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
name: linting (via docker)
33
entry: /linting.sh
44
language: docker
5+
require_serial: true

Dockerfile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
FROM python:3-slim
1+
FROM ghcr.io/super-linter/super-linter:latest
22

33
# This Dockerfile is used by the linting pre-commit hook.
44
# Unfortunately there is no config option yet to specify the name or path
55
# of this image, so it must be on root level simply named Dockerfile...
66

7-
RUN apt-get update && apt-get install jq wget -y
7+
RUN apk add jq wget
88
RUN pip install --upgrade pip yq virtualenv toml-union
99

1010
# Copy linting workflow to identify linter versions if needed
@@ -22,3 +22,31 @@ RUN pip install -r requirements.txt
2222
COPY pre_commit_hooks/linting.sh /linting.sh
2323

2424
CMD [ "bash /linting.sh" ]
25+
26+
# Below for super-linter
27+
28+
# pre-commit seems to set too strict permissions:
29+
# error: could not lock config file //.gitconfig: Permission denied
30+
# Therefore do not configure globally
31+
# https://github.com/super-linter/super-linter/blob/main/lib/linter.sh#L345
32+
RUN sed -i 's+git config --global --add safe.directory+git config --add safe.directory+g' /action/lib/linter.sh
33+
34+
# save to be able to export later
35+
RUN echo "RUN_LOCAL=TRUE" >> /super-linter.txt
36+
RUN echo "DEFAULT_BRANCH=main" >> /super-linter.txt
37+
RUN echo "DEFAULT_WORKSPACE=/src" >> /super-linter.txt
38+
RUN echo BASH_SEVERITY=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."BASH_SEVERITY"'.default) >> /super-linter.txt
39+
RUN echo VALIDATE_BASH=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_BASH"'.default) >> /super-linter.txt
40+
RUN echo VALIDATE_BASH_EXEC=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_BASH_EXEC"'.default) >> /super-linter.txt
41+
RUN echo VALIDATE_CSS=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_CSS"'.default) >> /super-linter.txt
42+
RUN echo VALIDATE_DOCKERFILE_HADOLINT=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_DOCKERFILE_HADOLINT"'.default) >> /super-linter.txt
43+
RUN echo VALIDATE_GITHUB_ACTIONS=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_GITHUB_ACTIONS"'.default) >> /super-linter.txt
44+
RUN echo VALIDATE_HTML=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_HTML"'.default) >> /super-linter.txt
45+
RUN echo VALIDATE_JAVASCRIPT_ES=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_JAVASCRIPT_ES"'.default) >> /super-linter.txt
46+
RUN echo VALIDATE_JAVASCRIPT_STANDARD=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_JAVASCRIPT_STANDARD"'.default) >> /super-linter.txt
47+
RUN echo VALIDATE_JSON=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_JSON"'.default) >> /super-linter.txt
48+
RUN echo VALIDATE_MARKDOWN=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_MARKDOWN"'.default) >> /super-linter.txt
49+
RUN echo VALIDATE_POWERSHELL=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_POWERSHELL"'.default) >> /super-linter.txt
50+
RUN echo VALIDATE_RENOVATE=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_RENOVATE"'.default) >> /super-linter.txt
51+
RUN echo VALIDATE_XML=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_XML"'.default) >> /super-linter.txt
52+
RUN echo VALIDATE_YAML=$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."VALIDATE_YAML"'.default) >> /super-linter.txt

pre_commit_hooks/linting.sh

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ RUN_BLACK=TRUE
2525
RUN_FLAKE8=TRUE
2626
RUN_PYLINT=TRUE
2727
RUN_RUFF=TRUE
28+
RUN_SUPERLINTER=TRUE
2829

2930
####################################
3031
# Overwrite linter versions if set #
@@ -38,10 +39,11 @@ then
3839
then
3940
RUN_BLACK=FALSE
4041
echo "- black configured to be skipped (empty string)"
41-
elif [ $DEFAULT_BLACK_VERSION != $NEW_BLACK_VERSION ]
42+
elif [ $DEFAULT_BLACK_VERSION != `echo $NEW_BLACK_VERSION | tr -d '"'` ]
4243
then
43-
# sed would fail with Permission denied
44+
# sed or pip would fail with Permission denied
4445
# sed -i "s+$DEFAULT_BLACK_VERSION+$NEW_BLACK_VERSION+g" /requirements.txt
46+
# pip3 install black==`echo $NEW_BLACK_VERSION | tr -d '"'`
4547
echo "- Warning: overwritten black version will not be installed!"
4648
fi
4749
else
@@ -108,7 +110,7 @@ echo
108110
# Install linters #
109111
###################
110112
# TODO: if versions are not default versions, this fails due to missing permissions
111-
pip3 install -r /requirements.txt
113+
# pip3 install -r /requirements.txt
112114
echo
113115
echo "flake8: `flake8 --version`"
114116
echo "pylint: `pylint --version`"
@@ -214,6 +216,27 @@ else
214216
echo "RUFF configured to be skipped"
215217
fi
216218

219+
if [ $RUN_SUPERLINTER != "FALSE" ]
220+
then
221+
echo
222+
echo "SUPERLINTER:"
223+
# overwrite rules with default config
224+
. /super-linter.txt && export $(cut -d= -f1 /super-linter.txt)
225+
# overwrite rules with custom repo config
226+
`cat /src/.github/workflows/linting.yml | yq .jobs.lint.with | grep -v "-version" | tr -d ',' | jq -r 'to_entries[] | "export \(.key)=\(.value)"'`
227+
# run linting script
228+
bash /action/lib/linter.sh
229+
if [ $? -ne 0 ]
230+
then
231+
RETURNCODE=1
232+
FAILINGSTEP="$FAILINGSTEP SUPERLINTER (run '/action/lib/linter.sh')"
233+
fi
234+
else
235+
echo
236+
echo "SUPERLINTER configured to be skipped"
237+
fi
238+
239+
217240
if [ $RETURNCODE -ne 0 ]
218241
then
219242
echo "Failing steps: ${FAILINGSTEP}"

0 commit comments

Comments
 (0)