summaryrefslogtreecommitdiffstats
path: root/.github/workflows/request-reviews.yml
blob: b358ae089f1c112c7e9e4dd70f68675b6dedd27d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

# 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:
  pull_request_target:
    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:
      # 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
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          sparse-checkout: |
            .github
            BaseTools/Scripts
            Maintainers.txt

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'
          cache: 'pip'
          cache-dependency-path: '.github/scripts/requirements.txt'

      - name: Install PIP Modules
        run: pip install -r .github/scripts/requirements.txt --upgrade

      - 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: |
            import git
            import os
            import sys
            sys.path.append(os.path.join(os.environ['WORKSPACE_PATH'], ".github"))
            from scripts import GitHub

            WORKSPACE_PATH = os.environ['WORKSPACE_PATH']
            GET_MAINTAINER_LOCAL_PATH = os.path.join(WORKSPACE_PATH, os.environ['GET_MAINTAINER_REL_PATH'])

            pr_commit_sha = GitHub.get_pr_sha(os.environ['GH_TOKEN'], os.environ['ORG_NAME'], os.environ['REPO_NAME'], int(os.environ['PR_NUMBER']))
            if not pr_commit_sha:
                sys.exit(1)

            print(f"::notice title=PR Commit SHA::Looking at files in consolidated PR commit: {pr_commit_sha}")

            git.Repo(WORKSPACE_PATH).remotes.origin.fetch(pr_commit_sha, depth=1)

            reviewers = GitHub.get_reviewers_for_range(WORKSPACE_PATH, GET_MAINTAINER_LOCAL_PATH, pr_commit_sha, pr_commit_sha)
            if not reviewers:
                print("::notice title=No Reviewers Found!::No reviewers found for this PR.")
                sys.exit(1)

            print(f"::notice title=Reviewer List::Reviewers found for PR {os.environ['PR_NUMBER']}:\n{', '.join(reviewers)}")

            GitHub.add_reviewers_to_pr(os.environ['GH_TOKEN'], os.environ['ORG_NAME'], os.environ['REPO_NAME'], int(os.environ['PR_NUMBER']), reviewers)