diff --git a/.github/scripts/label_title_regex.py b/.github/scripts/label_title_regex.py index 26fc02b3aef38..d1b59ca4da343 100644 --- a/.github/scripts/label_title_regex.py +++ b/.github/scripts/label_title_regex.py @@ -1,14 +1,19 @@ """Labels PRs based on title. Must be run in a github action with the pull_request_target event.""" -from ghapi.all import context_github -from ghapi.all import GhApi -from ghapi.all import user_repo -from ghapi.all import github_token +from github import Github +import os +import json import re -owner, repo = user_repo() -pull_request = context_github.event.pull_request -title = pull_request.title +context_dict = json.loads(os.getenv("CONTEXT_GITHUB")) + +repo = context_dict["repository"] +g = Github(context_dict["token"]) +repo = g.get_repo(repo) +pr_number = context_dict["event"]["number"] +issue = repo.get_issue(number=pr_number) +title = issue.title + regex_to_labels = [ (r"\bDOC\b", "Documentation"), @@ -21,5 +26,4 @@ ] if labels_to_add: - api = GhApi(owner=owner, repo=repo, token=github_token()) - api.issues.add_labels(pull_request.number, labels=labels_to_add) + issue.add_to_labels(*labels_to_add) diff --git a/.github/workflows/labeler-title-regex.yml b/.github/workflows/labeler-title-regex.yml index e3c0812029d1b..aaca442616d40 100644 --- a/.github/workflows/labeler-title-regex.yml +++ b/.github/workflows/labeler-title-regex.yml @@ -3,6 +3,10 @@ on: pull_request_target: types: [opened, edited] +permissions: + contents: read + pull-requests: write + jobs: labeler: @@ -12,8 +16,8 @@ jobs: - uses: actions/setup-python@v2 with: python-version: '3.9' - - name: Install ghapi - run: pip install -Uq ghapi + - name: Install PyGithub + run: pip install -Uq PyGithub - name: Label pull request run: python .github/scripts/label_title_regex.py env: