forked from ZedThree/clang-tidy-review
-
Notifications
You must be signed in to change notification settings - Fork 0
/
review.py
executable file
·822 lines (655 loc) · 25.8 KB
/
review.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#!/usr/bin/env python3
# clang-tidy review
# Copyright (c) 2020 Peter Hill
# SPDX-License-Identifier: MIT
# See LICENSE for more information
import argparse
import contextlib
import datetime
import itertools
import fnmatch
import json
import os
from operator import itemgetter
import pathlib
import pprint
import re
import requests
import subprocess
import textwrap
import unidiff
import yaml
from github import Github
from github.Requester import Requester
from github.PaginatedList import PaginatedList
from typing import List
BAD_CHARS_APT_PACKAGES_PATTERN = "[;&|($]"
DIFF_HEADER_LINE_LENGTH = 5
FIXES_FILE = "clang_tidy_review.yaml"
class PullRequest:
"""Add some convenience functions not in PyGithub"""
def __init__(self, repo: str, pr_number: int, token: str):
self.repo = repo
self.pr_number = pr_number
self.token = token
github = Github(token)
repo_object = github.get_repo(f"{repo}")
self._pull_request = repo_object.get_pull(pr_number)
def headers(self, media_type: str):
return {
"Accept": f"application/vnd.github.{media_type}",
"Authorization": f"token {self.token}",
}
@property
def base_url(self):
return f"https://api.github.com/repos/{self.repo}/pulls/{self.pr_number}"
def get(self, media_type: str, extra: str = "") -> str:
url = f"{self.base_url}{extra}"
response = requests.get(url, headers=self.headers(media_type))
response.raise_for_status()
return response.text
def get_pr_diff(self) -> List[unidiff.PatchSet]:
"""Download the PR diff, return a list of PatchedFile"""
diffs = self.get("v3.diff")
# PatchSet is the easiest way to construct what we want, but the
# diff_line_no property on lines is counted from the top of the
# whole PatchSet, whereas GitHub is expecting the "position"
# property to be line count within each file's diff. So we need to
# do this little bit of faff to get a list of file-diffs with
# their own diff_line_no range
diff = [unidiff.PatchSet(str(file))[0] for file in unidiff.PatchSet(diffs)]
return diff
def get_pr_comments(self):
"""Download the PR review comments using the comfort-fade preview headers"""
def get_element(
requester: Requester, headers: dict, element: dict, completed: bool
):
return element
return PaginatedList(
get_element,
self._pull_request._requester,
f"{self.base_url}/comments",
None,
)
def post_lgtm_comment(self):
"""Post a "LGTM" comment if everything's clean, making sure not to spam"""
BODY = 'clang-tidy review says "All clean, LGTM! :+1:"'
comments = self.get_pr_comments()
for comment in comments:
if comment["body"] == BODY:
print("Already posted, no need to update")
return
self._pull_request.create_issue_comment(BODY)
def post_review(self, review):
"""Submit a completed review"""
headers = {
"Accept": "application/vnd.github.comfort-fade-preview+json",
"Authorization": f"token {self.token}",
}
url = f"{self.base_url}/reviews"
post_review_response = requests.post(url, json=review, headers=headers)
print(post_review_response.text)
post_review_response.raise_for_status()
@contextlib.contextmanager
def message_group(title: str):
print(f"::group::{title}", flush=True)
try:
yield
finally:
print("::endgroup::", flush=True)
def make_file_line_lookup(diff):
"""Get a lookup table for each file in diff, to convert between source
line number to line number in the diff
"""
lookup = {}
for file in diff:
filename = file.target_file[2:]
lookup[filename] = {}
for hunk in file:
for line in hunk:
if line.diff_line_no is None:
continue
if not line.is_removed:
lookup[filename][line.target_line_no] = (
line.diff_line_no - DIFF_HEADER_LINE_LENGTH
)
return lookup
def make_file_offset_lookup(filenames):
"""Create a lookup table to convert between character offset and line
number for the list of files in `filenames`.
This is a dict of the cumulative sum of the line lengths for each file.
"""
lookup = {}
for filename in filenames:
with open(filename, "r") as file:
lines = file.readlines()
# Length of each line
line_lengths = map(len, lines)
# Cumulative sum of line lengths => offset at end of each line
lookup[os.path.abspath(filename)] = [0] + list(
itertools.accumulate(line_lengths)
)
return lookup
def find_line_number_from_offset(offset_lookup, filename, offset):
"""Work out which line number `offset` corresponds to using `offset_lookup`.
The line number (0-indexed) is the index of the first line offset
which is larger than `offset`.
"""
name = str(pathlib.Path(filename).resolve().absolute())
if name not in offset_lookup:
# Let's make sure we've the file offsets for this other file
offset_lookup.update(make_file_offset_lookup([name]))
for line_num, line_offset in enumerate(offset_lookup[name]):
if line_offset > offset:
return line_num - 1
return -1
def read_one_line(filename, line_offset):
"""Read a single line from a source file"""
# Could cache the files instead of opening them each time?
with open(filename, "r") as file:
file.seek(line_offset)
return file.readline().rstrip("\n")
def collate_replacement_sets(diagnostic, offset_lookup):
"""Return a dict of replacements on the same or consecutive lines, indexed by line number
We need this as we have to apply all the replacements on one line at the same time
This could break if there are replacements in with the same line
number but in different files.
"""
# First, make sure each replacement contains "LineNumber", and
# "EndLineNumber" in case it spans multiple lines
for replacement in diagnostic["Replacements"]:
# It's possible the replacement is needed in another file?
# Not really sure how that could come about, but let's
# cover our behinds in case it does happen:
if replacement["FilePath"] not in offset_lookup:
# Let's make sure we've the file offsets for this other file
offset_lookup.update(make_file_offset_lookup([replacement["FilePath"]]))
replacement["LineNumber"] = find_line_number_from_offset(
offset_lookup, replacement["FilePath"], replacement["Offset"]
)
replacement["EndLineNumber"] = find_line_number_from_offset(
offset_lookup,
replacement["FilePath"],
replacement["Offset"] + replacement["Length"],
)
# Now we can group them into consecutive lines
groups = []
for index, replacement in enumerate(diagnostic["Replacements"]):
if index == 0:
# First one starts a new group, always
groups.append([replacement])
elif (
replacement["LineNumber"] == groups[-1][-1]["LineNumber"]
or replacement["LineNumber"] - 1 == groups[-1][-1]["LineNumber"]
):
# Same or adjacent line to the last line in the last group
# goes in the same group
groups[-1].append(replacement)
else:
# Otherwise, start a new group
groups.append([replacement])
# Turn the list into a dict
return {g[0]["LineNumber"]: g for g in groups}
def replace_one_line(replacement_set, line_num, offset_lookup):
"""Apply all the replacements in replacement_set at the same time"""
filename = replacement_set[0]["FilePath"]
# File offset at the start of the first line
line_offset = offset_lookup[filename][line_num]
# List of (start, end) offsets from line_offset
insert_offsets = [(0, 0)]
# Read all the source lines into a dict so we only get one copy of
# each line, though we might read the same line in multiple times
source_lines = {}
for replacement in replacement_set:
start = replacement["Offset"] - line_offset
end = start + replacement["Length"]
insert_offsets.append((start, end))
# Make sure to read any extra lines we need too
for replacement_line_num in range(
replacement["LineNumber"], replacement["EndLineNumber"] + 1
):
replacement_line_offset = offset_lookup[filename][replacement_line_num]
source_lines[replacement_line_num] = (
read_one_line(filename, replacement_line_offset) + "\n"
)
# Replacements might cross multiple lines, so squash them all together
source_line = "".join(source_lines.values()).rstrip("\n")
insert_offsets.append((None, None))
fragments = []
for (_, start), (end, _) in zip(insert_offsets[:-1], insert_offsets[1:]):
fragments.append(source_line[start:end])
new_line = ""
for fragment, replacement in zip(fragments, replacement_set):
new_line += fragment + replacement["ReplacementText"]
return source_line, new_line + fragments[-1]
def format_ordinary_line(source_line, line_offset):
"""Format a single C++ line with a diagnostic indicator"""
return textwrap.dedent(
f"""\
```cpp
{source_line}
{line_offset * " " + "^"}
```
"""
)
def format_diff_line(diagnostic, offset_lookup, source_line, line_offset, line_num):
"""Format a replacement as a Github suggestion or diff block"""
end_line = line_num
# We're going to be appending to this
code_blocks = ""
replacement_sets = collate_replacement_sets(diagnostic, offset_lookup)
for replacement_line_num, replacement_set in replacement_sets.items():
old_line, new_line = replace_one_line(
replacement_set, replacement_line_num, offset_lookup
)
print(f"----------\n{old_line=}\n{new_line=}\n----------")
# If the replacement is for the same line as the
# diagnostic (which is where the comment will be), then
# format the replacement as a suggestion. Otherwise,
# format it as a diff
if replacement_line_num == line_num:
code_blocks += f"""
```suggestion
{new_line}
```
"""
end_line = replacement_set[-1]["EndLineNumber"]
else:
# Prepend each line in the replacement line with "+ "
# in order to make a nice diff block. The extra
# whitespace is so the multiline dedent-ed block below
# doesn't come out weird.
whitespace = "\n "
new_line = whitespace.join([f"+ {line}" for line in new_line.splitlines()])
old_line = whitespace.join([f"- {line}" for line in old_line.splitlines()])
rel_path = try_relative(replacement_set[0]["FilePath"])
code_blocks += textwrap.dedent(
f"""\
{rel_path}:{replacement_line_num}:
```diff
{old_line}
{new_line}
```
"""
)
return code_blocks, end_line
def try_relative(path):
"""Try making `path` relative to current directory, otherwise make it an absolute path"""
try:
here = pathlib.Path.cwd()
return pathlib.Path(path).relative_to(here)
except ValueError:
return pathlib.Path(path).resolve()
def format_notes(notes, offset_lookup):
"""Format an array of notes into a single string"""
code_blocks = ""
for note in notes:
filename = note["FilePath"]
if filename == "":
return note["Message"]
resolved_path = str(pathlib.Path(filename).resolve().absolute())
line_num = find_line_number_from_offset(
offset_lookup, resolved_path, note["FileOffset"]
)
line_offset = note["FileOffset"] - offset_lookup[resolved_path][line_num]
source_line = read_one_line(
resolved_path, offset_lookup[resolved_path][line_num]
)
path = try_relative(resolved_path)
message = f"**{path}:{line_num}:** {note['Message']}"
code = format_ordinary_line(source_line, line_offset)
code_blocks += f"{message}\n{code}"
return code_blocks
def make_comment_from_diagnostic(diagnostic_name, diagnostic, offset_lookup, notes):
"""Create a comment from a diagnostic
Comment contains the diagnostic message, plus its name, along with
code block(s) containing either the exact location of the
diagnostic, or suggested fix(es).
"""
filename = diagnostic["FilePath"]
line_num = find_line_number_from_offset(
offset_lookup, filename, diagnostic["FileOffset"]
)
line_offset = diagnostic["FileOffset"] - offset_lookup[filename][line_num]
source_line = read_one_line(filename, offset_lookup[filename][line_num])
end_line = line_num
print(
f"""{diagnostic}
{line_num=}; {line_offset=}; {source_line=}
"""
)
if diagnostic["Replacements"]:
code_blocks, end_line = format_diff_line(
diagnostic, offset_lookup, source_line, line_offset, line_num
)
else:
# No fixit, so just point at the problem
code_blocks = format_ordinary_line(source_line, line_offset)
code_blocks += format_notes(notes, offset_lookup)
comment_body = (
f"warning: {diagnostic['Message']} [{diagnostic_name}]\n{code_blocks}"
)
return comment_body, end_line + 1
def make_review(diagnostics, diff_lookup, offset_lookup):
"""Create a Github review from a set of clang-tidy diagnostics"""
comments = []
for diagnostic in diagnostics:
try:
diagnostic_message = diagnostic["DiagnosticMessage"]
except KeyError:
# Pre-clang-tidy-9 format
diagnostic_message = diagnostic
if diagnostic_message["FilePath"] == "":
continue
comment_body, end_line = make_comment_from_diagnostic(
diagnostic["DiagnosticName"],
diagnostic_message,
offset_lookup,
notes=diagnostic.get("Notes", []),
)
rel_path = str(try_relative(diagnostic_message["FilePath"]))
# diff lines are 1-indexed
source_line = 1 + find_line_number_from_offset(
offset_lookup,
diagnostic_message["FilePath"],
diagnostic_message["FileOffset"],
)
if rel_path not in diff_lookup or end_line not in diff_lookup[rel_path]:
print(
f"WARNING: Skipping comment for file '{rel_path}' not in PR changeset. Comment body is:\n{comment_body}"
)
continue
comments.append(
{
"path": rel_path,
"body": comment_body,
"side": "RIGHT",
"line": end_line,
}
)
# If this is a multiline comment, we need a couple more bits:
if end_line != source_line:
comments[-1].update(
{
"start_side": "RIGHT",
"start_line": source_line,
}
)
review = {
"body": "clang-tidy made some suggestions",
"event": "COMMENT",
"comments": comments,
}
return review
def get_line_ranges(diff, files):
"""Return the line ranges of added lines in diff, suitable for the
line-filter argument of clang-tidy
"""
lines_by_file = {}
for filename in diff:
if filename.target_file[2:] not in files:
continue
added_lines = []
for hunk in filename:
for line in hunk:
if line.is_added:
added_lines.append(line.target_line_no)
for _, group in itertools.groupby(
enumerate(added_lines), lambda ix: ix[0] - ix[1]
):
groups = list(map(itemgetter(1), group))
lines_by_file.setdefault(filename.target_file[2:], []).append(
[groups[0], groups[-1]]
)
line_filter_json = []
for name, lines in lines_by_file.items():
line_filter_json.append(str({"name": name, "lines": lines}))
return json.dumps(line_filter_json, separators=(",", ":"))
def get_clang_tidy_warnings(
line_filter, build_dir, clang_tidy_checks, clang_tidy_binary, config_file, files
):
"""Get the clang-tidy warnings"""
if config_file != "":
config = f"-config-file={config_file}"
else:
config = f"-checks={clang_tidy_checks}"
print(f"Using config: {config}")
command = f"{clang_tidy_binary} -p={build_dir} {config} -line-filter={line_filter} {files} --export-fixes={FIXES_FILE}"
start = datetime.datetime.now()
try:
with message_group(f"Running:\n\t{command}"):
output = subprocess.run(
command, capture_output=True, shell=True, check=True, encoding="utf-8"
)
except subprocess.CalledProcessError as e:
print(
f"\n\nclang-tidy failed with return code {e.returncode} and error:\n{e.stderr}\nOutput was:\n{e.stdout}"
)
end = datetime.datetime.now()
print(f"Took: {end - start}")
try:
with open(FIXES_FILE, "r") as fixes_file:
return yaml.safe_load(fixes_file)
except FileNotFoundError:
return {}
def cull_comments(pull_request: PullRequest, review, max_comments):
"""Remove comments from review that have already been posted, and keep
only the first max_comments
"""
comments = pull_request.get_pr_comments()
for comment in comments:
review["comments"] = list(
filter(
lambda review_comment: not (
review_comment["path"] == comment["path"]
and review_comment["line"] == comment["line"]
and review_comment["body"] == comment["body"]
),
review["comments"],
)
)
if len(review["comments"]) > max_comments:
review["body"] += (
"\n\nThere were too many comments to post at once. "
f"Showing the first {max_comments} out of {len(review['comments'])}. "
"Check the log or trigger a new build to see more."
)
review["comments"] = review["comments"][:max_comments]
return review
def main(
repo,
pr_number,
build_dir,
clang_tidy_checks,
clang_tidy_binary,
config_file,
token,
include,
exclude,
max_comments,
dry_run: bool = False,
):
pull_request = PullRequest(repo, pr_number, token)
diff = pull_request.get_pr_diff()
print(f"\nDiff from GitHub PR:\n{diff}\n")
changed_files = [filename.target_file[2:] for filename in diff]
files = []
for pattern in include:
files.extend(fnmatch.filter(changed_files, pattern))
print(f"include: {pattern}, file list now: {files}")
for pattern in exclude:
files = [f for f in files if not fnmatch.fnmatch(f, pattern)]
print(f"exclude: {pattern}, file list now: {files}")
if files == []:
print("No files to check!")
return
print(f"Checking these files: {files}", flush=True)
line_ranges = get_line_ranges(diff, files)
if line_ranges == "[]":
print("No lines added in this PR!")
return
print(f"Line filter for clang-tidy:\n{line_ranges}\n")
clang_tidy_warnings = get_clang_tidy_warnings(
line_ranges,
build_dir,
clang_tidy_checks,
clang_tidy_binary,
config_file,
" ".join(files),
)
print("clang-tidy had the following warnings:\n", clang_tidy_warnings, flush=True)
if clang_tidy_warnings == {}:
print("No warnings, LGTM!")
if not dry_run:
pull_request.post_lgtm_comment()
return
diff_lookup = make_file_line_lookup(diff)
offset_lookup = make_file_offset_lookup(files)
with message_group("Creating review from warnings"):
review = make_review(
clang_tidy_warnings["Diagnostics"], diff_lookup, offset_lookup
)
print(
"Created the following review:\n", pprint.pformat(review, width=130), flush=True
)
if review["comments"] == []:
print("No warnings to report, LGTM!")
if not dry_run:
pull_request.post_lgtm_comment()
return
print("Removing already posted or extra comments", flush=True)
trimmed_review = cull_comments(pull_request, review, max_comments)
print(f"::set-output name=total_comments::{len(review['comments'])}")
if trimmed_review["comments"] == []:
print("Everything already posted!")
return review
if dry_run:
pprint.pprint(review, width=130)
return
print("Posting the review:\n", pprint.pformat(trimmed_review), flush=True)
pull_request.post_review(trimmed_review)
def strip_enclosing_quotes(string: str) -> str:
"""Strip leading/trailing whitespace and remove any enclosing quotes"""
stripped = string.strip()
# Need to check double quotes again in case they're nested inside
# single quotes
for quote in ['"', "'", '"']:
if stripped.startswith(quote) and stripped.endswith(quote):
stripped = stripped[1:-1]
return stripped
def fix_absolute_paths(build_compile_commands, base_dir):
"""Update absolute paths in compile_commands.json to new location, if
compile_commands.json was created outside the Actions container
"""
basedir = pathlib.Path(base_dir).resolve()
newbasedir = pathlib.Path(".").resolve()
if basedir == newbasedir:
return
print(f"Found '{build_compile_commands}', updating absolute paths")
# We might need to change some absolute paths if we're inside
# a docker container
with open(build_compile_commands, "r") as f:
compile_commands = json.load(f)
print(f"Replacing '{basedir}' with '{newbasedir}'", flush=True)
modified_compile_commands = json.dumps(compile_commands).replace(
str(basedir), str(newbasedir)
)
with open(build_compile_commands, "w") as f:
f.write(modified_compile_commands)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Create a review from clang-tidy warnings"
)
parser.add_argument("--repo", help="Repo name in form 'owner/repo'")
parser.add_argument("--pr", help="PR number", type=int)
parser.add_argument(
"--clang_tidy_binary", help="clang-tidy binary", default="clang-tidy-11"
)
parser.add_argument(
"--build_dir", help="Directory with compile_commands.json", default="."
)
parser.add_argument(
"--base_dir",
help="Absolute path of initial working directory if compile_commands.json generated outside of Action",
default=".",
)
parser.add_argument(
"--clang_tidy_checks",
help="checks argument",
default="'-*,performance-*,readability-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,mpi-*,misc-*'",
)
parser.add_argument(
"--config_file",
help="Path to .clang-tidy config file. If not empty, takes precedence over --clang_tidy_checks",
default="",
)
parser.add_argument(
"--include",
help="Comma-separated list of files or patterns to include",
type=str,
nargs="?",
default="*.[ch],*.[ch]xx,*.[ch]pp,*.[ch]++,*.cc,*.hh",
)
parser.add_argument(
"--exclude",
help="Comma-separated list of files or patterns to exclude",
nargs="?",
default="",
)
parser.add_argument(
"--apt-packages",
help="Comma-separated list of apt packages to install",
type=str,
default="",
)
parser.add_argument(
"--cmake-command",
help="If set, run CMake as part of the action with this command",
type=str,
default="",
)
parser.add_argument(
"--max-comments",
help="Maximum number of comments to post at once",
type=int,
default=25,
)
parser.add_argument("--token", help="github auth token")
parser.add_argument(
"--dry-run", help="Run and generate review, but don't post", action="store_true"
)
args = parser.parse_args()
# Remove any enclosing quotes and extra whitespace
exclude = strip_enclosing_quotes(args.exclude).split(",")
include = strip_enclosing_quotes(args.include).split(",")
if args.apt_packages:
# Try to make sure only 'apt install' is run
apt_packages = re.split(BAD_CHARS_APT_PACKAGES_PATTERN, args.apt_packages)[
0
].split(",")
with message_group(f"Installing additional packages: {apt_packages}"):
subprocess.run(
["apt", "install", "-y", "--no-install-recommends"] + apt_packages
)
build_compile_commands = f"{args.build_dir}/compile_commands.json"
cmake_command = strip_enclosing_quotes(args.cmake_command)
# If we run CMake as part of the action, then we know the paths in
# the compile_commands.json file are going to be correct
if cmake_command:
with message_group(f"Running cmake: {cmake_command}"):
subprocess.run(cmake_command, shell=True, check=True)
elif os.path.exists(build_compile_commands):
fix_absolute_paths(build_compile_commands, args.base_dir)
main(
repo=args.repo,
pr_number=args.pr,
build_dir=args.build_dir,
clang_tidy_checks=args.clang_tidy_checks,
clang_tidy_binary=args.clang_tidy_binary,
config_file=args.config_file,
token=args.token,
include=include,
exclude=exclude,
max_comments=args.max_comments,
dry_run=args.dry_run,
)