Skip to content

Commit

Permalink
Add mention_maintainer + tests with mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoRubi committed Oct 22, 2019
1 parent 26a0991 commit cbd55ba
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 1 deletion.
3 changes: 2 additions & 1 deletion environment.sample
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ ODOO_PASSWORD=
# By default all configured tasks are run.
# 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
# gen_addons_readme,gen_addons_icon,setuptools_odoo,mention_maintainer,
# merge_bot,tag_needs_review
#BOT_TASKS=all

# Root of the PEP 503 simple index where wheels are published
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 @@ -4,6 +4,7 @@
from . import (
heartbeat,
main_branch_bot,
mention_maintainer,
tag_approved,
tag_needs_review,
tag_ready_to_merge,
Expand Down
62 changes: 62 additions & 0 deletions src/oca_github_bot/tasks/mention_maintainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2019 Simone Rubino - Agile Business Group
# Distributed under the MIT License (http://opensource.org/licenses/MIT).

from celery.task import task

from .. import github
from ..config import switchable
from ..manifest import get_manifest, git_modified_addon_dirs, is_addon_dir
from ..process import check_call
from ..queue import getLogger

_logger = getLogger(__name__)


@task()
@switchable("mention_maintainer")
def mention_maintainer(org, repo, pr, dry_run=False):
with github.login() as gh:
gh_pr = gh.pull_request(org, repo, pr)
target_branch = gh_pr.base.ref
with github.temporary_clone(org, repo, target_branch) as clonedir:
# Get modified addons list on the PR.
pr_branch = f"tmp-pr-{pr}"
check_call(
["git", "fetch", "origin", f"refs/pull/{pr}/head:{pr_branch}"],
cwd=clonedir,
)
check_call(["git", "checkout", pr_branch], cwd=clonedir)
modified_addon_dirs, _ = git_modified_addon_dirs(clonedir, target_branch)
# Remove not installable addons
# (case where an addon becomes no more installable).
modified_installable_addon_dirs = [
d for d in modified_addon_dirs if is_addon_dir(d, installable_only=True)
]

all_mentions_comment = get_maintainers_mentions(
modified_installable_addon_dirs
)

if not all_mentions_comment:
return False
if dry_run:
_logger.info(f"DRY-RUN Comment {all_mentions_comment}")
else:
_logger.info(f"Comment {all_mentions_comment}")
return github.gh_call(gh_pr.create_comment, all_mentions_comment)


def get_maintainers_mentions(addon_dirs):
all_maintainers = list()
for addon_dir in addon_dirs:
maintainers = get_manifest(addon_dir).get("maintainers", [])
all_maintainers.extend(maintainers)
if not all_maintainers:
return ""
all_mentions = map(lambda m: "@" + m, all_maintainers)
all_mentions_comment = (
"Hi " + ", ".join(all_mentions) + ",\n"
"some modules you are maintaining are being modified, "
"check this out!"
)
return all_mentions_comment
1 change: 1 addition & 0 deletions src/oca_github_bot/webhooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
on_pr_close_delete_branch,
on_pr_green_label_needs_review,
on_pr_open_label_new_contributor,
on_pr_open_mention_maintainer,
on_pr_review,
on_push_to_main_branch,
on_status_merge_bot,
Expand Down
19 changes: 19 additions & 0 deletions src/oca_github_bot/webhooks/on_pr_open_mention_maintainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2019 Simone Rubino - Agile Business Group
# Distributed under the MIT License (http://opensource.org/licenses/MIT).

import logging

from ..router import router
from ..tasks.mention_maintainer import mention_maintainer

_logger = logging.getLogger(__name__)


@router.register("pull_request", action="opened")
async def on_pr_open_mention_maintainer(event, *args, **kwargs):
"""
Whenever a PR is opened, mention the maintainers of modified addons.
"""
org, repo = event.data["repository"]["full_name"].split("/")
pr = event.data["pull_request"]["number"]
mention_maintainer.delay(org, repo, pr)
1 change: 1 addition & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def make_addon(git_clone, addon_name, **manifest_keys):
manifest = dict(name=addon_name, **manifest_keys)
(addon_dir / "__manifest__.py").write_text(repr(manifest))
(addon_dir / "__init__.py").write_text("")
return addon_dir


def commit_addon(git_clone, addon_name):
Expand Down
45 changes: 45 additions & 0 deletions tests/test_mention_maintainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2019 Simone Rubino - Agile Business Group
# Distributed under the MIT License (http://opensource.org/licenses/MIT).

import pytest

from oca_github_bot.tasks.mention_maintainer import mention_maintainer

from .common import make_addon


@pytest.mark.vcr()
def test_maintainer_mentioned(git_clone, mocker):
github_mock = mocker.patch("oca_github_bot.tasks.mention_maintainer.github")
github_mock.temporary_clone.return_value.__enter__.return_value = str(git_clone)

addon_name = "addon1"
addon_dir = make_addon(git_clone, addon_name, maintainers=["themaintainer"])

modified_addons_mock = mocker.patch(
"oca_github_bot.tasks.mention_maintainer.git_modified_addon_dirs"
)
modified_addons_mock.return_value = [addon_dir], False
mocker.patch("oca_github_bot.tasks.mention_maintainer.check_call")
mention_maintainer("org", "repo", "pr")

github_mock.gh_call.assert_called_once()
assert "@themaintainer" in github_mock.gh_call.mock_calls[0][1][1]


@pytest.mark.vcr()
def test_no_maintainer_no_mention(git_clone, mocker):
github_mock = mocker.patch("oca_github_bot.tasks.mention_maintainer.github")
github_mock.temporary_clone.return_value.__enter__.return_value = str(git_clone)

addon_name = "addon1"
addon_dir = make_addon(git_clone, addon_name)

modified_addons_mock = mocker.patch(
"oca_github_bot.tasks.mention_maintainer.git_modified_addon_dirs"
)
modified_addons_mock.return_value = [addon_dir], False
mocker.patch("oca_github_bot.tasks.mention_maintainer.check_call")
mention_maintainer("org", "repo", "pr")

github_mock.gh_call.assert_not_called()

0 comments on commit cbd55ba

Please sign in to comment.