1
1
name : pre-commit
2
2
on :
3
3
workflow_call :
4
+ inputs :
5
+ full_precommit :
6
+ description : Defines whether a full pre-commit scan should be performed (cli option -a) or not
7
+ type : boolean
8
+ required : false
9
+ default : true
4
10
jobs :
5
11
setup :
6
12
name : Setup
7
- runs-on : ubuntu-20 .04
13
+ runs-on : ubuntu-22 .04
8
14
steps :
9
15
- name : Checkout
10
- uses : actions/checkout@v3
16
+ uses : actions/checkout@v4
11
17
- name : Get hooks
12
18
id : hooks
13
19
run : echo "hooks=$(yq e '[.repos[].hooks[].id] | @json' .pre-commit-config.yaml)" >> $GITHUB_OUTPUT
14
20
outputs :
15
21
hooks : ${{ steps.hooks.outputs.hooks }}
16
22
hook :
17
23
name : Run hook
18
- runs-on : ubuntu-20 .04
24
+ runs-on : ubuntu-22 .04
19
25
needs : setup
20
26
strategy :
21
27
matrix :
22
28
hook : ${{ fromJSON(needs.setup.outputs.hooks) }}
23
29
fail-fast : false
24
30
steps :
25
31
- name : Checkout
26
- uses : actions/checkout@v3
32
+ uses : actions/checkout@v4
27
33
- name : Cache asdf
28
34
id : cache
29
35
uses : actions/cache@v3
@@ -32,17 +38,72 @@ jobs:
32
38
key : ${{ runner.os }}-${{ hashFiles('**/.tool-versions') }}
33
39
- name : Install
34
40
if : steps.cache.outputs.cache-hit != 'true'
35
- uses : asdf-vm/actions/install@v1.1 .0
41
+ uses : asdf-vm/actions/install@v2.2 .0
36
42
with :
37
43
# Normally, this action would just install the versions of the .tool-versions file in the root directory of the repository.
38
44
# As we also have .tool-versions files in subdirectories, pre-commit hooks would fail if versions in these files differ from
39
45
# the ones specified in the .tool-versions file in the root directory of this repository.
40
46
before_install : find . -name ".tool-versions" -exec bash -c 'while read line; do asdf install ${line}; done < ${0}' {} \;
41
- - name : Run ${{ matrix.hook }} hook
47
+ - name : Run ${{ matrix.hook }} hook (full repo scan)
48
+ if : inputs.full_precommit
42
49
# The asdf setup is required when restoring from cache
43
50
run : |
44
51
. /home/runner/.asdf/asdf.sh
45
- pre-commit run -a ${{ matrix.hook }}
52
+ pre-commit run -a ${{ matrix.hook }}
53
+ - name : Run ${{ matrix.hook }} hook (scan difference to default branch)
54
+ if : ${{ ! inputs.full_precommit }}
55
+ #
56
+ # The asdf setup is required when restoring from cache
57
+ run : |
58
+ . /home/runner/.asdf/asdf.sh
59
+
60
+ # extract the delta
61
+ # get the GITs default branch name
62
+ default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
63
+ # get the branch nam,e of the current branch we want to check
64
+ pr_branch=$(git branch | grep '^*' | awk '{print $2}')
65
+ # generate a uniq name for a temporary local branch
66
+ test_branch="pre-commit/${current_branch}"
67
+ # checkout the default (main) branch
68
+ git checkout ${default_branch}
69
+ # validate that it has the actual state
70
+ git pull
71
+ # create a new, temporary branch locally
72
+ git checkout -b ${test_branch}
73
+ # merge the content from the branch we want to check
74
+ git merge --no-ff -m "get changes from PR branch" ${pr_branch}
75
+ # decrease HEAD revision for 1 version in past, so we have the file changes
76
+ git reset HEAD~1
77
+ # add the changes, so we get the differences into the commit
78
+ git add .
79
+
80
+ # validate if there are only removals inside the commit
81
+ removals_only='false'
82
+ if [ $(git status -s | awk '{print $1}' | sort | uniq | egrep -v 'D' | wc -l) -eq 0 ]; then
83
+ removals_only='true'
84
+ fi
85
+
86
+ # validate if the current matrix job is a checkov scan
87
+ is_checkov='false'
88
+ if [ ! -z "echo ${{ matrix.hook }} | grep -i checkov" ]; then
89
+ is_checkov='true'
90
+ fi
91
+
92
+ # if there are only removals, checkov will fail - we will skip all checkov checks on removals only
93
+ if [ "${is_checkov}" == "true" -a "${removals_only}" == "true" ]; then
94
+ echo "INFO: Skip pre-commit run ${{ matrix.hook }}, because there are only removal of files and checkov would fail here!"
95
+ else
96
+ pre-commit run ${{ matrix.hook }}
97
+ RC="${?}"
98
+ fi
99
+ # go back to the initial PR branch
100
+ git checkout ${pr_branch}
101
+ # remove the temporary branch
102
+ git branch -D ${test_branch}
103
+
104
+ # exit step with RC of the pre-commit run
105
+ exit ${RC}
106
+
46
107
- name : Add summary on success
47
108
if : ${{ success() }}
48
109
run : |
0 commit comments