Skip to content

Commit f5ee8bb

Browse files
authored
Bugfix/delta branch process (#22)
* More debug output * Try to identify default branch via variable * Try to identify PR branch via variable * Try to identify PR branch via variable * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * add a DEBUG step to inspect environment * Fixing usage of a separate workdir * Testing delta checks * Testing delta checks * Testing delta checks * Testing delta checks * Testing delta checks * Testing delta checks * Testing delta checks * Testing delta checks * Testing delta checks
1 parent 1e7a84c commit f5ee8bb

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

.github/workflows/pre-commit.yaml

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,48 +58,68 @@ jobs:
5858
. /home/runner/.asdf/asdf.sh
5959
6060
# extract the delta
61+
git_url=${{ github.event.pull_request.base.repo.clone_url }}
6162
# 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}"
6365
# 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+
7790
# 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
7996
8097
# validate if there are only removals inside the commit
8198
removals_only='false'
8299
if [ $(git status -s | awk '{print $1}' | sort | uniq | egrep -v 'D' | wc -l) -eq 0 ]; then
83100
removals_only='true'
101+
echo "DEBUG: This branch contains only removals of files."
84102
fi
85103
86104
# validate if the current matrix job is a checkov scan
87105
is_checkov='false'
88-
if [ ! -z "echo ${{ matrix.hook }} | grep -i checkov" ]; then
106+
if [ ! -z "$(echo ${{ matrix.hook }} | grep -i checkov)" ]; then
89107
is_checkov='true'
108+
echo "DEBUG: Detecting a checkov scan (${{ matrix.hook }})."
90109
fi
91110
92111
# if there are only removals, checkov will fail - we will skip all checkov checks on removals only
93112
if [ "${is_checkov}" == "true" -a "${removals_only}" == "true" ]; then
94113
echo "INFO: Skip pre-commit run ${{ matrix.hook }}, because there are only removal of files and checkov would fail here!"
114+
RC=0
95115
else
96116
pre-commit run ${{ matrix.hook }}
97117
RC="${?}"
98118
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
103123
104124
# exit step with RC of the pre-commit run
105125
exit ${RC}

0 commit comments

Comments
 (0)