Skip to content

Commit 7939f1c

Browse files
authored
[CI][CodeStyle] Add a new workflow to check code style (#1552)
1 parent 55e4c19 commit 7939f1c

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Code Style Check
2+
3+
on:
4+
pull_request:
5+
branches: [ "develop" ]
6+
7+
jobs:
8+
pre-commit:
9+
name: pre-commit
10+
runs-on: ubuntu-latest
11+
env:
12+
PR_ID: ${{ github.event.pull_request.number }}
13+
BRANCH: develop
14+
15+
steps:
16+
- name: Checkout Base Branch
17+
uses: actions/checkout@v4
18+
with:
19+
ref: ${{ github.event.pull_request.base.ref }}
20+
fetch-depth: 1000
21+
22+
- name: Merge PR
23+
run: |
24+
git config --global user.name "Paddle2onnxCI"
25+
git config --global user.email "[email protected]"
26+
git fetch origin pull/${PR_ID}/merge
27+
git checkout -b test FETCH_HEAD
28+
29+
- name: Setup Python3.12
30+
uses: actions/setup-python@v5
31+
with:
32+
python-version: '3.12'
33+
cache: 'pip'
34+
35+
- name: Install Dependencies
36+
run: |
37+
pip install pre-commit==2.17.0 cpplint==1.6.0 clang-format==13.0.0
38+
39+
- name: Run Pre-commit
40+
env:
41+
SKIP_CLANG_TIDY_CHECK: "ON"
42+
run: |
43+
set +e
44+
bash -x tools/codestyle/pre_commit.sh;EXCODE=$?
45+
exit $EXCODE

tools/codestyle/pre_commit.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
set +x
16+
17+
# use pre-commit 2.17
18+
if ! [[ $(pre-commit --version) == *"2.17.0"* ]]; then
19+
pip install pre-commit==2.17.0 1>nul
20+
fi
21+
22+
# Install clang-format before git commit to avoid repeat installation due to
23+
# pre-commit multi-thread running.
24+
readonly VERSION="13.0.0"
25+
version=$(clang-format -version)
26+
if ! [[ $(python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1$2}') -ge 36 ]]; then
27+
echo "clang-format installation by pip need python version great equal 3.6,
28+
please change the default python to higher version."
29+
exit 1
30+
fi
31+
32+
diff_files=$(git diff --name-only --diff-filter=ACMR ${BRANCH})
33+
num_diff_files=$(echo "$diff_files" | wc -l)
34+
echo -e "diff files between pr and ${BRANCH}:\n${diff_files}"
35+
36+
echo "Checking code style by pre-commit ..."
37+
pre-commit run --files ${diff_files};check_error=$?
38+
39+
if test ! -z "$(git diff)"; then
40+
echo -e '\n************************************************************************************'
41+
echo -e "These files have been formatted by code format hook. You should use pre-commit to \
42+
format them before git push."
43+
echo -e '************************************************************************************\n'
44+
git diff 2>&1
45+
fi
46+
47+
echo -e '\n************************************************************************************'
48+
if [ ${check_error} != 0 ];then
49+
echo "Your PR code style check failed."
50+
echo "Please install pre-commit locally and set up git hook scripts:"
51+
echo ""
52+
echo " pip install pre-commit==2.17.0"
53+
echo " pre-commit install"
54+
echo ""
55+
if [[ $num_diff_files -le 100 ]];then
56+
echo "Then, run pre-commit to check codestyle issues in your PR:"
57+
echo ""
58+
echo " pre-commit run --files" $(echo ${diff_files} | tr "\n" " ")
59+
echo ""
60+
fi
61+
echo "For more information, please refer to our codestyle check guide:"
62+
echo "https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/git_guides/codestyle_check_guide_cn.html"
63+
else
64+
echo "Your PR code style check passed."
65+
fi
66+
echo -e '************************************************************************************\n'
67+
68+
exit ${check_error}

0 commit comments

Comments
 (0)