Skip to content

Commit fca5c9d

Browse files
legalsylvainsbidoul
andcommitted
[FIX] handle max size label limitation (50 char)
if label name is too long, it is cut, and ended by an hash of the module name. the module name is present in the description of the label - factor out function that computes the shortened label - use 'mod:' instead of 'addon:' - add MODULE_LABEL_COLOR as new configuration Co-authored-by: Stéphane Bidoul <[email protected]>
1 parent 256027c commit fca5c9d

5 files changed

Lines changed: 66 additions & 3 deletions

File tree

environment.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ ODOO_PASSWORD=
3434
# Number of days before the proposal can be marked as "Approved"
3535
#MIN_PR_AGE=5
3636

37+
# Color of the github label that contains the name of the module
38+
#MODULE_LABEL_COLOR=#ffc
39+
3740
# Coma separated list of task to run
3841
# By default all configured tasks are run.
3942
# Available tasks:

src/oca_github_bot/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def func_wrapper(*args, **kwargs):
101101
APPROVALS_REQUIRED = int(os.environ.get("APPROVALS_REQUIRED", "2"))
102102
MIN_PR_AGE = int(os.environ.get("MIN_PR_AGE", "5"))
103103

104+
MODULE_LABEL_COLOR = os.environ.get("MODULE_LABEL_COLOR", "#ffc")
105+
104106
dist_publisher = MultiDistPublisher()
105107
SIMPLE_INDEX_ROOT = os.environ.get("SIMPLE_INDEX_ROOT")
106108
if SIMPLE_INDEX_ROOT:

src/oca_github_bot/tasks/label_modified_addons.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
# Distributed under the MIT License (http://opensource.org/licenses/MIT).
33

44
from .. import github
5-
from ..config import switchable
5+
from ..config import MODULE_LABEL_COLOR, switchable
66
from ..manifest import git_modified_addons
77
from ..process import check_call
88
from ..queue import task
9+
from ..utils import compute_module_label_name
910
from ..version_branch import is_main_branch_bot_branch
1011

1112

1213
def _label_modified_addons(gh, org, repo, pr, dry_run):
14+
gh_repo = gh.repository(org, repo)
1315
gh_pr = gh.pull_request(org, repo, pr)
1416
target_branch = gh_pr.base.ref
1517
pr_branch = f"tmp-pr-{pr}"
@@ -23,7 +25,22 @@ def _label_modified_addons(gh, org, repo, pr, dry_run):
2325
if not modified_addons:
2426
return
2527
gh_issue = github.gh_call(gh_pr.issue)
26-
new_labels = {f"addon:{modified_addon}" for modified_addon in modified_addons}
28+
29+
new_labels = set()
30+
for modified_addon in modified_addons:
31+
label_name = compute_module_label_name(modified_addon)
32+
# We create label at repo level, because it is possible to
33+
# to set description in create_label() function
34+
# (and not in issue.add_labels())
35+
if label_name not in [x.name for x in gh_repo.labels()] and not dry_run:
36+
github.gh_call(
37+
gh_repo.create_label,
38+
name=label_name,
39+
description=f"Module {modified_addon}",
40+
color=MODULE_LABEL_COLOR.replace("#", ""),
41+
)
42+
new_labels.add(label_name)
43+
2744
if is_main_branch_bot_branch(target_branch):
2845
new_labels.add(f"series:{target_branch}")
2946
new_labels = new_labels - {label.name for label in gh_issue.labels()}

src/oca_github_bot/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# Copyright (c) ACSONE SA/NV 2021
22
# Distributed under the MIT License (http://opensource.org/licenses/MIT).
33

4+
import hashlib
45
import re
56
import shlex
67
import time
78
from typing import Sequence
89

910
from . import config
1011

12+
# Max size allowed by github for label name
13+
_MAX_LABEL_SIZE = 50
14+
# Size of the hash, added at the end of the label name
15+
# if module name is too long
16+
_HASH_SIZE = 5
17+
1118

1219
def hide_secrets(s: str) -> str:
1320
# TODO do we want to hide other secrets ?
@@ -33,3 +40,24 @@ def retry_on_exception(
3340

3441
def cmd_to_str(cmd: Sequence[str]) -> str:
3542
return shlex.join(str(c) for c in cmd)
43+
44+
45+
def compute_module_label_name(module_name: str) -> str:
46+
"""To avoid error if label name is too long
47+
we cut big label, and finish by a hash of the module name.
48+
(The full module name will be present in the description).
49+
Short module name exemple :
50+
- module : 'web_responsive'
51+
- label : 'mod:web_responsive'
52+
Long module name exemple :
53+
- module : 'account_invoice_supplierinfo_update_triple_discount'
54+
- label : 'mod:account_invoice_supplierinfo_update_trip bf3f3'
55+
"""
56+
label_name = f"mod:{module_name}"
57+
if len(label_name) > _MAX_LABEL_SIZE:
58+
module_hash = hashlib.sha256(bytes(module_name, "utf-8")).hexdigest()
59+
label_name = (
60+
f"{label_name[:(_MAX_LABEL_SIZE - (_HASH_SIZE + 1))]}"
61+
f" {module_hash[:_HASH_SIZE]}"
62+
)
63+
return label_name

tests/test_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
import pytest
77

8-
from oca_github_bot.utils import cmd_to_str, hide_secrets, retry_on_exception
8+
from oca_github_bot.utils import (
9+
cmd_to_str,
10+
compute_module_label_name,
11+
hide_secrets,
12+
retry_on_exception,
13+
)
914

1015
from .common import set_config
1116

@@ -98,3 +103,11 @@ def func_that_raises():
98103
)
99104
def test_cmd_to_str(cmd, expected):
100105
assert cmd_to_str(cmd) == expected
106+
107+
108+
def test_compute_module_label_name():
109+
assert compute_module_label_name("web_responsive") == "mod:web_responsive"
110+
assert (
111+
compute_module_label_name("account_invoice_supplierinfo_update_triple_discount")
112+
== "mod:account_invoice_supplierinfo_update_trip bf3f3"
113+
)

0 commit comments

Comments
 (0)