You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/09/07 20:15:21 UTC

[GitHub] [tvm] areusch commented on a diff in pull request #12695: [ci] Add bot to post welcome comment

areusch commented on code in PR #12695:
URL: https://github.com/apache/tvm/pull/12695#discussion_r965224999


##########
ci/scripts/github_commenter.py:
##########
@@ -0,0 +1,130 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+import logging
+from typing import Dict, Tuple, Any, Optional, List, Union
+
+from git_utils import GitHubRepo
+
+BOT_COMMENT_START = "<!---bot-comment-->"

Review Comment:
   how does this related to bot-comment-foo-start?



##########
ci/scripts/git_utils.py:
##########
@@ -51,34 +54,62 @@ def post(url: str, body: Optional[Any] = None, auth: Optional[Tuple[str, str]] =
         return response.read()
 
 
+def dry_run_token(is_dry_run: bool) -> Any:
+    if is_dry_run:
+        return DRY_RUN
+    return os.environ["GITHUB_TOKEN"]
+
+
 class GitHubRepo:
-    def __init__(self, user, repo, token):
+    def __init__(self, user, repo, token, test_data=None):
         self.token = token
         self.user = user
         self.repo = repo
+        self.test_data = test_data
+        self.num_calls = 0
         self.base = f"https://api.github.com/repos/{user}/{repo}/"
 
     def headers(self):
         return {
             "Authorization": f"Bearer {self.token}",
         }
 
+    def dry_run(self) -> bool:
+        return self.token == DRY_RUN
+
     def graphql(self, query: str, variables: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
         query = compress_query(query)
         if variables is None:
             variables = {}
+
+        url = "https://api.github.com/graphql"
         response = self._request(
-            "https://api.github.com/graphql",
+            url,
             {"query": query, "variables": variables},
             method="POST",
         )
+        if self.dry_run():
+            return self.testing_response("POST", url)
+
         if "data" not in response:
             msg = f"Error fetching data with query:\n{query}\n\nvariables:\n{variables}\n\nerror:\n{json.dumps(response, indent=2)}"
             raise RuntimeError(msg)
         return response
 
+    def testing_response(self, method: str, url: str) -> Any:
+        self.num_calls += 1
+        key = f"[{self.num_calls}] {method} - {url}"
+        if self.test_data is not None and key in self.test_data:
+            return self.test_data[key]
+        logging.info(f"Unknown URL in dry run: {key}")

Review Comment:
   should we error here?



##########
ci/scripts/github_commenter.py:
##########
@@ -0,0 +1,130 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+import logging
+from typing import Dict, Tuple, Any, Optional, List, Union
+
+from git_utils import GitHubRepo
+
+BOT_COMMENT_START = "<!---bot-comment-->"
+WELCOME_TEXT = "Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @-ing them in a comment."
+
+
+class BotCommentBuilder:
+    def __init__(self, github: GitHubRepo, data: Dict[str, Any]):
+        self.github = github
+        self.pr_number = data["number"]
+        self.comment_data = data["comments"]["nodes"]
+        self.author = data["author"]["login"]
+
+    def find_bot_comment(self) -> Optional[Dict[str, Any]]:
+        """
+        Return the existing bot comment or None if it does not exist
+        """
+        for comment in self.comment_data:
+            logging.info(f"Checking comment {comment}")
+            if (
+                comment["author"]["login"] == "github-actions"
+                and BOT_COMMENT_START in comment["body"]
+            ):
+                logging.info("Found existing comment")
+                return comment
+        logging.info("No existing comment found")
+        return None
+
+    def find_existing_body(self) -> Dict[str, str]:
+        """
+        Find existing dynamic bullet point items
+        """
+        existing_comment = self.find_bot_comment()
+        if existing_comment is None:
+            logging.info(f"No existing comment while searching for body items")
+            return {}
+
+        matches = re.findall(
+            r"<!--bot-comment-([a-z][a-z-]+)-start-->([\S\s]*?)<!--bot-comment-([a-z-]+)-end-->",
+            existing_comment["body"],
+            flags=re.MULTILINE,
+        )
+        logging.info(f"Fetch body item matches: {matches}")
+
+        items = {}
+        for start, text, end in matches:
+            if start != end:
+                raise RuntimeError(
+                    f"Malformed comment found: {start} marker did not have matching end, found instead {end}"
+                )
+            items[start] = text.strip().lstrip("* ")
+
+        logging.info(f"Found body items: {items}")
+        return items
+
+    def _post_comment(self, body_items: Dict[str, str]):
+        comment = BOT_COMMENT_START + "\n\n" + WELCOME_TEXT + "\n\n"
+        for key, content in body_items.items():
+            line = self.start_key(key) + "\n * " + content.strip() + self.end_key(key)

Review Comment:
   should we textutil.indent(content.strip())? what if it's multi-line?



##########
ci/scripts/github_commenter.py:
##########
@@ -0,0 +1,130 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+import logging
+from typing import Dict, Tuple, Any, Optional, List, Union
+
+from git_utils import GitHubRepo
+
+BOT_COMMENT_START = "<!---bot-comment-->"
+WELCOME_TEXT = "Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @-ing them in a comment."
+
+
+class BotCommentBuilder:

Review Comment:
   want to write a small unittest for find_bot_comment()/find_existing_body() logic?



##########
ci/scripts/github_commenter.py:
##########
@@ -0,0 +1,130 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+import logging
+from typing import Dict, Tuple, Any, Optional, List, Union
+
+from git_utils import GitHubRepo
+
+BOT_COMMENT_START = "<!---bot-comment-->"
+WELCOME_TEXT = "Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @-ing them in a comment."
+
+
+class BotCommentBuilder:
+    def __init__(self, github: GitHubRepo, data: Dict[str, Any]):
+        self.github = github
+        self.pr_number = data["number"]
+        self.comment_data = data["comments"]["nodes"]
+        self.author = data["author"]["login"]
+
+    def find_bot_comment(self) -> Optional[Dict[str, Any]]:
+        """
+        Return the existing bot comment or None if it does not exist
+        """
+        for comment in self.comment_data:
+            logging.info(f"Checking comment {comment}")
+            if (
+                comment["author"]["login"] == "github-actions"
+                and BOT_COMMENT_START in comment["body"]
+            ):
+                logging.info("Found existing comment")
+                return comment
+        logging.info("No existing comment found")
+        return None
+
+    def find_existing_body(self) -> Dict[str, str]:
+        """
+        Find existing dynamic bullet point items
+        """
+        existing_comment = self.find_bot_comment()
+        if existing_comment is None:
+            logging.info(f"No existing comment while searching for body items")
+            return {}
+
+        matches = re.findall(
+            r"<!--bot-comment-([a-z][a-z-]+)-start-->([\S\s]*?)<!--bot-comment-([a-z-]+)-end-->",
+            existing_comment["body"],
+            flags=re.MULTILINE,
+        )
+        logging.info(f"Fetch body item matches: {matches}")
+
+        items = {}
+        for start, text, end in matches:
+            if start != end:
+                raise RuntimeError(
+                    f"Malformed comment found: {start} marker did not have matching end, found instead {end}"
+                )
+            items[start] = text.strip().lstrip("* ")
+
+        logging.info(f"Found body items: {items}")
+        return items
+
+    def _post_comment(self, body_items: Dict[str, str]):
+        comment = BOT_COMMENT_START + "\n\n" + WELCOME_TEXT + "\n\n"
+        for key, content in body_items.items():
+            line = self.start_key(key) + "\n * " + content.strip() + self.end_key(key)
+            logging.info(f"Adding line {line}")
+            comment += line
+        comment += "\n\n<sub>Generated by [tvm-bot](https://github.com/apache/tvm/blob/main/ci/README.md#github-actions)</sub>"
+
+        data = {"body": comment}
+        url = f"issues/{self.pr_number}/comments"
+
+        logging.info(f"Commenting {comment} on {url}")
+
+        if self.author not in {"driazati", "gigiblender", "areusch"}:

Review Comment:
   should we comment/make a class-level constant?



##########
ci/scripts/git_utils.py:
##########
@@ -51,34 +54,62 @@ def post(url: str, body: Optional[Any] = None, auth: Optional[Tuple[str, str]] =
         return response.read()
 
 
+def dry_run_token(is_dry_run: bool) -> Any:
+    if is_dry_run:
+        return DRY_RUN
+    return os.environ["GITHUB_TOKEN"]
+
+
 class GitHubRepo:
-    def __init__(self, user, repo, token):
+    def __init__(self, user, repo, token, test_data=None):
         self.token = token
         self.user = user
         self.repo = repo
+        self.test_data = test_data
+        self.num_calls = 0
         self.base = f"https://api.github.com/repos/{user}/{repo}/"
 
     def headers(self):
         return {
             "Authorization": f"Bearer {self.token}",
         }
 
+    def dry_run(self) -> bool:
+        return self.token == DRY_RUN
+
     def graphql(self, query: str, variables: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
         query = compress_query(query)
         if variables is None:
             variables = {}
+
+        url = "https://api.github.com/graphql"

Review Comment:
   consider making a class-level constant



##########
ci/scripts/github_pr_comment.py:
##########
@@ -0,0 +1,141 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import argparse
+import os
+import json
+
+from git_utils import git, GitHubRepo, parse_remote, DRY_RUN
+from cmd_utils import init_log
+from github_commenter import BotCommentBuilder
+from github_skipped_tests_comment import get_skipped_tests_comment
+from github_tag_teams import get_tags
+from github_docs_comment import get_doc_url
+
+PR_QUERY = """
+    query ($owner: String!, $name: String!, $number: Int!) {
+      repository(owner: $owner, name: $name) {
+        pullRequest(number: $number) {
+          title
+          body
+          state
+          number
+          author {
+            login
+          }
+          labels(first:100) {
+            nodes {
+              name
+            }
+          }
+          comments(last: 100) {
+            pageInfo {
+              hasPreviousPage
+            }
+            nodes {
+              author {
+                login
+              }
+              databaseId
+              body
+            }
+          }
+          commits(last: 1) {
+            nodes {
+              commit {
+                oid
+                statusCheckRollup {
+                  contexts(first: 100) {
+                    pageInfo {
+                      hasNextPage
+                    }
+                    nodes {
+                      ... on StatusContext {
+                        state
+                        context
+                        targetUrl
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+"""
+
+
+if __name__ == "__main__":
+    help = "Comment a welcome message on PRs"
+    parser = argparse.ArgumentParser(description=help)
+    parser.add_argument("--remote", default="origin", help="ssh remote to parse")
+    parser.add_argument("--pr", required=True)
+    parser.add_argument("--test-data", help="(testing) mock GitHub API data")
+    parser.add_argument("--test-comments", help="(testing) testing comments")
+    parser.add_argument(
+        "--dry-run",
+        action="store_true",
+        default=False,
+        help="run but don't send any request to GitHub",
+    )
+    args = parser.parse_args()
+    init_log()
+
+    remote = git(["config", "--get", f"remote.{args.remote}.url"])
+    user, repo = parse_remote(remote)
+
+    test_data = None
+    if args.test_data is not None:
+        test_data = json.loads(args.test_data)
+
+    github = GitHubRepo(
+        user=user,
+        repo=repo,
+        token=DRY_RUN if args.dry_run else os.environ["GITHUB_TOKEN"],
+        test_data=test_data,
+    )
+
+    pr_data = github.graphql(
+        PR_QUERY,
+        {
+            "owner": user,
+            "name": repo,
+            "number": int(args.pr),
+        },
+    )
+
+    pr_data = pr_data["data"]["repository"]["pullRequest"]
+    commenter = BotCommentBuilder(github=github, data=pr_data)
+
+    if args.test_comments is not None:
+        test_comments = json.loads(args.test_comments)
+        skipped_tests = test_comments["skipped-tests"]
+        ccs = test_comments["ccs"]
+        docs_info = test_comments["docs"]
+    else:
+        skipped_tests = get_skipped_tests_comment(pr_data, github=github)
+        ccs = get_tags(pr_data, github, team_issue=10317)
+        docs_info = get_doc_url(pr_data)
+
+    items = {
+        "ccs": ccs,

Review Comment:
   do we need to also keep any sorta central registry of these? could conceptually put these keys as an enum in/next to BotCommentBuilder



##########
ci/scripts/github_commenter.py:
##########
@@ -0,0 +1,130 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+import logging
+from typing import Dict, Tuple, Any, Optional, List, Union
+
+from git_utils import GitHubRepo
+
+BOT_COMMENT_START = "<!---bot-comment-->"
+WELCOME_TEXT = "Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @-ing them in a comment."
+
+
+class BotCommentBuilder:
+    def __init__(self, github: GitHubRepo, data: Dict[str, Any]):
+        self.github = github
+        self.pr_number = data["number"]
+        self.comment_data = data["comments"]["nodes"]
+        self.author = data["author"]["login"]
+
+    def find_bot_comment(self) -> Optional[Dict[str, Any]]:
+        """
+        Return the existing bot comment or None if it does not exist
+        """
+        for comment in self.comment_data:
+            logging.info(f"Checking comment {comment}")
+            if (
+                comment["author"]["login"] == "github-actions"
+                and BOT_COMMENT_START in comment["body"]
+            ):
+                logging.info("Found existing comment")
+                return comment
+        logging.info("No existing comment found")
+        return None
+
+    def find_existing_body(self) -> Dict[str, str]:
+        """
+        Find existing dynamic bullet point items
+        """
+        existing_comment = self.find_bot_comment()
+        if existing_comment is None:
+            logging.info(f"No existing comment while searching for body items")
+            return {}
+
+        matches = re.findall(
+            r"<!--bot-comment-([a-z][a-z-]+)-start-->([\S\s]*?)<!--bot-comment-([a-z-]+)-end-->",
+            existing_comment["body"],
+            flags=re.MULTILINE,
+        )
+        logging.info(f"Fetch body item matches: {matches}")
+
+        items = {}
+        for start, text, end in matches:
+            if start != end:
+                raise RuntimeError(
+                    f"Malformed comment found: {start} marker did not have matching end, found instead {end}"
+                )
+            items[start] = text.strip().lstrip("* ")

Review Comment:
   why lstrip the `* `?



##########
ci/scripts/github_tag_teams.py:
##########
@@ -209,37 +250,31 @@ def gen_cc_line(users):
             print(f"Terminating since {pr['number']} is a draft")
             exit(0)
 
-    # PRs/issues have the same structure for the fields needed here
+    # # PRs/issues have the same structure for the fields needed here

Review Comment:
   nit: revert



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org