forked from ZedThree/clang-tidy-review
-
Notifications
You must be signed in to change notification settings - Fork 0
/
review.py
executable file
·153 lines (130 loc) · 3.94 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
#!/usr/bin/env python3
# clang-tidy review
# Copyright (c) 2020 Peter Hill
# SPDX-License-Identifier: MIT
# See LICENSE for more information
import argparse
import json
import os
import pathlib
import re
import subprocess
from review import (
PullRequest,
message_group,
strip_enclosing_quotes,
create_review,
save_metadata,
post_review,
)
def main(
repo,
pr_number,
token,
fixes_file,
include,
exclude,
max_comments,
lgtm_comment_body,
artifacts_dir,
dry_run: bool = False
):
pull_request = PullRequest(repo, pr_number, token)
review = create_review(
pull_request,
fixes_file,
include,
exclude,
artifacts_dir
)
with message_group("Saving metadata"):
save_metadata(pr_number, artifacts_dir)
post_review(pull_request, review, max_comments, lgtm_comment_body, dry_run)
def fix_absolute_paths(fixes_file, base_dir):
"""Update absolute paths in fixes file to new location, if
fixes file was created outside the Actions container
"""
basedir = pathlib.Path(base_dir).resolve()
newbasedir = pathlib.Path(".").resolve()
if basedir == newbasedir:
return
print(f"Found '{fixes_file}', updating absolute paths")
# We might need to change some absolute paths if we're inside
# a docker container
with open(fixes_file, "r") as f:
fixes = f.read()
print(f"Replacing '{basedir}' with '{newbasedir}'", flush=True)
modified_fixes = fixes.replace(
str(basedir), str(newbasedir)
)
with open(fixes_file, "w") as f:
f.write(modified_fixes)
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("--token", help="GitHub authentication token")
parser.add_argument(
"--base-dir",
help="Absolute path to initial working directory to fix absolute paths in clang-tidy fixes file",
default=".",
)
parser.add_argument(
"--fixes-file",
help="Path to pre-generated clang-tidy fixes file",
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(
"--max-comments",
help="Maximum number of comments to post at once",
type=int,
default=25,
)
parser.add_argument(
"--lgtm-comment-body",
help="Message to post on PR if no issues are found. An empty string will post no LGTM comment.",
type=str,
default='`clang-tidy` found no issues, all clean :+1:',
)
parser.add_argument(
"--artifacts-dir",
help="Directory to save artifacts in",
default="",
)
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(",")
fixes_file = args.fixes_file
if os.path.exists(fixes_file):
fix_absolute_paths(fixes_file, args.base_dir)
main(
repo=args.repo,
pr_number=args.pr,
token=args.token,
fixes_file=fixes_file,
include=include,
exclude=exclude,
max_comments=args.max_comments,
lgtm_comment_body=strip_enclosing_quotes(args.lgtm_comment_body),
artifacts_dir=args.artifacts_dir,
dry_run=args.dry_run
)