[llvm] [GitHub][workflows] Ask reviewers to merge PRs when author can not (PR #81142)

Tom Stellard via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 12 06:20:58 PST 2024


================
@@ -298,6 +298,44 @@ def run(self) -> bool:
         return True
 
 
+class PRMergeOnBehalfInformation:
+    COMMENT_TAG = "<!--LLVM MERGE ON BEHALF INFORMATION COMMENT-->\n"
+
+    def __init__(self, token: str, repo: str, pr_number: int, author: str):
+        self.repo = github.Github(token).get_repo(repo)
+        self.pr = self.repo.get_issue(pr_number).as_pull_request()
+        self.author = author
+
+    def run(self) -> bool:
+        # Check this first because it only costs 1 API point.
+        try:
+            if self.repo.get_collaborator_permission(self.author) in ["admin", "write"]:
+                return
+        # There is a UnknownObjectException for this scenario, but this method
+        # does not use it.
+        except github.GithubException as e:
+            # 404 means the author was not found in the collaborator list, so we
+            # know they don't have push permissions. Anything else is a real API
+            # issue, raise it so it is visible.
+            if e.status != 404:
+                raise e
+
+        # A review can be approved more than once, only comment the first time.
+        for comment in self.pr.as_issue().get_comments():
+            if self.COMMENT_TAG in comment.body:
+                return
+
+        # This text is using Markdown formatting.
+        comment = f"""\
+{self.COMMENT_TAG}
+@{self.author} you do not have permissions to merge your own PRs yet. Please let us know when you are happy for this to be merged, and one of the reviewers can merge it on your behalf.
+
+(if many approvals are required, please wait until everyone has approved before merging)
----------------
tstellar wrote:

I think this message should be directed at the reviewer, not the author.

https://github.com/llvm/llvm-project/pull/81142


More information about the llvm-commits mailing list