1 | ## @file
|
---|
2 | # Used in a CI workflow to request reviewers for a pull request.
|
---|
3 | #
|
---|
4 | # Refer to the following link for a list of pre-defined GitHub workflow
|
---|
5 | # environment variables:
|
---|
6 | # https://docs.github.com/actions/reference/environment-variables
|
---|
7 | #
|
---|
8 | # Copyright (c) Microsoft Corporation.
|
---|
9 | # SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
10 | #
|
---|
11 |
|
---|
12 | import git
|
---|
13 | import GitHub
|
---|
14 | import os
|
---|
15 | import sys
|
---|
16 |
|
---|
17 |
|
---|
18 | """Request Pull Request Reviewers Helpers"""
|
---|
19 |
|
---|
20 |
|
---|
21 | def request_pr_reviewers():
|
---|
22 | """Request pull request reviewers for a GitHub PR.
|
---|
23 |
|
---|
24 | This function is intended to be used in a GitHub Actions workflow to
|
---|
25 | request reviewers for a pull request triggered by a GitHub event. The
|
---|
26 | function makes assumptions about GitHub workflow environment variables and
|
---|
27 | the pull request context in which it is run.
|
---|
28 |
|
---|
29 | The function will exit with a non-zero status indicating an error if a
|
---|
30 | critical error occurs during execution so the workflow fails.
|
---|
31 |
|
---|
32 | The following environment variables are expected to be set before calling
|
---|
33 | this function. The recommend GitHub context values are show for reference:
|
---|
34 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
---|
35 | ORG_NAME: ${{ github.repository_owner }}
|
---|
36 | PR_NUMBER: ${{ github.event.number}}
|
---|
37 | REPO_NAME: ${{ github.event.pull_request.base.repo.name }}
|
---|
38 | TARGET_BRANCH: ${{ github.event.pull_request.base.ref }}
|
---|
39 | WORKSPACE_PATH: ${{ github.workspace }}
|
---|
40 | """
|
---|
41 | WORKSPACE_PATH = os.environ["WORKSPACE_PATH"]
|
---|
42 | GET_MAINTAINER_LOCAL_PATH = os.path.join(
|
---|
43 | WORKSPACE_PATH, os.environ["GET_MAINTAINER_REL_PATH"]
|
---|
44 | )
|
---|
45 |
|
---|
46 | # Step 1: Get the GitHub created PR commit SHA (contains all changes in a single commit)
|
---|
47 | pr_commit_sha = GitHub.get_pr_sha(
|
---|
48 | os.environ["GH_TOKEN"],
|
---|
49 | os.environ["ORG_NAME"],
|
---|
50 | os.environ["REPO_NAME"],
|
---|
51 | int(os.environ["PR_NUMBER"]),
|
---|
52 | )
|
---|
53 | if not pr_commit_sha:
|
---|
54 | sys.exit(1)
|
---|
55 |
|
---|
56 | print(
|
---|
57 | f"::notice title=PR Commit SHA::Looking at files in consolidated PR commit: {pr_commit_sha}"
|
---|
58 | )
|
---|
59 |
|
---|
60 | # Step 2: Fetch only the PR commit to get the files changed in the PR
|
---|
61 | git.Repo(WORKSPACE_PATH).remotes.origin.fetch(pr_commit_sha, depth=1)
|
---|
62 |
|
---|
63 | # Step 3: Get the list of reviewers for the PR
|
---|
64 | reviewers = GitHub.get_reviewers_for_range(
|
---|
65 | WORKSPACE_PATH, GET_MAINTAINER_LOCAL_PATH, pr_commit_sha, pr_commit_sha
|
---|
66 | )
|
---|
67 | if not reviewers:
|
---|
68 | print("::notice title=No New Reviewers Found!::No reviewers found for this PR.")
|
---|
69 | sys.exit(0)
|
---|
70 |
|
---|
71 | print(
|
---|
72 | f"::notice title=Preliminary Reviewer List::Total reviewer candidates for "
|
---|
73 | f"PR {os.environ['PR_NUMBER']}: {', '.join(reviewers)}"
|
---|
74 | )
|
---|
75 |
|
---|
76 | # Step 4: Add the reviewers to the PR
|
---|
77 | # Note the final requested reviewer list in the workflow run for reference
|
---|
78 | new_reviewers = GitHub.add_reviewers_to_pr(
|
---|
79 | os.environ["GH_TOKEN"],
|
---|
80 | os.environ["ORG_NAME"],
|
---|
81 | os.environ["REPO_NAME"],
|
---|
82 | int(os.environ["PR_NUMBER"]),
|
---|
83 | reviewers,
|
---|
84 | )
|
---|
85 | if new_reviewers:
|
---|
86 | print(
|
---|
87 | f"::notice title=New Reviewers Added::New reviewers requested for PR "
|
---|
88 | f"{os.environ['PR_NUMBER']}: {', '.join(new_reviewers)}"
|
---|
89 | )
|
---|
90 | else:
|
---|
91 | print(
|
---|
92 | "::notice title=No New Reviewers Added::No reviewers were found that "
|
---|
93 | "should be newly requested."
|
---|
94 | )
|
---|
95 |
|
---|
96 |
|
---|
97 | if __name__ == '__main__':
|
---|
98 | request_pr_reviewers()
|
---|