-
Notifications
You must be signed in to change notification settings - Fork 79
64 lines (60 loc) · 2.25 KB
/
code_format.yml
File metadata and controls
64 lines (60 loc) · 2.25 KB
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
name: Run clang-format
on:
pull_request_target:
branches:
- '*'
permissions:
contents: write
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- uses: DoozyX/clang-format-lint-action@v0.18.1
with:
extensions: 'c,cpp,h,hpp'
clangFormatVersion: 18
inplace: True
- name: Detect formatting changes
id: changes
run: |
if git diff --quiet --exit-code; then
echo "has_changes=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "has_changes=true" >> "$GITHUB_OUTPUT"
{
echo 'changed_files<<EOF'
git diff --name-only
echo 'EOF'
} >> "$GITHUB_OUTPUT"
- name: Classify pull request source
id: pr_source
run: |
if [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ]; then
echo "is_same_repo=true" >> "$GITHUB_OUTPUT"
else
echo "is_same_repo=false" >> "$GITHUB_OUTPUT"
fi
- name: Annotate formatting changes
if: ${{ steps.changes.outputs.has_changes == 'true' }}
run: |
files='${{ steps.changes.outputs.changed_files }}'
files="${files//$'\n'/%0A}"
echo "::warning::clang-format updated these files:%0A$files"
- name: Commit and push formatting fixes
if: ${{ steps.changes.outputs.has_changes == 'true' && steps.pr_source.outputs.is_same_repo == 'true' }}
run: |
git config user.name "openastrotech-bot"
git config user.email "openastrotech-bot@users.noreply.github.com"
git add -A
git commit -m "style: apply clang-format fixes"
git push origin "HEAD:${{ github.event.pull_request.head.ref }}"
- name: Fail for forked pull requests with formatting changes
if: ${{ steps.changes.outputs.has_changes == 'true' && steps.pr_source.outputs.is_same_repo != 'true' }}
run: |
echo "Automatic formatting commits are only supported for branches in this repository." >&2
exit 1