From 5c9c1f15186a001482b56915162ea201a9da9d70 Mon Sep 17 00:00:00 2001 From: DavHau Date: Wed, 26 Jul 2023 14:33:36 +0200 Subject: [PATCH] clan-merge: require bot assigned --- modules/web01/clan-merge.nix | 3 ++- pkgs/clan-merge/clan_merge/__init__.py | 17 ++++++++++---- pkgs/clan-merge/tests/test_cli.py | 31 +++++++++++++++++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/modules/web01/clan-merge.nix b/modules/web01/clan-merge.nix index f0af57d..a5b3199 100644 --- a/modules/web01/clan-merge.nix +++ b/modules/web01/clan-merge.nix @@ -17,7 +17,8 @@ while sleep 10; do ${self.packages.${pkgs.system}.clan-merge}/bin/clan-merge \ --allowed-users DavHau lassulus mic92 \ - --repos clan-core + --repos clan-core \ + --bot-name clan-bot done ''; }; diff --git a/pkgs/clan-merge/clan_merge/__init__.py b/pkgs/clan-merge/clan_merge/__init__.py index 11f8cb1..65984b2 100644 --- a/pkgs/clan-merge/clan_merge/__init__.py +++ b/pkgs/clan-merge/clan_merge/__init__.py @@ -30,12 +30,14 @@ def is_ci_green(pr: dict) -> bool: return True -def decide_merge(pr: dict, allowed_users: list[str]) -> bool: +def decide_merge(pr: dict, allowed_users: list[str], bot_name: str) -> bool: if ( pr["user"]["login"] in allowed_users and pr["mergeable"] is True and not pr["title"].startswith("WIP:") and pr["state"] == "open" + # check if bot is assigned + and any(reviewer["login"] == bot_name for reviewer in pr["assignees"]) and is_ci_green(pr) ): return True @@ -50,10 +52,10 @@ def list_prs(repo: str) -> list: return data -def list_prs_to_merge(prs: list, allowed_users: list[str]) -> list: +def list_prs_to_merge(prs: list, allowed_users: list[str], bot_name: str) -> list: prs_to_merge = [] for pr in prs: - if decide_merge(pr, allowed_users) is True: + if decide_merge(pr, allowed_users, bot_name) is True: prs_to_merge.append(pr) return prs_to_merge @@ -67,6 +69,12 @@ def parse_args() -> argparse.Namespace: help="list of users allowed to merge", required=True, ) + # option for bot-name + parser.add_argument( + "--bot-name", + help="name of the bot", + required=True, + ) # parse list of repository names for which to merge PRs parser.add_argument( "--repos", @@ -93,9 +101,10 @@ def clan_merge( allowed_users = args.allowed_users repos = args.repos dry_run = args.dry_run + bot_name = args.bot_name for repo in repos: prs = list_prs(repo) - prs_to_merge = list_prs_to_merge(prs, allowed_users) + prs_to_merge = list_prs_to_merge(prs, allowed_users, bot_name) for pr in prs_to_merge: url = ( "https://git.clan.lol/api/v1/repos/clan/" diff --git a/pkgs/clan-merge/tests/test_cli.py b/pkgs/clan-merge/tests/test_cli.py index 3621468..a2b0ae9 100644 --- a/pkgs/clan-merge/tests/test_cli.py +++ b/pkgs/clan-merge/tests/test_cli.py @@ -14,14 +14,16 @@ def test_no_args(capsys: pytest.CaptureFixture) -> None: def test_decide_merge_allowed(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(clan_merge, "is_ci_green", lambda x: True) allowed_users = ["foo"] + bot_name = "some-bot-name" pr = dict( id=1, user=dict(login="foo"), title="Some PR Title", mergeable=True, state="open", + assignees=[dict(login=bot_name)], ) - assert clan_merge.decide_merge(pr, allowed_users) is True + assert clan_merge.decide_merge(pr, allowed_users, bot_name=bot_name) is True def test_decide_merge_not_allowed(monkeypatch: pytest.MonkeyPatch) -> None: @@ -33,6 +35,7 @@ def test_decide_merge_not_allowed(monkeypatch: pytest.MonkeyPatch) -> None: title="Some PR Title", mergeable=True, state="open", + assignees=[dict(login="foo")], ) pr2 = dict( id=1, @@ -40,6 +43,7 @@ def test_decide_merge_not_allowed(monkeypatch: pytest.MonkeyPatch) -> None: title="WIP: xyz", mergeable=True, state="open", + assignees=[dict(login="foo")], ) pr3 = dict( id=1, @@ -47,6 +51,7 @@ def test_decide_merge_not_allowed(monkeypatch: pytest.MonkeyPatch) -> None: title="Some PR Title", mergeable=False, state="open", + assignees=[dict(login="foo")], ) pr4 = dict( id=1, @@ -54,15 +59,26 @@ def test_decide_merge_not_allowed(monkeypatch: pytest.MonkeyPatch) -> None: title="Some PR Title", mergeable=True, state="closed", + assignees=[dict(login="foo")], ) - assert clan_merge.decide_merge(pr1, allowed_users) is False - assert clan_merge.decide_merge(pr2, allowed_users) is False - assert clan_merge.decide_merge(pr3, allowed_users) is False - assert clan_merge.decide_merge(pr4, allowed_users) is False + pr5 = dict( + id=1, + user=dict(login="foo"), + title="Some PR Title", + mergeable=True, + state="open", + assignees=[dict(login="clan-bot")], + ) + assert clan_merge.decide_merge(pr1, allowed_users, bot_name="some-bot") is False + assert clan_merge.decide_merge(pr2, allowed_users, bot_name="some-bot") is False + assert clan_merge.decide_merge(pr3, allowed_users, bot_name="some-bot") is False + assert clan_merge.decide_merge(pr4, allowed_users, bot_name="some-bot") is False + assert clan_merge.decide_merge(pr5, allowed_users, bot_name="some-bot") is False def test_list_prs_to_merge(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(clan_merge, "is_ci_green", lambda x: True) + bot_name = "some-bot-name" prs = [ dict( id=1, @@ -72,6 +88,7 @@ def test_list_prs_to_merge(monkeypatch: pytest.MonkeyPatch) -> None: state="open", title="PR 1", mergeable=True, + assignees=[dict(login=bot_name)], ), dict( id=2, @@ -81,6 +98,7 @@ def test_list_prs_to_merge(monkeypatch: pytest.MonkeyPatch) -> None: state="open", title="WIP: xyz", mergeable=True, + assignees=[dict(login=bot_name)], ), dict( id=3, @@ -90,6 +108,7 @@ def test_list_prs_to_merge(monkeypatch: pytest.MonkeyPatch) -> None: state="open", title="PR 2", mergeable=True, + assignees=[dict(login=bot_name)], ), ] - assert clan_merge.list_prs_to_merge(prs, ["foo"]) == [prs[0]] + assert clan_merge.list_prs_to_merge(prs, ["foo"], bot_name=bot_name) == [prs[0]]