Skip to content

Commit ec66be3

Browse files
committed
ci: make clang-format check mandatory in CI
Signed-off-by: Jan Kraemer <jan.kraemer@vector.com>
1 parent a94b238 commit ec66be3

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

.github/workflows/sil-kit-ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ jobs:
4646
4747
check-formatting:
4848
name: Format checks for SIL Kit sources
49-
runs-on: ubuntu-22.04
50-
container:
51-
image: ghcr.io/vectorgrp/sil-kit-docker-build/sil-kit-ci-public-runner:main
49+
runs-on: ubuntu-24.04
5250
steps:
5351
- uses: actions/checkout@v4
5452
with:
5553
submodules: false
5654
- name: Check Formatting
5755
id: formatting-check
5856
run: |
59-
python3 ./SilKit/ci/check_formatting.py
57+
python3 ./SilKit/ci/check_formatting.py --repo ${{ github.repository }} --pr ${{ github.event.number }} --dryrun
6058
shell: bash
6159

6260
clang14-tsan:

SilKit/ci/check_formatting.py

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
#!/usr/bin/env python3
1+
#!/ usr / bin / env python3
22

3-
# SPDX-FileCopyrightText: 2024 Vector Informatik GmbH
3+
#SPDX - FileCopyrightText : 2024 Vector Informatik GmbH
44
#
5-
# SPDX-License-Identifier: MIT
5+
#SPDX - License - Identifier : MIT
66
from pathlib import Path
77
from shutil import which
88

99
import argparse
1010
import os
1111
import re
12+
import requests
1213
import subprocess
1314
import sys
1415

@@ -34,6 +35,39 @@ def die(status, fmt, *args):
3435
CLANG_VERSION = "18"
3536
CLANG_FORMAT = "clang-format-" + CLANG_VERSION
3637

38+
def get_pr_files(pr: str, repo: str):
39+
40+
url = 'https://api.github.com/repos/' + repo + '/pulls/' + pr + '/files'
41+
42+
log("Checking at {}".format(url))
43+
44+
r = requests.get(url)
45+
46+
max_page = 1
47+
if 'link' in r.headers:
48+
last_page_link_header = r.headers["link"].split(',')[-1].split(';')[0]
49+
page_reg = re.compile('.+page=([0-9]+)>')
50+
page = re.match(page_reg, last_page_link_header).group(1)
51+
52+
if page.isnumeric():
53+
max_page = int(page)
54+
55+
files = []
56+
for fileObject in r.json():
57+
58+
files.append(fileObject["filename"])
59+
60+
if max_page > 1:
61+
print(f"Need to check {max_page} pages")
62+
for page in range(2, max_page+1):
63+
page_url = url + f'?page={page}'
64+
r = requests.get(page_url)
65+
for fileObject in r.json():
66+
files.append(fileObject["filename"])
67+
68+
print(f"Found {len(files)} files")
69+
return files
70+
3771
def main():
3872
if which(CLANG_FORMAT) is None:
3973
warn("No {} found!", CLANG_FORMAT)
@@ -44,14 +78,14 @@ def main():
4478
description="Run clang tidy on select source files")
4579

4680
parser.add_argument(
47-
'--git_commit', help="The Git commit to be reformatted", type=str)
81+
'--pr', help="The Github PR to be reformatted", type=str)
4882
parser.add_argument(
49-
'--dry_run', help="Only produce warnings", action='store_true')
83+
'--repo', help="The Github repo to be reformatted", type=str)
5084
parser.add_argument(
51-
'--werror', help="Treat clang-format warnings as errors", action='store_true')
85+
'--dryrun', help="Only produce warnings", action='store_true')
5286
args = parser.parse_args()
5387

54-
# Check for supported clang-format version
88+
#Check for supported clang - format version
5589
format_version = subprocess.run([CLANG_FORMAT, '--version'], capture_output=True, encoding='utf-8')
5690

5791
version_reg = re.compile('^.* clang-format version (\d+)\.(\d+)\.(\d+).*')
@@ -74,37 +108,43 @@ def main():
74108
totalFiles = 0
75109
totalWarnings = 0
76110

77-
# Check the Formatting!
111+
#Check the Formatting !
78112
files = []
79113

80-
if args.git_commit is not None and args.git_commit != "":
81-
commit_files = subprocess.run(['git', 'diff-tree', '--no-commit-id', '--name-only', '-r', args.git_commit],
82-
capture_output=True, encoding='utf8')
83-
files = commit_files.stdout.splitlines()
114+
if args.pr is not None and args.repo is not None:
115+
files = get_pr_files(args.pr, args.repo)
84116
else:
85117
for directory in dirs:
86118
rootPath = Path("./" + directory)
87119
for ext in fileExtensions:
88120
files = files + (sorted(rootPath.rglob(ext)))
89121

90-
dry_run = ''
91-
if args.dry_run:
92-
dry_run = "--dry-run"
93-
print(dry_run)
122+
clang_format_cmd = [CLANG_FORMAT]
123+
if args.dryrun:
124+
clang_format_cmd.append('--Werror')
125+
clang_format_cmd.append('--dry-run')
126+
127+
clang_format_cmd = clang_format_cmd + ['-i', '--style=file']
128+
print(clang_format_cmd)
94129
for file in files:
95-
totalFiles = totalFiles + 1
96-
formatResult = subprocess.run([CLANG_FORMAT, '--Werror', dry_run, '-i', '--style=file', file], capture_output=True, encoding='utf-8')
97-
if formatResult.returncode != 0:
98-
formattingCorrect = False
99-
totalWarnings = totalWarnings + 1
100-
warn("File not formatted correctly: {}", file)
130+
131+
if any(p in file for p in dirs):
132+
133+
totalFiles = totalFiles + 1
134+
formatResult = subprocess.run(clang_format_cmd + [file], capture_output=True, encoding='utf-8')
135+
if formatResult.returncode != 0:
136+
formattingCorrect = False
137+
totalWarnings = totalWarnings + 1
138+
warn("File not formatted correctly: {}", file)
139+
else:
140+
print(f"Skip: {file}")
101141

102142
info("{} files checked, {} produced a warning!", totalFiles, totalWarnings)
103143
if formattingCorrect is False:
104-
# Only warn for now
144+
#Only warn for now
105145
warn("Formatting for one or more SilKit source code files not correct.!")
106146
warn("Please format your source code properly using the SilKit .clang-format config file!")
107-
ret_code = 64 if args.werror else 0
147+
ret_code = 64 if args.dryrun else 0
108148
exit(ret_code)
109149

110150
info("All source code files properly formatted!")

0 commit comments

Comments
 (0)