11# Copyright (c) ACSONE SA/NV 2024
22# Distributed under the MIT License (http://opensource.org/licenses/MIT).
3+ import hashlib
34
45from .. import github
56from ..config import switchable
89from ..queue import task
910from ..version_branch import is_main_branch_bot_branch
1011
12+ LABEL_COLOR = "ffc"
13+ # Max size allowed by github for label name
14+ MAX_LABEL_SIZE = 50
15+
1116
1217def _label_modified_addons (gh , org , repo , pr , dry_run ):
18+ gh_repo = gh .repository (org , repo )
1319 gh_pr = gh .pull_request (org , repo , pr )
1420 target_branch = gh_pr .base .ref
1521 pr_branch = f"tmp-pr-{ pr } "
@@ -23,7 +29,30 @@ def _label_modified_addons(gh, org, repo, pr, dry_run):
2329 if not modified_addons :
2430 return
2531 gh_issue = github .gh_call (gh_pr .issue )
26- new_labels = {f"addon:{ modified_addon } " for modified_addon in modified_addons }
32+
33+ new_labels = set ()
34+ for modified_addon in modified_addons :
35+ label_name = f"addons:{ modified_addon } "
36+ # To avoid error if label name is too long
37+ # we cut big label, and finish by a hash of the module name
38+ # the full module name will be present in the description
39+ if len (label_name ) > MAX_LABEL_SIZE :
40+ little_hash = hashlib .sha256 (
41+ bytes (modified_addon , "utf-8" )
42+ ).hexdigest ()[:5 ]
43+ label_name = f"{ label_name [:44 ]} { little_hash } "
44+ # We create label at repo level, because it is possible to
45+ # to set description in create_label() function
46+ # (and not in issue.add_labels())
47+ if label_name not in [x .name for x in gh_repo .labels ()] and not dry_run :
48+ github .gh_call (
49+ gh_repo .create_label ,
50+ name = label_name ,
51+ description = f"Module { modified_addon } " ,
52+ color = LABEL_COLOR ,
53+ )
54+ new_labels .add (label_name )
55+
2756 if is_main_branch_bot_branch (target_branch ):
2857 new_labels .add (f"series:{ target_branch } " )
2958 new_labels = new_labels - {label .name for label in gh_issue .labels ()}
0 commit comments