@@ -1359,6 +1359,95 @@ def merge_url(gh):
13591359 print_warning ("Review indicates this PR should not be merged (use -f/--force to do so anyway)" )
13601360
13611361
1362+ def det_pr_labels (file_info , pr_target_repo ):
1363+ """
1364+ Determine labels for a pull request based on provided information on files changed by that pull request.
1365+ """
1366+ labels = []
1367+ if pr_target_repo == GITHUB_EASYCONFIGS_REPO :
1368+ if any (file_info ['new_folder' ]):
1369+ labels .append ('new' )
1370+ if any (file_info ['new_file_in_existing_folder' ]):
1371+ labels .append ('update' )
1372+ elif pr_target_repo == GITHUB_EASYBLOCKS_REPO :
1373+ if any (file_info ['new' ]):
1374+ labels .append ('new' )
1375+ return labels
1376+
1377+
1378+ def post_pr_labels (pr , labels ):
1379+ """
1380+ Update PR labels
1381+ """
1382+ pr_target_account = build_option ('pr_target_account' )
1383+ pr_target_repo = build_option ('pr_target_repo' ) or GITHUB_EASYCONFIGS_REPO
1384+
1385+ # fetch GitHub token if available
1386+ github_user = build_option ('github_user' )
1387+ if github_user is None :
1388+ _log .info ("GitHub user not specified, not adding labels to PR# %s" % pr )
1389+ return False
1390+
1391+ github_token = fetch_github_token (github_user )
1392+ if github_token is None :
1393+ _log .info ("GitHub token for user '%s' not found, not adding labels to PR# %s" % (github_user , pr ))
1394+ return False
1395+
1396+ dry_run = build_option ('dry_run' ) or build_option ('extended_dry_run' )
1397+
1398+ if not dry_run :
1399+ g = RestClient (GITHUB_API_URL , username = github_user , token = github_token )
1400+
1401+ pr_url = g .repos [pr_target_account ][pr_target_repo ].issues [pr ]
1402+ try :
1403+ status , data = pr_url .labels .post (body = labels )
1404+ if status == HTTP_STATUS_OK :
1405+ print_msg ("Added labels %s to PR#%s" % (', ' .join (labels ), pr ), log = _log , prefix = False )
1406+ return True
1407+ except HTTPError as err :
1408+ _log .info ("Failed to add labels to PR# %s: %s." % (pr , err ))
1409+ return False
1410+ else :
1411+ return True
1412+
1413+
1414+ def add_pr_labels (pr , branch = 'develop' ):
1415+ """
1416+ Try to determine and add labels to PR.
1417+ :param pr: pull request number in easybuild-easyconfigs repo
1418+ :param branch: easybuild-easyconfigs branch to compare with
1419+ """
1420+ pr_target_repo = build_option ('pr_target_repo' ) or GITHUB_EASYCONFIGS_REPO
1421+ if pr_target_repo != GITHUB_EASYCONFIGS_REPO :
1422+ raise EasyBuildError ("Adding labels to PRs for repositories other than easyconfigs hasn't been implemented yet" )
1423+
1424+ tmpdir = tempfile .mkdtemp ()
1425+
1426+ download_repo_path = download_repo (branch = branch , path = tmpdir )
1427+
1428+ pr_files = [path for path in fetch_easyconfigs_from_pr (pr ) if path .endswith ('.eb' )]
1429+
1430+ file_info = det_file_info (pr_files , download_repo_path )
1431+
1432+ pr_target_account = build_option ('pr_target_account' )
1433+ github_user = build_option ('github_user' )
1434+ pr_data , _ = fetch_pr_data (pr , pr_target_account , pr_target_repo , github_user )
1435+ pr_labels = [label ['name' ] for label in pr_data ['labels' ]]
1436+
1437+ expected_labels = det_pr_labels (file_info , pr_target_repo )
1438+ missing_labels = [label for label in expected_labels if label not in pr_labels ]
1439+
1440+ dry_run = build_option ('dry_run' ) or build_option ('extended_dry_run' )
1441+
1442+ if missing_labels :
1443+ missing_labels_txt = ', ' .join (["'%s'" % ml for ml in missing_labels ])
1444+ print_msg ("PR #%s should be labelled %s" % (pr , missing_labels_txt ), log = _log , prefix = False )
1445+ if not dry_run and not post_pr_labels (pr , missing_labels ):
1446+ print_msg ("Could not add labels %s to PR #%s" % (missing_labels_txt , pr ), log = _log , prefix = False )
1447+ else :
1448+ print_msg ("Could not determine any missing labels for PR #%s" % pr , log = _log , prefix = False )
1449+
1450+
13621451@only_if_module_is_available ('git' , pkgname = 'GitPython' )
13631452def new_branch_github (paths , ecs , commit_msg = None ):
13641453 """
@@ -1486,14 +1575,9 @@ def new_pr_from_branch(branch_name, title=None, descr=None, pr_target_repo=None,
14861575
14871576 file_info = det_file_info (ec_paths , target_dir )
14881577
1489- labels = []
1490- if pr_target_repo == GITHUB_EASYCONFIGS_REPO :
1491- # label easyconfigs for new software and/or new easyconfigs for existing software
1492- if any (file_info ['new_folder' ]):
1493- labels .append ('new' )
1494- if any (file_info ['new_file_in_existing_folder' ]):
1495- labels .append ('update' )
1578+ labels = det_pr_labels (file_info , pr_target_repo )
14961579
1580+ if pr_target_repo == GITHUB_EASYCONFIGS_REPO :
14971581 # only use most common toolchain(s) in toolchain label of PR title
14981582 toolchains = ['%(name)s/%(version)s' % ec ['toolchain' ] for ec in file_info ['ecs' ]]
14991583 toolchains_counted = sorted ([(toolchains .count (tc ), tc ) for tc in nub (toolchains )])
@@ -1503,9 +1587,6 @@ def new_pr_from_branch(branch_name, title=None, descr=None, pr_target_repo=None,
15031587 classes = [ec ['moduleclass' ] for ec in file_info ['ecs' ]]
15041588 classes_counted = sorted ([(classes .count (c ), c ) for c in nub (classes )])
15051589 class_label = ',' .join ([tc for (cnt , tc ) in classes_counted if cnt == classes_counted [- 1 ][0 ]])
1506- elif pr_target_repo == GITHUB_EASYBLOCKS_REPO :
1507- if any (file_info ['new' ]):
1508- labels .append ('new' )
15091590
15101591 if title is None :
15111592 if pr_target_repo == GITHUB_EASYCONFIGS_REPO :
@@ -1581,15 +1662,9 @@ def new_pr_from_branch(branch_name, title=None, descr=None, pr_target_repo=None,
15811662 print_msg ("Opened pull request: %s" % data ['html_url' ], log = _log , prefix = False )
15821663
15831664 if labels :
1584- # post labels
15851665 pr = data ['html_url' ].split ('/' )[- 1 ]
1586- pr_url = g .repos [pr_target_account ][pr_target_repo ].issues [pr ]
1587- try :
1588- status , data = pr_url .labels .post (body = labels )
1589- if status == HTTP_STATUS_OK :
1590- print_msg ("Added labels %s to PR#%s" % (', ' .join (labels ), pr ), log = _log , prefix = False )
1591- except HTTPError as err :
1592- _log .info ("Failed to add labels to PR# %s: %s." % (pr , err ))
1666+ if not post_pr_labels (pr , labels ):
1667+ print_msg ("This PR should be labelled %s" % ', ' .join (labels ), log = _log , prefix = False )
15931668
15941669
15951670def new_pr (paths , ecs , title = None , descr = None , commit_msg = None ):
0 commit comments