Repository navigation
113 lines (103 loc) · 3.15 KB
/
Copy pathformat-code.yml
File metadata and controls
113 lines (103 loc) · 3.15 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
name: Format Code
on:
workflow_dispatch:
push:
branches:
- main
- develop
paths:
- '**.h'
- '**.hpp'
- '**.cpp'
- '**.c'
- '**.ino'
pull_request:
# Run when a new PR is opened
types: [opened]
branches:
- main
- develop
paths:
- '**.h'
- '**.hpp'
- '**.cpp'
- '**.c'
- '**.ino'
create:
# Run when a tag is created
ref_type: tag
release:
# Run when a new release is created/published
types: [created, published]
jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install astyle
run: sudo apt-get update && sudo apt-get install -y astyle
- name: Run astyle formatter
run: |
# Enable recursive globbing and make non-matching globs expand to empty
shopt -s globstar nullglob
files=(**/*.h **/*.hpp **/*.cpp **/*.c **/*.ino)
if [ ${#files[@]} -eq 0 ]; then
echo "No source files found, skipping astyle"
exit 0
fi
astyle \
--style=allman \
--attach-namespaces \
--attach-inlines \
--attach-extern-c \
--indent-modifiers \
--indent-switches \
--indent-cases \
--indent-namespaces \
--indent-labels \
--indent-col1-comments \
--pad-oper \
--pad-comma \
--pad-header \
--unpad-paren \
--align-pointer=middle \
--align-reference=middle \
--add-brackets \
--mode=c \
--align-method-colon \
--suffix=none \
--lineend=linux \
"${files[@]}"
- name: Check for changes
id: verify-changed-files
run: |
if git diff --quiet; then
echo "changed=false" >> $GITHUB_OUTPUT
else
echo "changed=true" >> $GITHUB_OUTPUT
fi
- name: Commit formatted code
if: steps.verify-changed-files.outputs.changed == 'true' && github.event_name == 'push'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add -A
git commit -m "Formatted code"
git push
- name: Create PR comment on formatting needed
if: steps.verify-changed-files.outputs.changed == 'true' && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '⚠️ Code formatting issues detected. Please run `astyle` locally with the provided configuration and push the formatted code.'
})
- name: Fail if formatting needed on PR
if: steps.verify-changed-files.outputs.changed == 'true' && github.event_name == 'pull_request'
run: exit 1