Skip to content

Commit 4f8d97f

Browse files
committed
Added c-cpp-linter.yml
1 parent a199e79 commit 4f8d97f

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

.github/workflows/c-cpp-linter.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: c-cpp-linter
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
paths: ['**.c', '**.cpp', '**.h', '**.hpp', '**.cc', '**.hh', '**.cxx', '**.hxx']
7+
8+
jobs:
9+
c-cpp-linter:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Check out the repo
13+
uses: actions/checkout@v4.1.0
14+
15+
- uses: cpp-linter/cpp-linter-action@v2.6.1
16+
id: linter
17+
continue-on-error: false
18+
env:
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
with:
21+
style: file
22+
tidy-checks: ''
23+
thread-comments: true
24+
step-summary: true
25+
26+
- name: Fail checks
27+
if: steps.linter.outputs.checks-failed > 0
28+
run: |
29+
echo "Some files failed the linting checks!"
30+
echo "No. of checks failed: ${{ steps.linter.outputs.checks-failed }}"
31+
echo "Use the clang-format diffs to find more help"
32+
33+
- name: Setup Python
34+
if: steps.linter.outputs.checks-failed > 0
35+
uses: actions/setup-python@v4.7.1
36+
with:
37+
python-version: '3.12'
38+
39+
- name: Getting PR details
40+
if: steps.linter.outputs.checks-failed > 0
41+
run: |
42+
touch pr.json # creating empty file for paths
43+
gh pr view $PR_NUMBER --json files > pr.json # storing file paths
44+
touch res # creating empty file for final output
45+
env:
46+
PR_NUMBER: ${{ github.event.pull_request.number }}
47+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48+
49+
- name: Python script for clang diff
50+
if: steps.linter.outputs.checks-failed > 0
51+
uses: jannekem/run-python-script-action@v1
52+
with:
53+
script: |
54+
import os
55+
import json
56+
import sys
57+
with open('pr.json','r') as json_file:
58+
data = json.load(json_file)
59+
for file in data["files"]:
60+
path = file["path"]
61+
if os.path.exists(path):
62+
os.system('echo "" >> res')
63+
os.system(f'echo "Filename: {path}" >> res')
64+
os.system('echo "" >> res')
65+
os.system(f'clang-format-12 -style=Google {path} >> res')
66+
os.system('echo ---------------------------------------- >> res')
67+
68+
- name: Show diffs and exit
69+
if: steps.linter.outputs.checks-failed > 0
70+
run: |
71+
cat res
72+
exit 1

0 commit comments

Comments
 (0)