[llvm] [GitHub][workflows] Add buildbot information comment to first merged PR from a new contributor (PR #78292)

David Spickett via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 16 07:33:21 PST 2024


https://github.com/DavidSpickett updated https://github.com/llvm/llvm-project/pull/78292

>From 20822b4a2f8e228365c8fa912af18afc9956749e Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Tue, 16 Jan 2024 13:36:15 +0000
Subject: [PATCH 1/2] [GitHub][workflows] Add buildbot information comment to
 first merged PR from a new contributor

This change adds a comment to the first PR from a new contributor that is merged,
which tells them what to expect post merge from the build bots.

How they will be notified, where to ask questions, that you're more likely
to be reverted than in other projects, etc.

To do this, I have added a hidden HTML comment to the new contributor greeting comment.
This workflow will look for that to tell if the author of the PR was
a new contributor at the time they opened the merge.

It has to be done this way because as soon as the PR is merged, they are
by GitHub's definition no longer a new contributor and I suspect that
their author assocication will be "contributor" instead.

I cannot 100% confirm that without a whole lot of effort and probably breaking
GitHub's terms of service, but it's fairly cheap to work around anyway.

It seems rare / almost impossible to reopen a PR in llvm at least, but
in case it does happen the buildbot info comment has its own hidden
HTML comment. If we find this we will not post another copy of the
same information.

An example PR can be found here: https://github.com/DavidSpickett/llvm-project/pull/84
(exact text content subject to change)
---
 .github/workflows/merged-prs.yml    | 36 +++++++++++++++
 llvm/utils/git/github-automation.py | 71 +++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
 create mode 100644 .github/workflows/merged-prs.yml

diff --git a/.github/workflows/merged-prs.yml b/.github/workflows/merged-prs.yml
new file mode 100644
index 00000000000000..1b1503610dac13
--- /dev/null
+++ b/.github/workflows/merged-prs.yml
@@ -0,0 +1,36 @@
+name: "Add buildbot information to first PRs from new contributors"
+
+permissions:
+  contents: read
+
+on:
+  # It's safe to use pull_request_target here, because we aren't checking out
+  # code from the pull request branch.
+  # See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
+  pull_request_target:
+    types:
+      - closed
+
+jobs:
+  buildbot_comment:
+    runs-on: ubuntu-latest
+    permissions:
+      pull-requests: write
+    if: >-
+      (github.repository == 'llvm/llvm-project') &&
+      (github.event.pull_request.merged == true)
+    steps:
+      - name: Setup Automation Script
+        run: |
+          curl -O -L --fail https://raw.githubusercontent.com/"$GITHUB_REPOSITORY"/main/llvm/utils/git/github-automation.py
+          curl -O -L --fail https://raw.githubusercontent.com/"$GITHUB_REPOSITORY"/main/llvm/utils/git/requirements.txt
+          chmod a+x github-automation.py
+          pip install -r requirements.txt
+
+      - name: Add Buildbot information comment
+        run: |
+          ./github-automation.py \
+            --token '${{ secrets.GITHUB_TOKEN }}' \
+            pr-buildbot-information \
+            --issue-number "${{ github.event.pull_request.number }}" \
+            --author "${{ github.event.pull_request.user.login }}"
diff --git a/llvm/utils/git/github-automation.py b/llvm/utils/git/github-automation.py
index f78d91059ecd36..55659679536f48 100755
--- a/llvm/utils/git/github-automation.py
+++ b/llvm/utils/git/github-automation.py
@@ -208,6 +208,8 @@ def _get_curent_team(self) -> Optional[github.Team.Team]:
 
 
 class PRGreeter:
+    COMMENT_TAG = "<!--LLVM NEW CONTRIBUTOR COMMENT-->\n"
+
     def __init__(self, token: str, repo: str, pr_number: int):
         repo = github.Github(token).get_repo(repo)
         self.pr = repo.get_issue(pr_number).as_pull_request()
@@ -217,7 +219,9 @@ def run(self) -> bool:
         # by a user new to LLVM and/or GitHub itself.
 
         # This text is using Markdown formatting.
+
         comment = f"""\
+{PRGreeter.COMMENT_TAG}
 Thank you for submitting a Pull Request (PR) to the LLVM Project!
 
 This PR will be automatically labeled and the relevant teams will be
@@ -240,6 +244,64 @@ def run(self) -> bool:
         return True
 
 
+class PRBuildbotInformation:
+    COMMENT_TAG = "<!--LLVM BUILDBOT INFORMATION COMMENT-->\n"
+
+    def __init__(self, token: str, repo: str, pr_number: int, author: str):
+        repo = github.Github(token).get_repo(repo)
+        self.pr = repo.get_issue(pr_number).as_pull_request()
+        self.author = author
+
+    def should_comment(self) -> bool:
+        # As soon as a new contributor has a PR merged, they are no longer a new contributor.
+        # We can tell that they were a new contributor previously because we would have
+        # added a new contributor greeting comment when they opened the PR.
+        found_greeting = False
+        for comment in self.pr.as_issue().get_comments():
+            if PRGreeter.COMMENT_TAG in comment.body:
+                found_greeting = True
+            elif PRBuildbotInformation.COMMENT_TAG in comment.body:
+                # When an issue is reopened, then closed as merged again, we should not
+                # add a second comment. This event will be rare in practice as it seems
+                # like it's only possible when the main branch is still at the exact
+                # revision that the PR was merged on to, beyond that it's closed forever.
+                return False
+        return found_greeting
+
+    def run(self) -> bool:
+        if not self.should_comment():
+            return
+
+        # This text is using Markdown formatting.
+        comment = f"""\
+{PRBuildbotInformation.COMMENT_TAG}
+@{self.author} Congratulations on having your first Pull Request (PR) merged into the LLVM Project!
+
+Your changes will be combined with recent changes from other authors, then tested
+by our [build bots](https://lab.llvm.org/buildbot/#/console).
+
+If there is a problem with the build, all the change authors will receive an email
+describing the problem. Please check whether the problem has been caused by your
+change, as the change set may include many authors. If the problem affects many
+configurations, you may get many emails for the same problem.
+
+If you are using a GitHub `noreply` email address, you will not receive these emails.
+Instead, someone will comment on this PR to inform you of the issue.
+
+If you do not receive any reports of problems, no action is required from you.
+Your changes are working as expected, well done!
+
+If your change causes an ongoing issue, it may be reverted. This is a [normal part of LLVM development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy)
+and is not a comment on yourself as an author.The revert commit (or a comment on this PR)
+should explain why it was reverted, how to fix your changes and merge them again.
+
+If you are unsure how to fix a problem, you can send questions in a reply
+to the notification email, add a comment to this PR, or ask on [Discord](https://discord.com/invite/xS7Z362).
+"""
+        self.pr.as_issue().create_comment(comment)
+        return True
+
+
 def setup_llvmbot_git(git_dir="."):
     """
     Configure the git repo in `git_dir` with the llvmbot account so
@@ -698,6 +760,10 @@ def execute_command(self) -> bool:
 pr_greeter_parser = subparsers.add_parser("pr-greeter")
 pr_greeter_parser.add_argument("--issue-number", type=int, required=True)
 
+pr_buildbot_information_parser = subparsers.add_parser("pr-buildbot-information")
+pr_buildbot_information_parser.add_argument("--issue-number", type=int, required=True)
+pr_buildbot_information_parser.add_argument("--author", type=str, required=True)
+
 release_workflow_parser = subparsers.add_parser("release-workflow")
 release_workflow_parser.add_argument(
     "--llvm-project-dir",
@@ -751,6 +817,11 @@ def execute_command(self) -> bool:
 elif args.command == "pr-greeter":
     pr_greeter = PRGreeter(args.token, args.repo, args.issue_number)
     pr_greeter.run()
+elif args.command == "pr-buildbot-information":
+    pr_buildbot_information = PRBuildbotInformation(
+        args.token, args.repo, args.issue_number, args.author
+    )
+    pr_buildbot_information.run()
 elif args.command == "release-workflow":
     release_workflow = ReleaseWorkflow(
         args.token,

>From cffa5066450936bcdb6966db1f972088b1274fad Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Tue, 16 Jan 2024 15:29:55 +0000
Subject: [PATCH 2/2] use sparse checkout

---
 .github/workflows/merged-prs.yml | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/merged-prs.yml b/.github/workflows/merged-prs.yml
index 1b1503610dac13..44332c1c9fe0bd 100644
--- a/.github/workflows/merged-prs.yml
+++ b/.github/workflows/merged-prs.yml
@@ -20,14 +20,19 @@ jobs:
       (github.repository == 'llvm/llvm-project') &&
       (github.event.pull_request.merged == true)
     steps:
+      - name: Checkout Automation Script
+        uses: actions/checkout at v4
+        with:
+          sparse-checkout: llvm/utils/git/
+
       - name: Setup Automation Script
+        working-directory: ./llvm/utils/git/
         run: |
-          curl -O -L --fail https://raw.githubusercontent.com/"$GITHUB_REPOSITORY"/main/llvm/utils/git/github-automation.py
-          curl -O -L --fail https://raw.githubusercontent.com/"$GITHUB_REPOSITORY"/main/llvm/utils/git/requirements.txt
           chmod a+x github-automation.py
           pip install -r requirements.txt
 
       - name: Add Buildbot information comment
+        working-directory: ./llvm/utils/git/
         run: |
           ./github-automation.py \
             --token '${{ secrets.GITHUB_TOKEN }}' \



More information about the llvm-commits mailing list