Skip to content

Commit 45a1901

Browse files
authored
Add Precommit and flake8 (#3287)
### What I did add precommit and flake8 to prevent syntax warning in python, e.g. sonic-net/sonic-buildimage#18401 #### How I did it - initially, this check will be optional to verify its functionality, allowing developers to proceed even if it fails. - if it performs as expected, this check will become mandatory in the future. - use flake8 v4.0.1 in order to use diff only for PRs (flake8 with diff only is deprecated in v6.0.0 PyCQA/flake8#1749 #### How to verify it https://dev.azure.com/mssonic/build/_build/results?buildId=530004&view=logs&jobId=0cb31af1-9c12-5e3c-148e-856fdcff3a02
1 parent 4a8ffc8 commit 45a1901

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
steps:
2+
- checkout: self
3+
clean: true
4+
displayName: 'checkout sonic-utilities repo'
5+
6+
- script: |
7+
set -x
8+
sudo pip install pre-commit
9+
pre-commit install-hooks
10+
displayName: 'Prepare pre-commit check'
11+
12+
- script: |
13+
# Run pre-commit check and capture the output
14+
out=`pre-commit run --color never --from-ref HEAD^ --to-ref HEAD 2>&1`
15+
RC=$?
16+
if [[ $RC -ne 0 ]]; then
17+
echo -e "The [pre-commit](http://pre-commit.com/) check detected issues in the files touched by this pull request.\n\
18+
The pre-commit check is a mandatory check, please fix detected issues.\n\
19+
\n\
20+
To run the pre-commit checks locally, you can follow below steps:\n\
21+
1. Ensure that default python is python3.\n\
22+
2. Ensure that the 'pre-commit' package is installed:\n\
23+
sudo pip install pre-commit\n\
24+
3. Go to repository root folder\n\
25+
4. Install the pre-commit hooks:\n\
26+
pre-commit install\n\
27+
5. Use pre-commit to check staged file:\n\
28+
pre-commit\n\
29+
6. Alternatively, you can check committed files using:\n\
30+
pre-commit run --from-ref <commit_id> --to-ref <commit_id>\n"
31+
fi
32+
echo "Pre-commit check results:"
33+
echo "$out"
34+
exit $RC
35+
displayName: 'Run pre-commit check'

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/PyCQA/flake8
5+
rev: 4.0.1
6+
hooks:
7+
- id: flake8
8+
entry: bash -c 'git diff HEAD^ HEAD -U0 -- "$@" | flake8 --diff "$@"' --
9+
args: ["--max-line-length=120"]

azure-pipelines.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ variables:
2121
value: $(Build.SourceBranchName)
2222

2323
stages:
24+
- stage: Pretest
25+
jobs:
26+
- job: static_analysis
27+
displayName: "Static Analysis"
28+
timeoutInMinutes: 10
29+
continueOnError: true
30+
pool: ubuntu-20.04
31+
steps:
32+
- template: .azure-pipelines/pre-commit-check.yml
33+
2434
- stage: Build
2535

2636
jobs:

0 commit comments

Comments
 (0)