-
Notifications
You must be signed in to change notification settings - Fork 1.3k
98 lines (87 loc) · 3.26 KB
/
code_analysis.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
name: code analysis
on: pull_request
# push:
# branches: [ $default-branch ]
# pull_request:
# branches: [ $default-branch ]
permissions:
contents: read
jobs:
clang-format:
# For any event that is not a PR, the CI will always run. In PRs, the CI
# can be skipped if the tag [skip-ci] is written in the title.
if: |
(github.repository_owner == 'root-project' && github.event_name != 'pull_request') ||
(github.event_name == 'pull_request' && !contains(github.event.pull_request.title, '[skip-ci]'))
runs-on: ubuntu-latest
env:
TRAVIS_BRANCH: ${{ github.base_ref }}
TRAVIS_PULL_REQUEST_BRANCH: ${{ github.head_ref }}
BASE_COMMIT: ${{ github.event.pull_request.base.sha }}
steps:
- uses: actions/checkout@v4
- name: Fetch base sha
run: git fetch --depth=1 origin +${{github.event.pull_request.base.sha}}:origin/base_sha
- name: install clang-format
run: sudo apt-get install -y clang-format
- name: run clang-format script
run: .ci/format_script.sh
ruff:
if: |
(github.repository_owner == 'root-project' && github.event_name != 'pull_request') ||
(github.event_name == 'pull_request' && !contains(github.event.pull_request.title, '[skip-ci]'))
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get the list of changed files
id: diff
run: |
git fetch --depth=1 origin $GITHUB_BASE_REF
git diff --name-only origin/$GITHUB_BASE_REF > changed_files.txt
- name: Install ruff
uses: astral-sh/ruff-action@v3
with:
args: "--version"
- name: Lint code
run: |
files=$(cat changed_files.txt | grep '\.py$' || echo "")
if [ -n "$files" ]; then
echo "$files" | xargs ruff check --diff || true
echo "$files" | xargs ruff check
else
echo "No python files to lint"
fi
- name: Format code
if: always()
run: |
files=$(cat changed_files.txt | grep '\.py$' || echo "")
if [ -n "$files" ]; then
diff_command=""
apply_command=""
for file in $files; do
while IFS=- read -r start length; do
[ -z "$start" ] && continue
length=${length:-1}
# Skip invalid ranges
if [ "$start" -eq 0 ] || [ "$length" -eq 0 ]; then
continue
fi
end=$((start + length))
diff_command+="ruff format --diff --range $start-$end $file && "
apply_command+="ruff format --range $start-$end $file && "
done < <(git diff --unified=0 origin/$GITHUB_BASE_REF "$file" | grep '^@@' | sed -E 's/^@@ -[0-9]+(,[0-9]+)? \+([0-9]+)(,([0-9]+))? @@.*/\2-\4/')
done
if [ -n "$diff_command" ]; then
diff_command=${diff_command% && }
if ! eval "$diff_command"; then
apply_command=${apply_command% && }
echo -e "::error::Formatting failed. To apply the changes locally, run the following command:\n$apply_command"
exit 123
fi
else
echo "No ranges detected to format."
fi
else
echo "No python files to format"
fi