Skip to content

Commit 005bb0d

Browse files
authored
Add ci to run pre-commit through github actions. (#32)
1 parent 5a90075 commit 005bb0d

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: "Check bypass"
2+
description: "A custom action to encapsulate PFCCLab/ci-bypass"
3+
inputs:
4+
github-token:
5+
description: "GitHub token"
6+
required: true
7+
workflow-name:
8+
description: "Workflow name"
9+
required: true
10+
outputs:
11+
can-skip:
12+
description: "Whether the workflow can be skipped."
13+
value: ${{ steps.check-bypass.outputs.can-skip }}
14+
15+
runs:
16+
using: "composite"
17+
steps:
18+
- id: check-bypass
19+
name: Check Bypass
20+
env:
21+
CI_TEAM_MEMBERS: '["SigureMo", "risemeup1", "tianshuo78520a", "0x3878f", "swgu98", "luotao1", "XieYunshen"]'
22+
uses: PFCCLab/ci-bypass@v1
23+
with:
24+
github-token: ${{ inputs.github-token }}
25+
non-pull-request-event-strategy: 'always-skipped'
26+
type: 'composite'
27+
composite-rule: |
28+
{
29+
"any": [
30+
{
31+
"type": "labeled",
32+
"label": ["skip-ci: ${{ inputs.workflow-name }}", "skip-ci: all"],
33+
"username": ${{ env.CI_TEAM_MEMBERS }}
34+
},
35+
{
36+
"type": "commented",
37+
"comment-pattern": [".*/skip-ci ${{ inputs.workflow-name }}.*", ".*/skip-ci all.*"],
38+
"username": ${{ env.CI_TEAM_MEMBERS }}
39+
}
40+
]
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Codestyle-Check
2+
3+
on:
4+
pull_request:
5+
branches: ["develop"]
6+
7+
jobs:
8+
pre-commit:
9+
name: Pre Commit
10+
if: ${{ github.repository_owner == 'PaddlePaddle' }}
11+
runs-on:
12+
group: APPROVAL
13+
env:
14+
PR_ID: ${{ github.event.pull_request.number }}
15+
BRANCH: develop
16+
17+
steps:
18+
- name: Cleanup
19+
run: |
20+
rm -rf * .[^.]*
21+
22+
- name: Checkout base repo
23+
uses: actions/checkout@v4
24+
with:
25+
ref: ${{ github.event.pull_request.base.ref }}
26+
fetch-depth: 1000
27+
28+
- name: Check bypass
29+
id: check-bypass
30+
uses: ./.github/actions/check-bypass
31+
with:
32+
github-token: ${{ secrets.GITHUB_TOKEN }}
33+
workflow-name: codestyle
34+
35+
- name: Merge PR to test branch
36+
if: steps.check-bypass.outputs.can-skip != 'true'
37+
run: |
38+
git fetch origin pull/${PR_ID}/merge
39+
git checkout -b test FETCH_HEAD
40+
41+
- name: Setup python3.12
42+
if: steps.check-bypass.outputs.can-skip != 'true'
43+
uses: actions/setup-python@v5
44+
with:
45+
python-version: '3.12'
46+
cache: 'pip'
47+
48+
- name: Install dependencies
49+
if: steps.check-bypass.outputs.can-skip != 'true'
50+
run: |
51+
pip install pre-commit==2.17.0 cpplint==1.6.0 clang-format==13.0.0
52+
53+
- name: Check pre-commit
54+
if: steps.check-bypass.outputs.can-skip != 'true'
55+
env:
56+
SKIP_CLANG_TIDY_CHECK: "ON"
57+
run: |
58+
set +e
59+
bash -x tools/codestyle/pre_commit.sh;EXCODE=$?
60+
exit $EXCODE

tools/codestyle/pre_commit.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (c) 2022 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+
diff_files=$(git diff --name-only --diff-filter=ACMR ${BRANCH})
23+
num_diff_files=$(echo "$diff_files" | wc -l)
24+
echo -e "diff files between pr and ${BRANCH}:\n${diff_files}"
25+
26+
echo "Checking code style by pre-commit ..."
27+
pre-commit run --files ${diff_files};check_error=$?
28+
29+
if test ! -z "$(git diff)"; then
30+
echo -e '\n************************************************************************************'
31+
echo -e "These files have been formatted by code format hook. You should use pre-commit to \
32+
format them before git push."
33+
echo -e '************************************************************************************\n'
34+
git diff 2>&1
35+
fi
36+
37+
echo -e '\n************************************************************************************'
38+
if [ ${check_error} != 0 ];then
39+
echo "Your PR code style check failed."
40+
echo "Please install pre-commit locally and set up git hook scripts:"
41+
echo ""
42+
echo " pip install pre-commit==2.17.0"
43+
echo " pre-commit install"
44+
echo ""
45+
if [[ $num_diff_files -le 100 ]];then
46+
echo "Then, run pre-commit to check codestyle issues in your PR:"
47+
echo ""
48+
echo " pre-commit run --files" $(echo ${diff_files} | tr "\n" " ")
49+
echo ""
50+
fi
51+
echo "For more information, please refer to our codestyle check guide:"
52+
echo "https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/git_guides/codestyle_check_guide_cn.html"
53+
else
54+
echo "Your PR code style check passed."
55+
fi
56+
echo -e '************************************************************************************\n'
57+
58+
exit ${check_error}

0 commit comments

Comments
 (0)