MCPcopy Create free account
hub / github.com/apache/tvm / BotCommentBuilder

Class BotCommentBuilder

ci/scripts/github/github_commenter.py:36–139  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34
35
36class BotCommentBuilder:
37 ALLOWLIST_USERS = {"driazati", "gigiblender", "areusch"}
38
39 def __init__(self, github: GitHubRepo, data: dict[str, Any]):
40 self.github = github
41 self.pr_number = data["number"]
42 self.comment_data = data["comments"]["nodes"]
43 self.author = data["author"]["login"]
44
45 def find_bot_comment(self) -> dict[str, Any] | None:
46 """
47 Return the existing bot comment or None if it does not exist
48 """
49 for comment in self.comment_data:
50 logging.info(f"Checking comment {comment}")
51 if (
52 comment["author"]["login"] == "github-actions"
53 and BOT_COMMENT_START in comment["body"]
54 ):
55 logging.info("Found existing comment")
56 return comment
57 logging.info("No existing comment found")
58 return None
59
60 def find_existing_body(self) -> dict[str, str]:
61 """
62 Find existing dynamic bullet point items
63 """
64 existing_comment = self.find_bot_comment()
65 if existing_comment is None:
66 logging.info("No existing comment while searching for body items")
67 return {}
68
69 matches = re.findall(
70 r"<!--bot-comment-([a-z][a-z-]+)-start-->([\S\s]*?)<!--bot-comment-([a-z-]+)-end-->",
71 existing_comment["body"],
72 flags=re.MULTILINE,
73 )
74 logging.info(f"Fetch body item matches: {matches}")
75
76 items = {}
77 for start, text, end in matches:
78 if start != end:
79 raise RuntimeError(
80 f"Malformed comment found: {start} marker did not have matching end, found instead {end}"
81 )
82 items[start] = text.strip().lstrip("* ")
83
84 logging.info(f"Found body items: {items}")
85 return items
86
87 def _post_comment(self, body_items: dict[str, str]):
88 comment = BOT_COMMENT_START + "\n\n" + WELCOME_TEXT + "\n\n"
89 for key, content in body_items.items():
90 line = self.start_key(key) + "\n * " + content.strip() + self.end_key(key)
91 logging.info(f"Adding line {line}")
92 comment += line
93 comment += "\n\n<sub>Generated by [tvm-bot](https://github.com/apache/tvm/blob/main/ci/README.md#github-actions)</sub>"

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…