================
@@ -124,133 +92,205 @@ def create_comment_text(warning: str, cpp_files: 
List[str]) -> str:
 
 <details>
 <summary>
-View the output from clang-tidy here.
+View the output from {self.name} here.
 </summary>
 
 ```
-{warning}
+{linter_output}
 ```
 
 </details>
 """
 
+    # TODO: Refactor this
+    def find_comment(self, pr: Any) -> Any:
+        for comment in pr.as_issue().get_comments():
+            if comment.body.startswith(self.comment_tag):
+                return comment
+        return None
 
-def find_comment(pr: any) -> any:
-    for comment in pr.as_issue().get_comments():
-        if COMMENT_TAG in comment.body:
-            return comment
-    return None
+    def update_pr(self, comment_text: str, args: LintArgs, create_new: bool) 
-> None:
+        assert args.repo is not None
+        repo = github.Github(args.token).get_repo(args.repo)
+        pr = repo.get_issue(args.issue_number).as_pull_request()
 
+        comment_text = f"{self.comment_tag}\n\n{comment_text}"
 
-def create_comment(
-    comment_text: str, args: LintArgs, create_new: bool
-) -> Optional[dict]:
-    import github
+        existing_comment = self.find_comment(pr)
 
-    repo = github.Github(args.token).get_repo(args.repo)
-    pr = repo.get_issue(args.issue_number).as_pull_request()
+        if existing_comment:
+            self.comment = {"body": comment_text, "id": existing_comment.id}
+        elif create_new:
+            self.comment = {"body": comment_text}
 
-    comment_text = COMMENT_TAG + "\n\n" + comment_text
 
-    existing_comment = find_comment(pr)
+    def run(self, changed_files: List[str], args: LintArgs) -> bool:
+        if args.verbose:
+            print(f"got changed files: {changed_files}")
 
-    comment = None
-    if create_new or existing_comment:
-        comment = {"body": comment_text}
-    if existing_comment:
-        comment["id"] = existing_comment.id
-    return comment
+        files_to_lint = self.filter_changed_files(changed_files)
 
+        if not files_to_lint and args.verbose:
+            print("no modified files found")
 
-def run_clang_tidy(changed_files: List[str], args: LintArgs) -> Optional[str]:
-    if not changed_files:
-        print("no c/c++ files found")
-        return None
+        is_success = True
+        linter_output = None
 
-    git_diff_cmd = [
-        "git",
-        "diff",
-        "-U0",
-        f"{args.start_rev}...{args.end_rev}",
-        "--",
-    ] + changed_files
-
-    diff_proc = subprocess.run(
-        git_diff_cmd,
-        stdout=subprocess.PIPE,
-        stderr=subprocess.PIPE,
-        text=True,
-        check=False,
-    )
+        if files_to_lint:
+            linter_output = self.run_linter_tool(files_to_lint, args)
+            if linter_output:
+                is_success = False
 
-    if diff_proc.returncode != 0:
-        print(f"Git diff failed: {diff_proc.stderr}")
-        return None
+        should_update_gh = args.token is not None and args.repo is not None
 
-    diff_content = diff_proc.stdout
-    if not diff_content.strip():
-        print("No diff content found")
-        return None
+        if is_success:
+            if should_update_gh:
+                comment_text = (
+                    ":white_check_mark: With the latest revision "
+                    f"this PR passed the {self.friendly_name}."
+                )
+                self.update_pr(comment_text, args, create_new=False)
+            return True
+        else:
+            if should_update_gh:
+                if linter_output:
+                    comment_text = self.create_comment_text(
+                        linter_output, files_to_lint, args
+                    )
+                    self.update_pr(comment_text, args, create_new=True)
+                else:
+                    # The linter failed but didn't output a result (e.g. some 
sort of
+                    # infrastructure failure).
+                    comment_text = (
+                        f":warning: The {self.friendly_name} failed without 
printing "
+                        "an output. Check the logs for output. :warning:"
+                    )
+                    self.update_pr(comment_text, args, create_new=False)
+            else:
+                if linter_output:
+                    print(
+                        f"Warning: {self.friendly_name}, {self.name} detected "
+                        "some issues with your code..."
+                    )
+                    print(linter_output)
+                else:
+                    print(f"Warning: {self.friendly_name}, {self.name} failed 
to run.")
+            return False
+
+
+class ClangTidyLintHelper(LintHelper):
+    name: Final[str] = "clang-tidy"
+    friendly_name: Final[str] = "C/C++ code linter"
----------------
EugeneZelenko wrote:

```suggestion
    name: Final = "clang-tidy"
    friendly_name: Final = "C/C++ code linter"
```

https://github.com/llvm/llvm-project/pull/168827
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to