Skip to content

ping maintainers in case a PR was not properly labeled #3993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/process_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import json
import sys
from typing import Any, Dict, Set, Tuple

import requests

# If the PR has any of these labels, we mark it as properly labeled.
REQUIRED_LABELS = {
"new feature",
"bug",
"code quality",
"enhancement",
"bc-breaking",
"dependency issue",
"deprecation",
"module: c++ frontend",
"module: ci",
"module: datasets",
"module: documentation",
"module: io",
"module: models.quantization",
"module: models",
"module: onnx",
"module: ops",
"module: reference scripts",
"module: rocm",
"module: tests",
"module: transforms",
"module: utils",
"module: video",
"Perf",
"Revert(ed)",
}


def main(commit_hash: str) -> Dict[str, Any]:
pr_number = get_pr_number(commit_hash)
merger, labels = get_pr_merger_and_labels(pr_number)
is_properly_labeled = bool(REQUIRED_LABELS.intersection(labels))
if not is_properly_labeled:
users = {merger, *get_pr_reviewers(pr_number)}
else:
users = ()
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:
response = requests.get(f"https://api.github.com/repos/pytorch/vision/{cmd}", headers=dict(Accept=accept))
return response.json()


def get_pr_number(commit_hash: str) -> int:
# 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")
return data[0]["number"]


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
data = _query_torchvision(f"pulls/{pr_number}", accept="application/vnd.github.v3+json")
merger = data["merged_by"]["login"]
labels = {label["name"] for label in data["labels"]}
return merger, labels


def get_pr_reviewers(pr_number: int) -> Set[str]:
# 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")
return {review["user"]["login"] for review in data if review["state"] == "APPROVED"}


if __name__ == "__main__":
commit_hash = sys.argv[1]
data = main(commit_hash)
print(json.dumps(data))
39 changes: 39 additions & 0 deletions .github/workflows/pr-labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: pr-labels

on:
push:
branches:
- master

jobs:
is-properly-labeled:
runs-on: ubuntu-latest

steps:
- name: Set up python
uses: actions/setup-python@v2

- name: Install requests
run: pip install requests

- name: Checkout repository
uses: actions/checkout@v2

- name: Process PR associated with current commit
id: data
run: |
DATA=$(python .github/foo.py ${{ github.sha }})
jq . <<< $DATA
echo "::set-output name=is_properly_labeled::$(jq .is_properly_labeled <<< $DATA)"
echo "::set-output name=responsible_users::$(jq .responsible_users <<< $DATA)"

- name: Ping responsible users
if: steps.data.outputs.is_properly_labeled == "false"
uses: mshick/add-pr-comment@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
message: |
Hey ${{ steps.data.outputs.responsible_users }}!

You approved or merged this PR, but no labels were added.