Skip to content

Commit

Permalink
CI: use a minimal local clone to get changes in patch
Browse files Browse the repository at this point in the history
  This aims at circumventing limit of 300 changed files returned by GH API
  • Loading branch information
0penBrain committed May 29, 2023
1 parent 413f9fe commit 587ade5
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions .github/workflows/sub_prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ jobs:
run: |
mkdir -p ${{ env.logdir }}
mkdir -p ${{ env.reportdir }}
commitCnt=0
touch ${{ env.logdir }}changedFiles.lst ${{ env.logdir }}changedCppFiles.lst ${{ env.logdir }}changedPythonFiles.lst
echo "reportFile=${{ env.reportfilename }}" >> $GITHUB_OUTPUT
- name: Determine base and head SHA in case of PR
Expand All @@ -102,7 +101,7 @@ jobs:
# Exit the step with appropriate code
if [ $dateDiff -gt ${{ inputs.maxRebaseHours }} ]
then
echo -n ":warning: Pull request should be rebased" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
echo ":warning: Pull request should be rebased" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
exit 1
fi
- name: Determine base and head SHA in case of push
Expand All @@ -121,20 +120,23 @@ jobs:
echo "Base SHA is $baseSha, Head SHA is $headSha" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
echo "baseSha=$baseSha" >> $GITHUB_ENV
fi
- name: Get compare between head and base
if: env.isPR == 'true' || env.isPush == 'true'
- name: Prepare minimal local clone for subsequent operations
run: |
echo "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/compare/$baseSha...$headSha"
curl -H "Accept: application/vnd.github+json" -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/compare/$baseSha...$headSha > ${{ env.logdir }}compare.json
set -x
git clone --filter=blob:none --no-checkout $GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git
- name: Get number of commits in the changeset
if: env.isPR == 'true' || env.isPush == 'true'
run: |
commitCnt=$(jq -re '.ahead_by' ${{ env.logdir }}compare.json)
set -x
cd $(basename "$GITHUB_REPOSITORY")
commitCnt=$(git log $baseSha..$headSha --oneline | wc -l)
echo "Changeset is composed of $commitCnt commit(s)" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
- name: Get files modified in changeset
if: env.isPR == 'true' || env.isPush == 'true'
run: |
jq -r '.files[] | if .status != "removed" then .filename else empty end' ${{ env.logdir }}compare.json > ${{ env.logdir }}changedFiles.lst
set -x
cd $(basename "$GITHUB_REPOSITORY")
git diff $baseSha...$headSha --name-only --diff-filter=d | cut -f 1 > ${{ env.logdir }}changedFiles.lst
grep -E '\.(py|py3)"' ${{ env.logdir }}changedFiles.lst > ${{ env.logdir }}changedPythonFiles.lst || true
grep -E '\.(c|c\+\+|cc|cpp|cu|cuh|cxx|h|h\+\+|hh|hpp|hxx)"' ${{ env.logdir }}changedFiles.lst > ${{ env.logdir }}changedCppFiles.lst || true
# Write the report
Expand All @@ -148,35 +150,18 @@ jobs:
run: |
set -x
sudo apt-get install -y --no-install-recommends pre-commit
BASEADDR="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/raw/$headSha"
mkdir precommit
cd precommit
git init
wget $BASEADDR/.pre-commit-config.yaml
mkdir files
pushd files
for file in $(cat ${{ env.logdir }}changedFiles.lst)
do
if [ $(dirname "$file") != '.' ]
then
mkdir -p $(dirname $file)
fi
wget -O $file $BASEADDR/$file
done
popd
pre-commit run --files $(find files/ -type f)
cd $(basename "$GITHUB_REPOSITORY")
IFS=$'\n' files=($(cat ${{ env.logdir }}changedFiles.lst))
git config gc.auto 0
git fetch --depth=1 origin $GITHUB_REF
git checkout $headSha -- .pre-commit-config.yaml ${files[@]}
pre-commit run -a
- name: Transmitting outputs
id: Output
run: |
echo "changedFiles<<EOD" >> $GITHUB_OUTPUT
cat ${{ env.logdir }}changedFiles.lst >> $GITHUB_OUTPUT
echo "EOD" >> $GITHUB_OUTPUT
echo "changedPythonFiles<<EOD" >> $GITHUB_OUTPUT
cat ${{ env.logdir }}changedPythonFiles.lst >> $GITHUB_OUTPUT
echo "EOD" >> $GITHUB_OUTPUT
echo "changedCppFiles<<EOD" >> $GITHUB_OUTPUT
cat ${{ env.logdir }}changedCppFiles.lst >> $GITHUB_OUTPUT
echo "EOD" >> $GITHUB_OUTPUT
echo "changedFiles=$(cat ${{ env.logdir }}changedFiles.lst | xargs -I{file} echo -n "\"{file}\" ")" >> $GITHUB_OUTPUT
echo "changedPythonFiles=$(cat ${{ env.logdir }}changedPythonFiles.lst | xargs -I{file} echo -n "\"{file}\" ")" >> $GITHUB_OUTPUT
echo "changedCppFiles=$(cat ${{ env.logdir }}changedCppFiles.lst | xargs -I{file} echo -n "\"{file}\" ")" >> $GITHUB_OUTPUT
- name: Upload logs
if: always()
uses: actions/upload-artifact@v3
Expand Down

0 comments on commit 587ade5

Please sign in to comment.