@@ -58,48 +58,68 @@ jobs:
58
58
. /home/runner/.asdf/asdf.sh
59
59
60
60
# extract the delta
61
+ git_url=${{ github.event.pull_request.base.repo.clone_url }}
61
62
# get the GITs default branch name
62
- default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
63
+ default_branch=${{ github.event.repository.default_branch }}
64
+ echo "DEBUG: The repos default branch name is: ${default_branch}"
63
65
# 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
66
+ pr_branch=${{ github.event.pull_request.head.ref }}
67
+ echo "DEBUG: The current branch is: ${pr_branch}"
68
+
69
+ # Generate a new working direcory, based on the current GitHub workspace
70
+ BASEDIR=$(dirname ${GITHUB_WORKSPACE})
71
+ WORKDIR="${BASEDIR}/pre-commit-test"
72
+ mkdir -p ${WORKDIR} || exit 1
73
+ cd ${WORKDIR} || exit 1
74
+
75
+ # cloning the GIT repository
76
+ echo "DEBUG: Cloning ${{ github.event.pull_request.base.repo.clone_url }} in $(pwd)"
77
+ git clone ${{ github.event.pull_request.base.repo.clone_url }} . || exit 1
78
+ # get the revision of default branch
79
+ echo "DEBUG: Get revision of the default branch"
80
+ revision=$(git rev-parse origin/${default_branch})
81
+ echo "DEBUG: Revision of origin/${default_branch} is: ${revision}"
82
+
83
+ # Switch to the PR branch
84
+ echo "DEBUG: Switch to the pull request branch: ${pr_branch}"
85
+ git checkout ${pr_branch} || exit 1
86
+ # Reset the branch to default branch revision
87
+ echo "DEBUG Do a soft reset to revision: ${revision}"
88
+ git reset --soft ${revision} || exit 1
89
+
77
90
# add the changes, so we get the differences into the commit
78
- git add .
91
+ echo "DEBUG: Adding all changes to stage the delta"
92
+ git add . || exit 1
93
+ # output delta
94
+ echo "DEBUG: Staged delta is:"
95
+ git status -s || exit 1
79
96
80
97
# validate if there are only removals inside the commit
81
98
removals_only='false'
82
99
if [ $(git status -s | awk '{print $1}' | sort | uniq | egrep -v 'D' | wc -l) -eq 0 ]; then
83
100
removals_only='true'
101
+ echo "DEBUG: This branch contains only removals of files."
84
102
fi
85
103
86
104
# validate if the current matrix job is a checkov scan
87
105
is_checkov='false'
88
- if [ ! -z "echo ${{ matrix.hook }} | grep -i checkov" ]; then
106
+ if [ ! -z "$( echo ${{ matrix.hook }} | grep -i checkov) " ]; then
89
107
is_checkov='true'
108
+ echo "DEBUG: Detecting a checkov scan (${{ matrix.hook }})."
90
109
fi
91
110
92
111
# if there are only removals, checkov will fail - we will skip all checkov checks on removals only
93
112
if [ "${is_checkov}" == "true" -a "${removals_only}" == "true" ]; then
94
113
echo "INFO: Skip pre-commit run ${{ matrix.hook }}, because there are only removal of files and checkov would fail here!"
114
+ RC=0
95
115
else
96
116
pre-commit run ${{ matrix.hook }}
97
117
RC="${?}"
98
118
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}
119
+ # Go back to the GitHub workspace
120
+ cd ${GITHUB_WORKSPACE} || exit 1
121
+ # remove the rtemporare workdir
122
+ rm -rf ${WORKDIR} || exit 1
103
123
104
124
# exit step with RC of the pre-commit run
105
125
exit ${RC}
0 commit comments