Skip to content

Commit

Permalink
Label PRs with modified addons
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Jul 22, 2024
1 parent 2376d30 commit 5c7b4e5
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/oca_github_bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def func_wrapper(*args, **kwargs):
# Available tasks:
# delete_branch,tag_approved,tag_ready_to_merge,gen_addons_table,
# gen_addons_readme,gen_addons_icon,setuptools_odoo,merge_bot,tag_needs_review,
# migration_issue_bot,whool_init,gen_metapackage
# migration_issue_bot,whool_init,gen_metapackage,label_modified_addons
BOT_TASKS = os.environ.get("BOT_TASKS", "all").split(",")

BOT_TASKS_DISABLED = os.environ.get("BOT_TASKS_DISABLED", "").split(",")
Expand Down
1 change: 1 addition & 0 deletions src/oca_github_bot/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from . import (
heartbeat,
label_modified_addons,
main_branch_bot,
mention_maintainer,
migration_issue_bot,
Expand Down
37 changes: 37 additions & 0 deletions src/oca_github_bot/tasks/label_modified_addons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) ACSONE SA/NV 2024
# Distributed under the MIT License (http://opensource.org/licenses/MIT).

from .. import github
from ..config import switchable
from ..manifest import git_modified_addons
from ..process import check_call
from ..queue import task
from ..version_branch import is_main_branch_bot_branch


@task()
@switchable("label_modified_addons")
def label_modified_addons(org, repo, pr, dry_run):
with github.login() as gh:
gh_pr = gh.pull_request(org, repo, pr)
target_branch = gh_pr.base.ref
pr_branch = f"tmp-pr-{pr}"
with github.temporary_clone(org, repo, target_branch) as clone_dir:
check_call(
["git", "fetch", "origin", f"pull/{pr}/head:{pr_branch}"],
cwd=clone_dir,
)
check_call(["git", "checkout", pr_branch], cwd=clone_dir)
modified_addons, _ = git_modified_addons(clone_dir, target_branch)
if not modified_addons:
return
gh_issue = github.gh_call(gh_pr.issue)
new_labels = {
f"addon:{modified_addon}" for modified_addon in modified_addons
}
if is_main_branch_bot_branch(target_branch):
new_labels.add(f"series:{target_branch}")
new_labels = new_labels - {label.name for label in gh_issue.labels()}
if new_labels and not dry_run:
for new_label in new_labels:
github.gh_call(gh_issue.add_labels, new_label)
2 changes: 1 addition & 1 deletion src/oca_github_bot/version_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def is_main_branch_bot_branch(branch_name):


def is_protected_branch(branch_name):
if branch_name == "master":
if branch_name in ("master", "main"):
return True
return bool(ODOO_VERSION_RE.match(branch_name))

Expand Down
21 changes: 21 additions & 0 deletions src/oca_github_bot/webhooks/on_pr_label_modified_addons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) ACSONE SA/NV 2024
# Distributed under the MIT License (http://opensource.org/licenses/MIT).

import logging

from ..router import router
from ..tasks.label_modified_addons import label_modified_addons

_logger = logging.getLogger(__name__)


@router.register("pull_request", action="opened")
@router.register("pull_request", action="reopened")
@router.register("pull_request", action="synchronize")
async def on_pr_label_modified_addons(event, *args, **kwargs):
"""
Whenever a PR is opened, add labels based on modified addons.
"""
org, repo = event.data["repository"]["full_name"].split("/")
pr = event.data["pull_request"]["number"]
label_modified_addons.delay(org, repo, pr)
445 changes: 445 additions & 0 deletions tests/cassettes/test_label_modified_addons.yaml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions tests/test_label_modified_addons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2019 Simone Rubino - Agile Business Group
# Distributed under the MIT License (http://opensource.org/licenses/MIT).
import pytest

from oca_github_bot.tasks.label_modified_addons import label_modified_addons


@pytest.mark.vcr()
def test_label_modified_addons(mocker):
label_modified_addons("OCA", "mis-builder", "610", dry_run=False)

0 comments on commit 5c7b4e5

Please sign in to comment.