Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 31, 2024
Merged
41 changes: 41 additions & 0 deletions .github/workflows/merged-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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: Checkout Automation Script
uses: actions/checkout@v4
with:
sparse-checkout: llvm/utils/git/
ref: main

- name: Setup Automation Script
working-directory: ./llvm/utils/git/
run: |
pip install -r requirements.txt

- name: Add Buildbot information comment
working-directory: ./llvm/utils/git/
run: |
python3 ./github-automation.py \
--token '${{ secrets.GITHUB_TOKEN }}' \
pr-buildbot-information \
--issue-number "${{ github.event.pull_request.number }}" \
--author "${{ github.event.pull_request.user.login }}"
75 changes: 75 additions & 0 deletions llvm/utils/git/github-automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -240,6 +244,68 @@ 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/).

If there is a problem with a build, all the change authors will receive an email
describing the problem. Please check whether the problem has been caused by your
change specifically, 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.

You can also track the progress of your change using the [build bot console view](https://lab.llvm.org/buildbot/#/console).
However, this page only shows recent changes. If yours is not listed there, rely on the notifications.

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 and how to fix your changes. Please open a new
PR with the fixed changes and describe what was done to fix them.

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
Expand Down Expand Up @@ -698,6 +764,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",
Expand Down Expand Up @@ -751,6 +821,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,
Expand Down