Skip to content

Commit bc85ee4

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

File tree

6 files changed

+515
-1
lines changed

6 files changed

+515
-1
lines changed

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

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)

0 commit comments

Comments
 (0)