-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mention_maintainer + tests with mocks
- Loading branch information
Showing
7 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/oca_github_bot/webhooks/on_pr_open_mention_maintainer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |