xref: /llvm-project/llvm/utils/git/code-format-helper.py (revision 56d0e8ccf424ddcd74a505837b8966204aaba415)
1#!/usr/bin/env python3
2#
3# ====- code-format-helper, runs code formatters from the ci --*- python -*--==#
4#
5# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6# See https://llvm.org/LICENSE.txt for license information.
7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8#
9# ==-------------------------------------------------------------------------==#
10
11import argparse
12import os
13import subprocess
14import sys
15from functools import cached_property
16
17import github
18from github import IssueComment, PullRequest
19
20
21class FormatHelper:
22    COMMENT_TAG = "<!--LLVM CODE FORMAT COMMENT: {fmt}-->"
23    name: str
24    friendly_name: str
25
26    @property
27    def comment_tag(self) -> str:
28        return self.COMMENT_TAG.replace("fmt", self.name)
29
30    @property
31    def instructions(self) -> str:
32        raise NotImplementedError()
33
34    def format_run(
35        self, changed_files: list[str], args: argparse.Namespace
36    ) -> str | None:
37        raise NotImplementedError()
38
39    def pr_comment_text_for_diff(self, diff: str) -> str:
40        return f"""
41:warning: {self.friendly_name}, {self.name} found issues in your code. :warning:
42
43<details>
44<summary>
45You can test this locally with the following command:
46</summary>
47
48``````````bash
49{self.instructions}
50``````````
51
52</details>
53
54<details>
55<summary>
56View the diff from {self.name} here.
57</summary>
58
59``````````diff
60{diff}
61``````````
62
63</details>
64"""
65
66    def find_comment(
67        self, pr: PullRequest.PullRequest
68    ) -> IssueComment.IssueComment | None:
69        for comment in pr.as_issue().get_comments():
70            if self.comment_tag in comment.body:
71                return comment
72        return None
73
74    def update_pr(
75        self, comment_text: str, args: argparse.Namespace, create_new: bool
76    ) -> None:
77        repo = github.Github(args.token).get_repo(args.repo)
78        pr = repo.get_issue(args.issue_number).as_pull_request()
79
80        comment_text = self.comment_tag + "\n\n" + comment_text
81
82        existing_comment = self.find_comment(pr)
83        if existing_comment:
84            existing_comment.edit(comment_text)
85        elif create_new:
86            pr.as_issue().create_comment(comment_text)
87
88    def run(self, changed_files: list[str], args: argparse.Namespace) -> bool:
89        diff = self.format_run(changed_files, args)
90        if diff is None:
91            comment_text = f"""
92:white_check_mark: With the latest revision this PR passed the {self.friendly_name}.
93"""
94            self.update_pr(comment_text, args, create_new=False)
95            return True
96        elif len(diff) > 0:
97            comment_text = self.pr_comment_text_for_diff(diff)
98            self.update_pr(comment_text, args, create_new=True)
99            return False
100        else:
101            # The formatter failed but didn't output a diff (e.g. some sort of
102            # infrastructure failure).
103            comment_text = f"""
104:warning: The {self.friendly_name} failed without printing a diff. Check the logs for stderr output. :warning:
105"""
106            self.update_pr(comment_text, args, create_new=False)
107            return False
108
109
110class ClangFormatHelper(FormatHelper):
111    name = "clang-format"
112    friendly_name = "C/C++ code formatter"
113
114    @property
115    def instructions(self) -> str:
116        return " ".join(self.cf_cmd)
117
118    @cached_property
119    def libcxx_excluded_files(self) -> list[str]:
120        with open("libcxx/utils/data/ignore_format.txt", "r") as ifd:
121            return [excl.strip() for excl in ifd.readlines()]
122
123    def should_be_excluded(self, path: str) -> bool:
124        if path in self.libcxx_excluded_files:
125            print(f"{self.name}: Excluding file {path}")
126            return True
127        return False
128
129    def filter_changed_files(self, changed_files: list[str]) -> list[str]:
130        filtered_files = []
131        for path in changed_files:
132            _, ext = os.path.splitext(path)
133            if ext in (".cpp", ".c", ".h", ".hpp", ".hxx", ".cxx"):
134                if not self.should_be_excluded(path):
135                    filtered_files.append(path)
136        return filtered_files
137
138    def format_run(
139        self, changed_files: list[str], args: argparse.Namespace
140    ) -> str | None:
141        cpp_files = self.filter_changed_files(changed_files)
142        if not cpp_files:
143            return None
144        cf_cmd = [
145            "git-clang-format",
146            "--diff",
147            args.start_rev,
148            args.end_rev,
149            "--",
150        ] + cpp_files
151        print(f"Running: {' '.join(cf_cmd)}")
152        self.cf_cmd = cf_cmd
153        proc = subprocess.run(cf_cmd, capture_output=True)
154        sys.stdout.write(proc.stderr.decode("utf-8"))
155
156        if proc.returncode != 0:
157            # formatting needed, or the command otherwise failed
158            print(f"error: {self.name} exited with code {proc.returncode}")
159            # Print the diff in the log so that it is viewable there
160            print(proc.stdout.decode("utf-8"))
161            return proc.stdout.decode("utf-8")
162        else:
163            sys.stdout.write(proc.stdout.decode("utf-8"))
164            return None
165
166
167class DarkerFormatHelper(FormatHelper):
168    name = "darker"
169    friendly_name = "Python code formatter"
170
171    @property
172    def instructions(self) -> str:
173        return " ".join(self.darker_cmd)
174
175    def filter_changed_files(self, changed_files: list[str]) -> list[str]:
176        filtered_files = []
177        for path in changed_files:
178            name, ext = os.path.splitext(path)
179            if ext == ".py":
180                filtered_files.append(path)
181
182        return filtered_files
183
184    def format_run(
185        self, changed_files: list[str], args: argparse.Namespace
186    ) -> str | None:
187        py_files = self.filter_changed_files(changed_files)
188        if not py_files:
189            return None
190        darker_cmd = [
191            "darker",
192            "--check",
193            "--diff",
194            "-r",
195            f"{args.start_rev}..{args.end_rev}",
196        ] + py_files
197        print(f"Running: {' '.join(darker_cmd)}")
198        self.darker_cmd = darker_cmd
199        proc = subprocess.run(darker_cmd, capture_output=True)
200        sys.stdout.write(proc.stderr.decode("utf-8"))
201
202        if proc.returncode != 0:
203            # formatting needed, or the command otherwise failed
204            print(f"error: {self.name} exited with code {proc.returncode}")
205            # Print the diff in the log so that it is viewable there
206            print(proc.stdout.decode("utf-8"))
207            return proc.stdout.decode("utf-8")
208        else:
209            sys.stdout.write(proc.stdout.decode("utf-8"))
210            return None
211
212
213ALL_FORMATTERS = (DarkerFormatHelper(), ClangFormatHelper())
214
215if __name__ == "__main__":
216    parser = argparse.ArgumentParser()
217    parser.add_argument(
218        "--token", type=str, required=True, help="GitHub authentiation token"
219    )
220    parser.add_argument(
221        "--repo",
222        type=str,
223        default=os.getenv("GITHUB_REPOSITORY", "llvm/llvm-project"),
224        help="The GitHub repository that we are working with in the form of <owner>/<repo> (e.g. llvm/llvm-project)",
225    )
226    parser.add_argument("--issue-number", type=int, required=True)
227    parser.add_argument(
228        "--start-rev",
229        type=str,
230        required=True,
231        help="Compute changes from this revision.",
232    )
233    parser.add_argument(
234        "--end-rev", type=str, required=True, help="Compute changes to this revision"
235    )
236    parser.add_argument(
237        "--changed-files",
238        type=str,
239        help="Comma separated list of files that has been changed",
240    )
241
242    args = parser.parse_args()
243
244    changed_files = []
245    if args.changed_files:
246        changed_files = args.changed_files.split(",")
247
248    failed_formatters = []
249    for fmt in ALL_FORMATTERS:
250        if not fmt.run(changed_files, args):
251            failed_formatters.append(fmt.name)
252
253    if len(failed_formatters) > 0:
254        print(f"error: some formatters failed: {' '.join(failed_formatters)}")
255        sys.exit(1)
256