Skip to content

Commit 6ec9d0e

Browse files
committed
[ci] Add ruff python linter and formatter to the code analysis workflow
1 parent cee0ce2 commit 6ec9d0e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

.github/workflows/code_analysis.yml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: clang-tools code analysis
1+
name: code analysis
22

33
on: pull_request
44
# push:
@@ -30,3 +30,44 @@ jobs:
3030
run: sudo apt-get install -y clang-format
3131
- name: run clang-format script
3232
run: .ci/format_script.sh
33+
34+
ruff:
35+
if: |
36+
(github.repository_owner == 'root-project' && github.event_name != 'pull_request') ||
37+
(github.event_name == 'pull_request' && !contains(github.event.pull_request.title, '[skip-ci]'))
38+
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Checkout code
42+
uses: actions/checkout@v4
43+
44+
- name: Get the list of changed files
45+
id: diff
46+
run: |
47+
git fetch --depth=1 origin $GITHUB_BASE_REF
48+
git diff --name-only origin/$GITHUB_BASE_REF > changed_files.txt
49+
50+
- name: Install ruff
51+
uses: astral-sh/ruff-action@v3
52+
with:
53+
args: "--version"
54+
55+
- name: Lint code
56+
run: |
57+
files=$(cat changed_files.txt | grep '\.py$' || echo "")
58+
if [ -n "$files" ]; then
59+
echo "$files" | xargs ruff check --diff || true
60+
echo "$files" | xargs ruff check
61+
else
62+
echo "No python files to lint"
63+
fi
64+
65+
- name: Format code
66+
if: always()
67+
run: |
68+
files=$(cat changed_files.txt | grep '\.py$' || echo "")
69+
if [ -n "$files" ]; then
70+
echo "$files" | xargs ruff format --diff
71+
else
72+
echo "No python files to format"
73+
fi

ruff.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
line-length = 120
2+
3+
[lint]
4+
select = [
5+
# Pyflakes
6+
"F",
7+
# Pycodestyle
8+
"E4",
9+
"E7",
10+
"E9",
11+
# isort
12+
"I001"
13+
]

0 commit comments

Comments
 (0)