Skip to content
Merged
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
9 changes: 8 additions & 1 deletion docs/source/actions/backport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ Options
- ``conflicts``
- The label to add to the created pull request if it has conflicts and
``ignore_conflicts`` is set to ``true``.

* - ``assignees``
- list of :ref:`data type template`
-
- Users to assign the newly created pull request. As the type is
:ref:`data type template`, you could use, e.g., ``{{author}}`` to assign
the pull request to its original author.

Examples
--------
Expand All @@ -94,5 +99,7 @@ added and the pull request merged:
backport:
branches:
- stable
assignees:
- "{{ author }}"

.. include:: ../global-substitutions.rst
6 changes: 6 additions & 0 deletions docs/source/actions/copy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ Options
- ``conflicts``
- The label to add to the created pull request if it has conflicts and
``ignore_conflicts`` is set to ``true``.
* - ``assignees``
- list of :ref:`data type template`
-
- Users to assign the newly created pull request. As the type is
:ref:`data type template`, you could use, e.g., ``{{author}}`` to assign
the pull request to its original author.

Examples
--------
Expand Down
17 changes: 17 additions & 0 deletions mergify_engine/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,20 @@ async def get_rule(
return EvaluatedActionRule(
"", rules.RuleConditions([]), rules.RuleMissingConditions([])
)

@staticmethod
async def wanted_users(
Comment thread
jd marked this conversation as resolved.
ctxt: context.Context, users: typing.List[str]
) -> typing.List[str]:
wanted = set()
for user in set(users):
try:
user = await ctxt.pull_request.render_template(user)
except context.RenderTemplateFailure:
# NOTE: this should never happen since
# the template is validated when parsing the config 🤷
continue
else:
wanted.add(user)

return list(wanted)
20 changes: 2 additions & 18 deletions mergify_engine/actions/assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def run(
async def _add_assignees(
self, ctxt: context.Context, users_to_add: typing.List[str]
) -> check_api.Result:
assignees = await self._wanted_users(ctxt, users_to_add)
assignees = await self.wanted_users(ctxt, users_to_add)

if assignees:
try:
Expand Down Expand Up @@ -87,7 +87,7 @@ async def _add_assignees(
async def _remove_assignees(
self, ctxt: context.Context, users_to_remove: typing.List[str]
) -> check_api.Result:
assignees = await self._wanted_users(ctxt, users_to_remove)
assignees = await self.wanted_users(ctxt, users_to_remove)

if assignees:
try:
Expand All @@ -113,19 +113,3 @@ async def _remove_assignees(
"Empty users list",
"No user removed from assignees",
)

async def _wanted_users(
self, ctxt: context.Context, users: typing.List[str]
) -> typing.List[str]:
wanted = set()
for user in set(users):
try:
user = await ctxt.pull_request.render_template(user)
except context.RenderTemplateFailure:
# NOTE: this should never happen since
# the template is validated when parsing the config 🤷
continue
else:
wanted.add(user)

return list(wanted)
4 changes: 4 additions & 0 deletions mergify_engine/actions/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from mergify_engine import github_types
from mergify_engine import rules
from mergify_engine.clients import http
from mergify_engine.rules import types


def Regex(value: str) -> typing.Pattern[str]:
Expand All @@ -46,6 +47,7 @@ class CopyAction(actions.Action):
voluptuous.Required("branches", default=[]): [str],
voluptuous.Required("regexes", default=[]): [voluptuous.Coerce(Regex)],
voluptuous.Required("ignore_conflicts", default=True): bool,
voluptuous.Required("assignees", default=[]): [types.Jinja2],
voluptuous.Required("labels", default=[]): [str],
voluptuous.Required("label_conflicts", default="conflicts"): str,
}
Expand Down Expand Up @@ -75,12 +77,14 @@ async def _copy(self, ctxt, branch_name):
# No, then do it
if not new_pull:
try:
users_to_add = await self.wanted_users(ctxt, self.config["assignees"])
new_pull = await duplicate_pull.duplicate(
ctxt,
branch_name,
self.config["labels"],
self.config["label_conflicts"],
self.config["ignore_conflicts"],
users_to_add,
self.KIND,
)
except duplicate_pull.DuplicateAlreadyExists:
Expand Down
10 changes: 10 additions & 0 deletions mergify_engine/duplicate_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ async def duplicate(
labels: typing.Optional[List[str]] = None,
label_conflicts: typing.Optional[str] = None,
ignore_conflicts: bool = False,
assignees: typing.Optional[List[str]] = None,
kind: KindT = "backport",
) -> typing.Optional[github_types.GitHubPullRequest]:
"""Duplicate a pull request.
Expand All @@ -225,6 +226,7 @@ async def duplicate(
:param labels: The list of labels to add to the created PR.
:param label_conflicts: The label to add to the created PR when cherry-pick failed.
:param ignore_conflicts: Whether to commit the result if the cherry-pick fails.
:param assignees: The list of users to be assigned to the created PR.
:param kind: is a backport or a copy
"""
repo_full_name = ctxt.pull["base"]["repo"]["full_name"]
Expand Down Expand Up @@ -349,4 +351,12 @@ async def duplicate(
json={"labels": effective_labels},
)

if assignees is not None and len(assignees) > 0:
# NOTE(sileht): we don't have to deal with invalid assignees as GitHub
# just ignore them and always return 201
await ctxt.client.post(
f"{ctxt.base_url}/issues/{duplicate_pr['number']}/assignees",
json={"assignees": assignees},
)

return duplicate_pr
6 changes: 5 additions & 1 deletion mergify_engine/tests/functional/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ async def test_backport_ignore_conflicts(self):
"backported",
"conflicts",
]
assert pull["assignees"] == []

async def _do_test_backport(self, method, config=None):
stable_branch = self.get_full_branch_name("stable/#3.1")
Expand Down Expand Up @@ -421,9 +422,12 @@ async def test_backport_merge_commit(self):
async def test_backport_merge_commit_regexes(self):
prefix = self.get_full_branch_name("stable")
p = await self._do_test_backport(
"merge", config={"regexes": [f"^{prefix}/.*$"]}
"merge",
config={"regexes": [f"^{prefix}/.*$"], "assignees": ["mergify-test3"]},
)
assert 2 == p["commits"]
assert len(p["assignees"]) == 1
assert p["assignees"][0]["login"] == "mergify-test3"

async def test_backport_squash_and_merge(self):
p = await self._do_test_backport("squash")
Expand Down
1 change: 1 addition & 0 deletions mergify_engine/tests/unit/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ def test_command_loader():
"ignore_conflicts": True,
"labels": [],
"label_conflicts": "conflicts",
"assignees": [],
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20210322233744
20210407093837
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"args": ["init"], "kwargs": {}, "out": "hint: Using 'master' as the name for the initial branch. This default branch name\nhint: is subject to change. To configure the initial branch name to use in all\nhint: of your new repositories, which will suppress this warning, call:\nhint: \nhint: \tgit config --global init.defaultBranch <name>\nhint: \nhint: Names commonly chosen instead of 'master' are 'main', 'trunk' and\nhint: 'development'. The just-created branch can be renamed via this command:\nhint: \nhint: \tgit branch -m <name>\nInitialized empty Git repository in /tmp/mergify-gitter3i84_qkh/.git/\n"}, {"args": ["config", "user.name", "mergify-bot"], "kwargs": {}, "out": ""}, {"args": ["config", "user.email", "noreply@mergify.io"], "kwargs": {}, "out": ""}, {"args": ["config", "credential.useHttpPath", "true"], "kwargs": {}, "out": ""}, {"args": ["config", "credential.helper", "cache --timeout=300 --socket=/tmp/mergify-gitter<random>/.git/creds/socket"], "kwargs": {}, "out": ""}, {"args": ["credential", "approve"], "kwargs": {"_input": "url=https://<TOKEN>:@github.com/mergifyio-testing/functional-testing-repo\n\n"}, "out": ""}, {"args": ["credential", "approve"], "kwargs": {"_input": "url=https://<TOKEN>:@github.com/mergify-test2/functional-testing-repo\n\n"}, "out": ""}, {"args": ["config", "user.name", "mergify-tester"], "kwargs": {}, "out": ""}, {"args": ["remote", "add", "main", "https://github.com/mergifyio-testing/functional-testing-repo"], "kwargs": {}, "out": ""}, {"args": ["remote", "add", "fork", "https://github.com/mergify-test2/functional-testing-repo"], "kwargs": {}, "out": ""}, {"args": ["add", ".mergify.yml"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "initial commit"], "kwargs": {}, "out": "[master (root-commit) 1fc8d44] initial commit\n 1 file changed, 17 insertions(+)\n create mode 100644 .mergify.yml\n"}, {"args": ["branch", "-M", "20210322233744/test_backport_merge_commit_regexes/master"], "kwargs": {}, "out": ""}, {"args": ["branch", "20210322233744/test_backport_merge_commit_regexes/stable/#3.1", "20210322233744/test_backport_merge_commit_regexes/master"], "kwargs": {}, "out": ""}, {"args": ["push", "--quiet", "main", "20210322233744/test_backport_merge_commit_regexes/master", "20210322233744/test_backport_merge_commit_regexes/stable/#3.1"], "kwargs": {}, "out": ""}, {"args": ["push", "--quiet", "fork", "20210322233744/test_backport_merge_commit_regexes/master", "20210322233744/test_backport_merge_commit_regexes/stable/#3.1"], "kwargs": {}, "out": ""}, {"args": ["checkout", "--quiet", "fork/20210322233744/test_backport_merge_commit_regexes/master", "-b", "20210322233744/test_backport_merge_commit_regexes/fork/pr1"], "kwargs": {}, "out": ""}, {"args": ["add", "test1"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "test_backport_merge_commit_regexes: pull request n1 from fork"], "kwargs": {}, "out": "[20210322233744/test_backport_merge_commit_regexes/fork/pr1 58633a9] test_backport_merge_commit_regexes: pull request n1 from fork\n 1 file changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 test1\n"}, {"args": ["mv", "test1", "test1-moved"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "test_backport_merge_commit_regexes: pull request n1 from fork, moved"], "kwargs": {}, "out": "[20210322233744/test_backport_merge_commit_regexes/fork/pr1 5da4b9b] test_backport_merge_commit_regexes: pull request n1 from fork, moved\n 1 file changed, 0 insertions(+), 0 deletions(-)\n rename test1 => test1-moved (100%)\n"}, {"args": ["push", "--quiet", "fork", "20210322233744/test_backport_merge_commit_regexes/fork/pr1"], "kwargs": {}, "out": "remote: \nremote: Create a pull request for '20210322233744/test_backport_merge_commit_regexes/fork/pr1' on GitHub by visiting: \nremote: https://github.com/mergify-test2/functional-testing-repo/pull/new/20210322233744/test_backport_merge_commit_regexes/fork/pr1 \nremote: \n"}, {"args": ["checkout", "--quiet", "fork/20210322233744/test_backport_merge_commit_regexes/stable/#3.1", "-b", "20210322233744/test_backport_merge_commit_regexes/fork/pr2"], "kwargs": {}, "out": ""}, {"args": ["add", "test2"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "test_backport_merge_commit_regexes: pull request n2 from fork"], "kwargs": {}, "out": "[20210322233744/test_backport_merge_commit_regexes/fork/pr2 a6971b9] test_backport_merge_commit_regexes: pull request n2 from fork\n 1 file changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 test2\n"}, {"args": ["push", "--quiet", "fork", "20210322233744/test_backport_merge_commit_regexes/fork/pr2"], "kwargs": {}, "out": "remote: \nremote: Create a pull request for '20210322233744/test_backport_merge_commit_regexes/fork/pr2' on GitHub by visiting: \nremote: https://github.com/mergify-test2/functional-testing-repo/pull/new/20210322233744/test_backport_merge_commit_regexes/fork/pr2 \nremote: \n"}, {"args": ["credential-cache", "--socket=/tmp/mergify-gitter<random>/.git/creds/socket", "exit"], "kwargs": {}, "out": ""}]
[{"args": ["init"], "kwargs": {}, "out": "hint: Using 'master' as the name for the initial branch. This default branch name\nhint: is subject to change. To configure the initial branch name to use in all\nhint: of your new repositories, which will suppress this warning, call:\nhint: \nhint: \tgit config --global init.defaultBranch <name>\nhint: \nhint: Names commonly chosen instead of 'master' are 'main', 'trunk' and\nhint: 'development'. The just-created branch can be renamed via this command:\nhint: \nhint: \tgit branch -m <name>\nInitialized empty Git repository in /tmp/mergify-gitterje8oedot/.git/\n"}, {"args": ["config", "user.name", "mergify-bot"], "kwargs": {}, "out": ""}, {"args": ["config", "user.email", "noreply@mergify.io"], "kwargs": {}, "out": ""}, {"args": ["config", "credential.useHttpPath", "true"], "kwargs": {}, "out": ""}, {"args": ["config", "credential.helper", "cache --timeout=300 --socket=/tmp/mergify-gitter<random>/.git/creds/socket"], "kwargs": {}, "out": ""}, {"args": ["credential", "approve"], "kwargs": {"_input": "url=https://<TOKEN>:@github.com/mergifyio-testing/functional-testing-repo\n\n"}, "out": ""}, {"args": ["credential", "approve"], "kwargs": {"_input": "url=https://<TOKEN>:@github.com/mergify-test2/functional-testing-repo\n\n"}, "out": ""}, {"args": ["config", "user.name", "mergify-tester"], "kwargs": {}, "out": ""}, {"args": ["remote", "add", "main", "https://github.com/mergifyio-testing/functional-testing-repo"], "kwargs": {}, "out": ""}, {"args": ["remote", "add", "fork", "https://github.com/mergify-test2/functional-testing-repo"], "kwargs": {}, "out": ""}, {"args": ["add", ".mergify.yml"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "initial commit"], "kwargs": {}, "out": "[master (root-commit) f5f7d80] initial commit\n 1 file changed, 19 insertions(+)\n create mode 100644 .mergify.yml\n"}, {"args": ["branch", "-M", "20210407093837/test_backport_merge_commit_regexes/master"], "kwargs": {}, "out": ""}, {"args": ["branch", "20210407093837/test_backport_merge_commit_regexes/stable/#3.1", "20210407093837/test_backport_merge_commit_regexes/master"], "kwargs": {}, "out": ""}, {"args": ["push", "--quiet", "main", "20210407093837/test_backport_merge_commit_regexes/master", "20210407093837/test_backport_merge_commit_regexes/stable/#3.1"], "kwargs": {}, "out": ""}, {"args": ["push", "--quiet", "fork", "20210407093837/test_backport_merge_commit_regexes/master", "20210407093837/test_backport_merge_commit_regexes/stable/#3.1"], "kwargs": {}, "out": ""}, {"args": ["checkout", "--quiet", "fork/20210407093837/test_backport_merge_commit_regexes/master", "-b", "20210407093837/test_backport_merge_commit_regexes/fork/pr1"], "kwargs": {}, "out": ""}, {"args": ["add", "test1"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "test_backport_merge_commit_regexes: pull request n1 from fork"], "kwargs": {}, "out": "[20210407093837/test_backport_merge_commit_regexes/fork/pr1 6898fa8] test_backport_merge_commit_regexes: pull request n1 from fork\n 1 file changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 test1\n"}, {"args": ["mv", "test1", "test1-moved"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "test_backport_merge_commit_regexes: pull request n1 from fork, moved"], "kwargs": {}, "out": "[20210407093837/test_backport_merge_commit_regexes/fork/pr1 43b18d1] test_backport_merge_commit_regexes: pull request n1 from fork, moved\n 1 file changed, 0 insertions(+), 0 deletions(-)\n rename test1 => test1-moved (100%)\n"}, {"args": ["push", "--quiet", "fork", "20210407093837/test_backport_merge_commit_regexes/fork/pr1"], "kwargs": {}, "out": "remote: \nremote: Create a pull request for '20210407093837/test_backport_merge_commit_regexes/fork/pr1' on GitHub by visiting: \nremote: https://github.com/mergify-test2/functional-testing-repo/pull/new/20210407093837/test_backport_merge_commit_regexes/fork/pr1 \nremote: \n"}, {"args": ["checkout", "--quiet", "fork/20210407093837/test_backport_merge_commit_regexes/stable/#3.1", "-b", "20210407093837/test_backport_merge_commit_regexes/fork/pr2"], "kwargs": {}, "out": ""}, {"args": ["add", "test2"], "kwargs": {}, "out": ""}, {"args": ["commit", "--no-edit", "-m", "test_backport_merge_commit_regexes: pull request n2 from fork"], "kwargs": {}, "out": "[20210407093837/test_backport_merge_commit_regexes/fork/pr2 626da85] test_backport_merge_commit_regexes: pull request n2 from fork\n 1 file changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 test2\n"}, {"args": ["push", "--quiet", "fork", "20210407093837/test_backport_merge_commit_regexes/fork/pr2"], "kwargs": {}, "out": "remote: \nremote: Create a pull request for '20210407093837/test_backport_merge_commit_regexes/fork/pr2' on GitHub by visiting: \nremote: https://github.com/mergify-test2/functional-testing-repo/pull/new/20210407093837/test_backport_merge_commit_regexes/fork/pr2 \nremote: \n"}, {"args": ["credential-cache", "--socket=/tmp/mergify-gitter<random>/.git/creds/socket", "exit"], "kwargs": {}, "out": ""}]
Loading