Skip to content

Commit 93cc406

Browse files
committed
Label PRs with modified addons
1 parent 2376d30 commit 93cc406

File tree

7 files changed

+518
-2
lines changed

7 files changed

+518
-2
lines changed

src/oca_github_bot/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def func_wrapper(*args, **kwargs):
5959
# Available tasks:
6060
# delete_branch,tag_approved,tag_ready_to_merge,gen_addons_table,
6161
# gen_addons_readme,gen_addons_icon,setuptools_odoo,merge_bot,tag_needs_review,
62-
# migration_issue_bot,whool_init,gen_metapackage
62+
# migration_issue_bot,whool_init,gen_metapackage,label_modified_addons
6363
BOT_TASKS = os.environ.get("BOT_TASKS", "all").split(",")
6464

6565
BOT_TASKS_DISABLED = os.environ.get("BOT_TASKS_DISABLED", "").split(",")

src/oca_github_bot/tasks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from . import (
55
heartbeat,
6+
label_modified_addons,
67
main_branch_bot,
78
mention_maintainer,
89
migration_issue_bot,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 switchable
6+
from ..manifest import git_modified_addons
7+
from ..process import check_call
8+
from ..queue import task
9+
from ..version_branch import is_main_branch_bot_branch
10+
11+
12+
def _label_modified_addons(gh, org, repo, pr, dry_run):
13+
gh_pr = gh.pull_request(org, repo, pr)
14+
target_branch = gh_pr.base.ref
15+
pr_branch = f"tmp-pr-{pr}"
16+
with github.temporary_clone(org, repo, target_branch) as clone_dir:
17+
check_call(
18+
["git", "fetch", "origin", f"pull/{pr}/head:{pr_branch}"],
19+
cwd=clone_dir,
20+
)
21+
check_call(["git", "checkout", pr_branch], cwd=clone_dir)
22+
modified_addons, _ = git_modified_addons(clone_dir, target_branch)
23+
if not modified_addons:
24+
return
25+
gh_issue = github.gh_call(gh_pr.issue)
26+
new_labels = {f"addon:{modified_addon}" for modified_addon in modified_addons}
27+
if is_main_branch_bot_branch(target_branch):
28+
new_labels.add(f"series:{target_branch}")
29+
new_labels = new_labels - {label.name for label in gh_issue.labels()}
30+
if new_labels and not dry_run:
31+
for new_label in new_labels:
32+
github.gh_call(gh_issue.add_labels, new_label)
33+
34+
35+
@task()
36+
@switchable("label_modified_addons")
37+
def label_modified_addons(org, repo, pr, dry_run):
38+
with github.login() as gh:
39+
_label_modified_addons(gh, org, repo, pr, dry_run)

src/oca_github_bot/version_branch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def is_main_branch_bot_branch(branch_name):
3333

3434

3535
def is_protected_branch(branch_name):
36-
if branch_name == "master":
36+
if branch_name in ("master", "main"):
3737
return True
3838
return bool(ODOO_VERSION_RE.match(branch_name))
3939

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) ACSONE SA/NV 2024
2+
# Distributed under the MIT License (http://opensource.org/licenses/MIT).
3+
4+
import logging
5+
6+
from ..router import router
7+
from ..tasks.label_modified_addons import label_modified_addons
8+
9+
_logger = logging.getLogger(__name__)
10+
11+
12+
@router.register("pull_request", action="opened")
13+
@router.register("pull_request", action="reopened")
14+
@router.register("pull_request", action="synchronize")
15+
async def on_pr_label_modified_addons(event, *args, **kwargs):
16+
"""
17+
Whenever a PR is opened, add labels based on modified addons.
18+
"""
19+
org, repo = event.data["repository"]["full_name"].split("/")
20+
pr = event.data["pull_request"]["number"]
21+
label_modified_addons.delay(org, repo, pr)

tests/cassettes/test_label_modified_addons.yaml

Lines changed: 445 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright ACSONE SA/NV 2024
2+
# Distributed under the MIT License (http://opensource.org/licenses/MIT).
3+
import pytest
4+
5+
from oca_github_bot.tasks.label_modified_addons import _label_modified_addons
6+
7+
8+
@pytest.mark.vcr()
9+
def test_label_modified_addons(gh):
10+
_label_modified_addons(gh, "OCA", "mis-builder", "610", dry_run=False)

0 commit comments

Comments
 (0)