-
Notifications
You must be signed in to change notification settings - Fork 46
/
post.py
executable file
·114 lines (97 loc) · 3.15 KB
/
post.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
#!/usr/bin/env python3
# clang-tidy review - post comments
# Copyright (c) 2022 Peter Hill
# SPDX-License-Identifier: MIT
# See LICENSE for more information
import argparse
import pathlib
import pprint
from clang_tidy_review import (
PullRequest,
load_and_merge_reviews,
post_review,
load_metadata,
strip_enclosing_quotes,
download_artifacts,
post_annotations,
bool_argument,
REVIEW_FILE,
add_auth_arguments,
get_auth_from_arguments,
)
def main() -> int:
parser = argparse.ArgumentParser(
description="Post a review based on feedback generated by the clang-tidy-review action"
)
parser.add_argument("--repo", help="Repo name in form 'owner/repo'")
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 review says "All clean, LGTM! :+1:"',
)
parser.add_argument(
"--dry-run", help="Run and generate review, but don't post", action="store_true"
)
parser.add_argument(
"--workflow_id",
help="ID of the workflow that generated the review",
default=None,
)
parser.add_argument(
"--annotations",
help="Use annotations instead of comments",
type=bool_argument,
default=False,
)
parser.add_argument(
"--num-comments-as-exitcode",
help="Set the exit code to be the amount of comments",
type=bool_argument,
default=True,
)
add_auth_arguments(parser)
parser.add_argument(
"reviews",
metavar="REVIEW_FILES",
type=pathlib.Path,
nargs="*",
default=[pathlib.Path(REVIEW_FILE)],
help="Split workflow review results",
)
args = parser.parse_args()
pull_request = PullRequest(args.repo, None, get_auth_from_arguments(args))
# Try to read the review artifacts if they're already present
metadata = load_metadata()
review = load_and_merge_reviews(args.reviews)
# If not, try to download them automatically
if metadata is None and args.workflow_id is not None:
print("Attempting to automatically download review artifacts", flush=True)
metadata, review = download_artifacts(pull_request, int(args.workflow_id))
if metadata is None:
raise RuntimeError("Couldn't find review metadata")
pull_request.pr_number = metadata["pr_number"]
print(
"clang-tidy-review generated the following review",
pprint.pformat(review, width=130),
flush=True,
)
if args.annotations:
exit_code = post_annotations(pull_request, review)
else:
lgtm_comment_body = strip_enclosing_quotes(args.lgtm_comment_body)
exit_code = post_review(
pull_request, review, args.max_comments, lgtm_comment_body, args.dry_run
)
if args.num_comments_as_exitcode:
return exit_code
else:
return 0
if __name__ == "__main__":
exit(main())