Unverified Commit 78fc1434 authored by Philip Meier's avatar Philip Meier Committed by GitHub
Browse files

Fix workflow for pinging maintainers in case of unlabeled PRs (#4021)

* add additional workflow trigger

* [skip ci] fix name of script for PR label processing (#4013)

* trigger CI

* fix workflow in case commit has no associated PR

* [skip CI] test PR for label-checking workflow (#4015)

* [skip CI] fix PR labels processing logic (#4016)

* [skip CI] try fix trigger for unlabeled PR pings (#4017)

* [skip CI] remove quotes around users ping (#4018)

* [skip CI] cleanup PR labeling workflow (#4019)

* [skip CI] fix ping message for unlabeled PRs (#4020)

* cleanup
parent 4db4d157
import json """
This script finds all responsible users for labeling a PR by a commit SHA. It is used by the workflow in
'.github/workflows/pr-labels.yml'. If there exists no PR associated with the commit or the PR is properly labeled,
this script is a no-op.
"""
import sys import sys
from typing import Any, Dict, Set, Tuple from typing import Any, Optional, Set, Tuple
import requests import requests
# If the PR has any of these labels, we mark it as properly labeled. # If the PR has any of these labels, we accept it as properly labeled.
REQUIRED_LABELS = { REQUIRED_LABELS = {
"new feature", "new feature",
"bug", "bug",
...@@ -33,34 +38,35 @@ REQUIRED_LABELS = { ...@@ -33,34 +38,35 @@ REQUIRED_LABELS = {
} }
def main(commit_hash: str) -> Dict[str, Any]: def find_responsible_users(commit_hash: str) -> Set[str]:
pr_number = get_pr_number(commit_hash) pr_number = get_pr_number(commit_hash)
if not pr_number:
return set()
merger, labels = get_pr_merger_and_labels(pr_number) merger, labels = get_pr_merger_and_labels(pr_number)
is_properly_labeled = bool(REQUIRED_LABELS.intersection(labels)) is_properly_labeled = bool(REQUIRED_LABELS.intersection(labels))
if not is_properly_labeled: if is_properly_labeled:
users = {merger, *get_pr_reviewers(pr_number)} return set()
else:
users = () return {merger, *get_pr_reviewers(pr_number)}
return dict(
is_properly_labeled=is_properly_labeled,
responsible_users=", ".join(sorted([f"@{user}" for user in users])),
)
def _query_torchvision(cmd: str, *, accept) -> Any: def query_torchvision(cmd: str, *, accept) -> Any:
response = requests.get(f"https://api.github.com/repos/pytorch/vision/{cmd}", headers=dict(Accept=accept)) response = requests.get(f"https://api.github.com/repos/pytorch/vision/{cmd}", headers=dict(Accept=accept))
return response.json() return response.json()
def get_pr_number(commit_hash: str) -> int: def get_pr_number(commit_hash: str) -> Optional[int]:
# See https://docs.github.com/en/rest/reference/repos#list-pull-requests-associated-with-a-commit # See https://docs.github.com/en/rest/reference/repos#list-pull-requests-associated-with-a-commit
data = _query_torchvision(f"commits/{commit_hash}/pulls", accept="application/vnd.github.groot-preview+json") data = query_torchvision(f"commits/{commit_hash}/pulls", accept="application/vnd.github.groot-preview+json")
if not data:
return None
return data[0]["number"] return data[0]["number"]
def get_pr_merger_and_labels(pr_number: int) -> Tuple[str, Set[str]]: def get_pr_merger_and_labels(pr_number: int) -> Tuple[str, Set[str]]:
# See https://docs.github.com/en/rest/reference/pulls#get-a-pull-request # See https://docs.github.com/en/rest/reference/pulls#get-a-pull-request
data = _query_torchvision(f"pulls/{pr_number}", accept="application/vnd.github.v3+json") data = query_torchvision(f"pulls/{pr_number}", accept="application/vnd.github.v3+json")
merger = data["merged_by"]["login"] merger = data["merged_by"]["login"]
labels = {label["name"] for label in data["labels"]} labels = {label["name"] for label in data["labels"]}
return merger, labels return merger, labels
...@@ -68,11 +74,11 @@ def get_pr_merger_and_labels(pr_number: int) -> Tuple[str, Set[str]]: ...@@ -68,11 +74,11 @@ def get_pr_merger_and_labels(pr_number: int) -> Tuple[str, Set[str]]:
def get_pr_reviewers(pr_number: int) -> Set[str]: def get_pr_reviewers(pr_number: int) -> Set[str]:
# See https://docs.github.com/en/rest/reference/pulls#list-reviews-for-a-pull-request # See https://docs.github.com/en/rest/reference/pulls#list-reviews-for-a-pull-request
data = _query_torchvision(f"pulls/{pr_number}/reviews", accept="application/vnd.github.v3+json") data = query_torchvision(f"pulls/{pr_number}/reviews", accept="application/vnd.github.v3+json")
return {review["user"]["login"] for review in data if review["state"] == "APPROVED"} return {review["user"]["login"] for review in data if review["state"] == "APPROVED"}
if __name__ == "__main__": if __name__ == "__main__":
commit_hash = sys.argv[1] commit_hash = sys.argv[1]
data = main(commit_hash) users = find_responsible_users(commit_hash)
print(json.dumps(data)) print(", ".join(sorted([f"@{user}" for user in users])))
...@@ -19,21 +19,17 @@ jobs: ...@@ -19,21 +19,17 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Process PR associated with current commit - name: Process commit and find users responsible for labeling
id: data id: commit
run: | run: echo "::set-output name=responsible_users::$(python .github/process_commit.py ${{ github.sha }})"
DATA=$(python .github/foo.py ${{ github.sha }})
jq . <<< $DATA - name: Ping users responsible for labeling if necessary
echo "::set-output name=is_properly_labeled::$(jq .is_properly_labeled <<< $DATA)" if: ${{ steps.commit.outputs.responsible_users != '' }}
echo "::set-output name=responsible_users::$(jq .responsible_users <<< $DATA)"
- name: Ping responsible users
if: ${{ !steps.data.outputs.is_properly_labeled }}
uses: mshick/add-pr-comment@v1 uses: mshick/add-pr-comment@v1
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
message: | message: |
Hey ${{ steps.data.outputs.responsible_users }}! Hey ${{ steps.commit.outputs.responsible_users }}!
You approved or merged this PR, but no labels were added. You approved or merged this PR, but no labels were added.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment