2024-07-22 23:42:43 +02:00
|
|
|
|
|
|
|
# This workflow automatically adds the appropriate reviewers to a pull request.
|
|
|
|
#
|
|
|
|
# The workflow directly reuses logic in the BaseTools/Scripts/GetMaintainer.py script
|
|
|
|
# to determine the appropriate reviewers, so it matches what a user would see running
|
|
|
|
# the script locally.
|
|
|
|
#
|
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause-Patent
|
|
|
|
|
|
|
|
name: Add Pull Request Reviewers
|
|
|
|
|
|
|
|
on:
|
2024-07-25 13:43:09 +02:00
|
|
|
pull_request_target:
|
2024-07-22 23:42:43 +02:00
|
|
|
branches:
|
|
|
|
- master
|
|
|
|
types: [opened, ready_for_review, reopened, synchronize]
|
|
|
|
|
|
|
|
env:
|
|
|
|
GET_MAINTAINER_REL_PATH: "BaseTools/Scripts/GetMaintainer.py"
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
auto-request-review:
|
|
|
|
name: Add Pull Request Reviewers
|
|
|
|
# Do not run on draft PRs and only run on PRs in the tianocore organization
|
|
|
|
if: ${{ github.event.pull_request.draft == false && github.repository_owner == 'tianocore' }}
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
|
|
|
|
permissions:
|
|
|
|
contents: read
|
|
|
|
issues: write
|
|
|
|
pull-requests: write
|
|
|
|
|
|
|
|
steps:
|
2024-08-01 00:18:23 +02:00
|
|
|
# Reduce checkout time with sparse-checkout
|
|
|
|
# - .github: Contains the scripts to interact with Github and add reviewers
|
|
|
|
# - BaseTools/Scripts: Contains the GetMaintainer.py script
|
|
|
|
# - Maintainers.txt: Contains the list of maintainers for the repository
|
2024-07-22 23:42:43 +02:00
|
|
|
- name: Checkout repository
|
|
|
|
uses: actions/checkout@v4
|
|
|
|
with:
|
2024-08-01 00:18:23 +02:00
|
|
|
fetch-depth: 1
|
|
|
|
sparse-checkout: |
|
|
|
|
.github
|
|
|
|
BaseTools/Scripts
|
|
|
|
Maintainers.txt
|
2024-07-22 23:42:43 +02:00
|
|
|
|
2024-08-01 00:21:30 +02:00
|
|
|
- name: Setup Python
|
2024-07-22 23:42:43 +02:00
|
|
|
uses: actions/setup-python@v5
|
|
|
|
with:
|
|
|
|
python-version: '3.x'
|
2024-08-01 00:21:30 +02:00
|
|
|
cache: 'pip'
|
|
|
|
cache-dependency-path: '.github/scripts/requirements.txt'
|
2024-07-22 23:42:43 +02:00
|
|
|
|
|
|
|
- name: Install PIP Modules
|
2024-08-01 00:21:30 +02:00
|
|
|
run: pip install -r .github/scripts/requirements.txt --upgrade
|
2024-07-22 23:42:43 +02:00
|
|
|
|
|
|
|
- name: Add Reviewers to Pull Request
|
|
|
|
shell: python
|
|
|
|
env:
|
|
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
ORG_NAME: ${{ github.repository_owner }}
|
|
|
|
PR_NUMBER: ${{ github.event.number}}
|
|
|
|
REPO_NAME: ${{ github.event.pull_request.base.repo.name }}
|
|
|
|
TARGET_BRANCH: ${{ github.event.pull_request.base.ref }}
|
|
|
|
WORKSPACE_PATH: ${{ github.workspace }}
|
|
|
|
run: |
|
2024-08-01 00:28:27 +02:00
|
|
|
import git
|
2024-07-22 23:42:43 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2024-08-01 01:06:24 +02:00
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.environ["WORKSPACE_PATH"], ".github"))
|
2024-07-22 23:42:43 +02:00
|
|
|
from scripts import GitHub
|
|
|
|
|
2024-08-01 01:06:24 +02:00
|
|
|
WORKSPACE_PATH = os.environ["WORKSPACE_PATH"]
|
|
|
|
GET_MAINTAINER_LOCAL_PATH = os.path.join(
|
|
|
|
WORKSPACE_PATH, os.environ["GET_MAINTAINER_REL_PATH"]
|
|
|
|
)
|
2024-07-22 23:42:43 +02:00
|
|
|
|
2024-08-01 01:04:06 +02:00
|
|
|
# Step 1: Get the GitHub created PR commit SHA (contains all changes in a single commit)
|
2024-08-01 01:06:24 +02:00
|
|
|
pr_commit_sha = GitHub.get_pr_sha(
|
|
|
|
os.environ["GH_TOKEN"],
|
|
|
|
os.environ["ORG_NAME"],
|
|
|
|
os.environ["REPO_NAME"],
|
|
|
|
int(os.environ["PR_NUMBER"]),
|
|
|
|
)
|
2024-07-25 13:43:09 +02:00
|
|
|
if not pr_commit_sha:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2024-08-01 01:06:24 +02:00
|
|
|
print(
|
|
|
|
f"::notice title=PR Commit SHA::Looking at files in consolidated PR commit: {pr_commit_sha}"
|
|
|
|
)
|
2024-07-25 13:43:09 +02:00
|
|
|
|
2024-08-01 01:04:06 +02:00
|
|
|
# Step 2: Fetch only the PR commit to get the files changed in the PR
|
2024-08-01 00:28:27 +02:00
|
|
|
git.Repo(WORKSPACE_PATH).remotes.origin.fetch(pr_commit_sha, depth=1)
|
2024-07-25 13:43:09 +02:00
|
|
|
|
2024-08-01 01:04:06 +02:00
|
|
|
# Step 3: Get the list of reviewers for the PR
|
2024-08-01 01:06:24 +02:00
|
|
|
reviewers = GitHub.get_reviewers_for_range(
|
|
|
|
WORKSPACE_PATH, GET_MAINTAINER_LOCAL_PATH, pr_commit_sha, pr_commit_sha
|
|
|
|
)
|
2024-07-22 23:42:43 +02:00
|
|
|
if not reviewers:
|
2024-08-01 00:51:12 +02:00
|
|
|
print("::notice title=No New Reviewers Found!::No reviewers found for this PR.")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
print(
|
|
|
|
f"::notice title=Preliminary Reviewer List::Total reviewer candidates for "
|
|
|
|
f"PR {os.environ['PR_NUMBER']}: {', '.join(reviewers)}"
|
|
|
|
)
|
|
|
|
|
2024-08-01 01:04:06 +02:00
|
|
|
# Step 4: Add the reviewers to the PR
|
|
|
|
# Note the final requested reviewer list in the workflow run for reference
|
2024-08-01 00:51:12 +02:00
|
|
|
new_reviewers = GitHub.add_reviewers_to_pr(
|
|
|
|
os.environ["GH_TOKEN"],
|
|
|
|
os.environ["ORG_NAME"],
|
|
|
|
os.environ["REPO_NAME"],
|
|
|
|
int(os.environ["PR_NUMBER"]),
|
|
|
|
reviewers,
|
|
|
|
)
|
|
|
|
if new_reviewers:
|
|
|
|
print(
|
|
|
|
f"::notice title=New Reviewers Added::New reviewers requested for PR "
|
|
|
|
f"{os.environ['PR_NUMBER']}: {', '.join(new_reviewers)}"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
print(
|
|
|
|
"::notice title=No New Reviewers Added::No reviewers were found that "
|
|
|
|
"should be newly requested."
|
|
|
|
)
|