GitHub CI: Add filter for newly created PRs, and labels map in config.json (#1532)

* Add filter for newly created PRs, and labels map in config.json

* Fix label name
This commit is contained in:
Alexander Zhogov 2020-07-31 10:43:22 +03:00 committed by GitHub
parent d8502bf7d3
commit c5bac5a1b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 20 deletions

View File

@ -8,6 +8,7 @@ Check GitHub PRs and set labels by type and categories, e.g. 'ExternalPR', 'cate
# pylint: disable=fixme,no-member # pylint: disable=fixme,no-member
import re import re
import datetime
from argparse import ArgumentParser from argparse import ArgumentParser
from enum import Enum from enum import Enum
@ -18,8 +19,8 @@ from configs import Config
class PrType(Enum): class PrType(Enum):
"""Constants for type of GitHub pull request by author membership""" """Constants for type of GitHub pull request by author membership"""
EXTERNAL = 'ExternalPR' EXTERNAL = 'ExternalPR'
INTEL = 'IntelDevPR' INTEL = 'ExternalIntelPR'
ORG = 'OpenvinoDevPR' ORG = 'OpenvinoPR'
BAD = 'BadPR' BAD = 'BadPR'
@ -31,16 +32,15 @@ def get_pr_labels(pull):
return pr_lables return pr_lables
def set_pr_label(pull, labels): def set_pr_labels(pull, labels):
"""Sets PR labels""" """Sets PR labels"""
if not labels or Config().DRY_RUN: if not labels or Config().DRY_RUN:
return return
print(f'Set PR labels:', labels) print(f'Set PR labels:', labels)
# TODO: Review labels and enable. Check setting existing labels pull.set_labels(labels)
#pull.set_labels(labels)
def get_pr_type(pull): def get_pr_type_by_labels(pull):
"""Gets PR type using labels""" """Gets PR type using labels"""
pr_lables = get_pr_labels(pull) pr_lables = get_pr_labels(pull)
pr_types = set(type.value for type in PrType) pr_types = set(type.value for type in PrType)
@ -53,8 +53,8 @@ def get_pr_type(pull):
return PrType(PrType(pr_types_labels.pop())) return PrType(PrType(pr_types_labels.pop()))
def get_label_by_team_name(team_name): def get_label_by_team_name_re(team_name):
"""Generates label by PR reviwer team name""" """Generates label by PR reviwer team name using regular expressions"""
if 'admins' in team_name: if 'admins' in team_name:
return 'category: ci' return 'category: ci'
re_compile_label = re.compile(rf'{Config().GITHUB_REPO}-(.+)-maintainers') re_compile_label = re.compile(rf'{Config().GITHUB_REPO}-(.+)-maintainers')
@ -64,12 +64,17 @@ def get_label_by_team_name(team_name):
return None return None
def get_label_by_team_name_map(team_name):
"""Generates label by PR reviwer team name using config map"""
return Config().TEAM_TO_LABEL.get(team_name)
def get_category_labels(pull): def get_category_labels(pull):
"""Gets list of category labels by all PR reviwer teams""" """Gets list of category labels by all PR reviwer teams"""
labels = [] labels = []
pr_lables = get_pr_labels(pull) pr_lables = get_pr_labels(pull)
for reviewer_team in pull.get_review_requests()[1]: for reviewer_team in pull.get_review_requests()[1]:
reviewer_label = get_label_by_team_name(reviewer_team.name) reviewer_label = get_label_by_team_name_map(reviewer_team.name)
if reviewer_label and reviewer_label not in pr_lables: if reviewer_label and reviewer_label not in pr_lables:
labels.append(reviewer_label) labels.append(reviewer_label)
return labels return labels
@ -84,6 +89,8 @@ def main():
help="Get GitHub pull request with the number") help="Get GitHub pull request with the number")
arg_parser.add_argument("--pr-state", default="open", choices=["open", "closed"], arg_parser.add_argument("--pr-state", default="open", choices=["open", "closed"],
help="Set GitHub pull request state") help="Set GitHub pull request state")
arg_parser.add_argument("--newer", metavar="MINUTES",
help="Get newly created GitHub pull request only")
args, unknown_args = arg_parser.parse_known_args() args, unknown_args = arg_parser.parse_known_args()
Config(args.cfg_file, unknown_args) Config(args.cfg_file, unknown_args)
@ -93,40 +100,44 @@ def main():
pulls = [gh_api.repo.get_pull(int(args.pr))] pulls = [gh_api.repo.get_pull(int(args.pr))]
else: else:
pulls = gh_api.repo.get_pulls(state=args.pr_state) pulls = gh_api.repo.get_pulls(state=args.pr_state)
print(f'PRs count ({args.pr_state}):', pulls.totalCount) print(f'\nPRs count ({args.pr_state}):', pulls.totalCount)
if args.newer:
pr_created_after = datetime.datetime.now() - datetime.timedelta(minutes=int(args.newer))
print('PRs created after:', pr_created_after)
non_org_intel_pr_users = set() non_org_intel_pr_users = set()
non_org_pr_users = set() non_org_pr_users = set()
for pull in pulls: for pull in pulls:
if args.newer and pull.created_at <= pr_created_after:
print(f'\nIGNORE: {pull} - Created: {pull.created_at}')
continue
pr_lables = get_pr_labels(pull) pr_lables = get_pr_labels(pull)
pr_type = get_pr_type(pull) pr_type_by_labels = get_pr_type_by_labels(pull)
set_labels = [] set_labels = []
print('\n', pull, f'- Labels: {pr_lables} -', f'Type: {pr_type}', end='') print(f'\n{pull} - Created: {pull.created_at} - Labels: {pr_lables} -',
f'Type: {pr_type_by_labels}', end='')
# Checks PR source type # Checks PR source type
if gh_api.is_org_user(pull.user): if gh_api.is_org_user(pull.user):
print(' - Org user') print(' - Org user')
if pr_type is not PrType.ORG:
print(f'NO "{PrType.ORG.value}" label: ', end='')
github_api.print_users(pull.user)
set_labels.append(PrType.ORG.value)
elif github_api.is_intel_email(pull.user.email) or \ elif github_api.is_intel_email(pull.user.email) or \
github_api.is_intel_company(pull.user.company): github_api.is_intel_company(pull.user.company):
print(' - Non org user with Intel email or company') print(' - Non org user with Intel email or company')
non_org_intel_pr_users.add(pull.user) non_org_intel_pr_users.add(pull.user)
if pr_type is not PrType.INTEL: if pr_type_by_labels is not PrType.INTEL:
print(f'NO "{PrType.INTEL.value}" label: ', end='') print(f'NO "{PrType.INTEL.value}" label: ', end='')
github_api.print_users(pull.user) github_api.print_users(pull.user)
set_labels.append(PrType.INTEL.value) set_labels.append(PrType.INTEL.value)
else: else:
print(f' - Non org user with NO Intel email or company') print(f' - Non org user with NO Intel email or company')
non_org_pr_users.add(pull.user) non_org_pr_users.add(pull.user)
if pr_type is not PrType.EXTERNAL: if pr_type_by_labels is not PrType.EXTERNAL:
print(f'NO "{PrType.EXTERNAL.value}" label: ', end='') print(f'NO "{PrType.EXTERNAL.value}" label: ', end='')
github_api.print_users(pull.user) github_api.print_users(pull.user)
set_labels.append(PrType.EXTERNAL.value) set_labels.append(PrType.EXTERNAL.value)
set_labels += get_category_labels(pull) set_labels += get_category_labels(pull)
set_pr_label(pull, set_labels) set_pr_labels(pull, set_labels)
print(f'\nNon org user with Intel email or company:') print(f'\nNon org user with Intel email or company:')
github_api.print_users(non_org_intel_pr_users) github_api.print_users(non_org_intel_pr_users)

View File

@ -14,5 +14,23 @@
"HTTPS_PROXY": null, "HTTPS_PROXY": null,
"NO_PROXY": "localhost,127.0.0.1,.intel.com" "NO_PROXY": "localhost,127.0.0.1,.intel.com"
}, },
"DRY_RUN": false "DRY_RUN": false,
"TEAM_TO_LABEL": {
"openvino-admins": "category: CI",
"openvino-maintainers": "category: IE common",
"openvino-docs-maintainers": "category: docs",
"openvino-ie-maintainers": "category: IE common",
"openvino-ie-cpu-maintainers": "category: CPU",
"openvino-ie-gna-maintainers": "category: GNA",
"openvino-ie-gpu-maintainers": "category: GPU",
"openvino-ie-lpt-maintainers": "category: LP transformations",
"openvino-ie-multi-maintainers": "category: MULTI",
"openvino-ie-python-api-maintainers": "category: python api",
"openvino-ie-tests-maintainers": "category: IE Tests",
"openvino-ie-vpu-maintainers": "category: VPU",
"openvino-mo-maintainers": "category: MO",
"openvino-ngraph-maintainers": "category: nGraph",
"openvino-tests-maintainers": "category: IE Tests",
"openvino-tools-maintainers": "category: tools"
}
} }