|
| 1 | +# Copyright (c) ACSONE SA/NV 2024 |
| 2 | +# Distributed under the MIT License (http://opensource.org/licenses/MIT). |
| 3 | + |
| 4 | +from .. import github |
| 5 | +from ..config import MODULE_LABEL_COLOR, switchable |
| 6 | +from ..manifest import git_modified_addons |
| 7 | +from ..process import check_call |
| 8 | +from ..queue import task |
| 9 | +from ..utils import compute_module_label_name |
| 10 | +from ..version_branch import is_main_branch_bot_branch |
| 11 | + |
| 12 | + |
| 13 | +def _label_modified_addons(gh, org, repo, pr, dry_run): |
| 14 | + gh_repo = gh.repository(org, repo) |
| 15 | + gh_pr = gh.pull_request(org, repo, pr) |
| 16 | + target_branch = gh_pr.base.ref |
| 17 | + pr_branch = f"tmp-pr-{pr}" |
| 18 | + with github.temporary_clone(org, repo, target_branch) as clone_dir: |
| 19 | + check_call( |
| 20 | + ["git", "fetch", "origin", f"pull/{pr}/head:{pr_branch}"], |
| 21 | + cwd=clone_dir, |
| 22 | + ) |
| 23 | + check_call(["git", "checkout", pr_branch], cwd=clone_dir) |
| 24 | + modified_addons, _ = git_modified_addons(clone_dir, target_branch) |
| 25 | + if not modified_addons: |
| 26 | + return |
| 27 | + gh_issue = github.gh_call(gh_pr.issue) |
| 28 | + repo_label_names = [label.name for label in gh_repo.labels()] |
| 29 | + issue_label_names = [label.name for label in gh_issue.labels()] |
| 30 | + |
| 31 | + new_labels = set() |
| 32 | + for modified_addon in modified_addons: |
| 33 | + label_name = compute_module_label_name(modified_addon) |
| 34 | + # We create label at repo level, because it is possible to |
| 35 | + # to set description in create_label() function |
| 36 | + # (and not in issue.add_labels()) |
| 37 | + if label_name not in repo_label_names and not dry_run: |
| 38 | + github.gh_call( |
| 39 | + gh_repo.create_label, |
| 40 | + name=label_name, |
| 41 | + description=f"Module {modified_addon}", |
| 42 | + color=MODULE_LABEL_COLOR.replace("#", ""), |
| 43 | + ) |
| 44 | + new_labels.add(label_name) |
| 45 | + |
| 46 | + if is_main_branch_bot_branch(target_branch): |
| 47 | + new_labels.add(f"series:{target_branch}") |
| 48 | + new_labels |= { |
| 49 | + x |
| 50 | + for x in issue_label_names |
| 51 | + if not (x.startswith("mod:") or x.startswith("series:")) |
| 52 | + } |
| 53 | + |
| 54 | + if not dry_run and new_labels != set(issue_label_names): |
| 55 | + github.gh_call(gh_issue.replace_labels, list(new_labels)) |
| 56 | + |
| 57 | + |
| 58 | +@task() |
| 59 | +@switchable("label_modified_addons") |
| 60 | +def label_modified_addons(org, repo, pr, dry_run=False): |
| 61 | + with github.login() as gh: |
| 62 | + _label_modified_addons(gh, org, repo, pr, dry_run) |
0 commit comments