Skip to content

Commit fef5b11

Browse files
committed
Upgrade versions and implement option to only check deltas
1 parent 87df7cd commit fef5b11

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

.github/workflows/pre-commit.yaml

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
name: pre-commit
22
on:
33
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
410
jobs:
511
setup:
612
name: Setup
7-
runs-on: ubuntu-20.04
13+
runs-on: ubuntu-22.04
814
steps:
915
- name: Checkout
10-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
1117
- name: Get hooks
1218
id: hooks
1319
run: echo "hooks=$(yq e '[.repos[].hooks[].id] | @json' .pre-commit-config.yaml)" >> $GITHUB_OUTPUT
1420
outputs:
1521
hooks: ${{ steps.hooks.outputs.hooks }}
1622
hook:
1723
name: Run hook
18-
runs-on: ubuntu-20.04
24+
runs-on: ubuntu-22.04
1925
needs: setup
2026
strategy:
2127
matrix:
2228
hook: ${{ fromJSON(needs.setup.outputs.hooks) }}
2329
fail-fast: false
2430
steps:
2531
- name: Checkout
26-
uses: actions/checkout@v3
32+
uses: actions/checkout@v4
2733
- name: Cache asdf
2834
id: cache
2935
uses: actions/cache@v3
@@ -32,17 +38,72 @@ jobs:
3238
key: ${{ runner.os }}-${{ hashFiles('**/.tool-versions') }}
3339
- name: Install
3440
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
3642
with:
3743
# Normally, this action would just install the versions of the .tool-versions file in the root directory of the repository.
3844
# As we also have .tool-versions files in subdirectories, pre-commit hooks would fail if versions in these files differ from
3945
# the ones specified in the .tool-versions file in the root directory of this repository.
4046
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
4249
# The asdf setup is required when restoring from cache
4350
run: |
4451
. /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+
46107
- name: Add summary on success
47108
if: ${{ success() }}
48109
run: |

0 commit comments

Comments
 (0)