Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label PRs with modified addons #300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 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,39 @@
# 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


def _label_modified_addons(gh, org, repo, pr, dry_run):
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)


@task()
@switchable("label_modified_addons")
def label_modified_addons(org, repo, pr, dry_run):
with github.login() as gh:
_label_modified_addons(gh, org, repo, pr, dry_run)
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 ACSONE SA/NV 2024
# 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(gh):
_label_modified_addons(gh, "OCA", "mis-builder", "610", dry_run=False)
Loading