From 654a78dec33e621e63331a5cf56f6eece5a9fede Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Sun, 3 Jul 2022 17:44:21 +0200 Subject: [PATCH 1/2] =?UTF-8?q?[IMP]=20ocabot=20merge=20:=20check=20line?= =?UTF-8?q?=20in=20migration=20issue=20Co-authored-by:=20Simone=20Rubino?= =?UTF-8?q?=20=20[IMP]=20Do=20not=20create=20milestone?= =?UTF-8?q?=20branch=20when=20merging=20PR=20Co-authored-by:=20St=C3=A9pha?= =?UTF-8?q?ne=20Bidoul=20=20[pre-commit.ci]=20a?= =?UTF-8?q?uto=20fixes=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- newsfragments/192.bugfix | 1 + src/oca_github_bot/tasks/merge_bot.py | 8 +++++++ .../tasks/migration_issue_bot.py | 12 ++++++++++ tests/test_migration_issue_bot.py | 23 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 newsfragments/192.bugfix diff --git a/newsfragments/192.bugfix b/newsfragments/192.bugfix new file mode 100644 index 00000000..58e8dbf2 --- /dev/null +++ b/newsfragments/192.bugfix @@ -0,0 +1 @@ +Automaticaly check line in Migration issue when merging the according Pull Request. diff --git a/src/oca_github_bot/tasks/merge_bot.py b/src/oca_github_bot/tasks/merge_bot.py index 4d1d2f6d..48c7bf14 100644 --- a/src/oca_github_bot/tasks/merge_bot.py +++ b/src/oca_github_bot/tasks/merge_bot.py @@ -27,6 +27,7 @@ from ..utils import hide_secrets from ..version_branch import make_merge_bot_branch, parse_merge_bot_branch from .main_branch_bot import main_branch_bot_actions +from .migration_issue_bot import _check_line_issue, _find_issue _logger = getLogger(__name__) @@ -186,6 +187,7 @@ def _merge_bot_merge_pr(org, repo, merge_bot_branch, cwd, dry_run=False): _git_delete_branch("origin", merge_bot_branch, cwd=cwd) with github.login() as gh: gh_pr = gh.pull_request(org, repo, pr) + gh_repo = gh.repository(org, repo) merge_sha = github.git_get_head_sha(cwd=cwd) github.gh_call( gh_pr.create_comment, @@ -200,6 +202,12 @@ def _merge_bot_merge_pr(org, repo, merge_bot_branch, cwd, dry_run=False): _logger.info(f"add {LABEL_MERGED} label to PR {gh_pr.url}") github.gh_call(gh_issue.add_labels, LABEL_MERGED) github.gh_call(gh_pr.close) + + # Check line in migration issue if required + migration_issue = _find_issue(gh_repo, target_branch) + if migration_issue: + new_body = _check_line_issue(gh_pr.number, migration_issue.body) + migration_issue.edit(body=new_body) return True diff --git a/src/oca_github_bot/tasks/migration_issue_bot.py b/src/oca_github_bot/tasks/migration_issue_bot.py index b4911a7c..c07328c1 100644 --- a/src/oca_github_bot/tasks/migration_issue_bot.py +++ b/src/oca_github_bot/tasks/migration_issue_bot.py @@ -29,6 +29,18 @@ def _find_issue(gh_repo, milestone, target_branch): return issue +def _check_line_issue(gh_pr_number, issue_body): + lines = [] + regex = r"\#%s\b" % gh_pr_number + for line in issue_body.split("\n"): + if re.findall(regex, line): + checked_line = line.replace("[ ]", "[x]", 1) + lines.append(checked_line) + continue + lines.append(line) + return "\n".join(lines) + + def _set_lines_issue(gh_pr_user_login, gh_pr_number, issue_body, module): lines = [] added = False diff --git a/tests/test_migration_issue_bot.py b/tests/test_migration_issue_bot.py index b54863f7..5fb8f439 100644 --- a/tests/test_migration_issue_bot.py +++ b/tests/test_migration_issue_bot.py @@ -4,6 +4,7 @@ import pytest from oca_github_bot.tasks.migration_issue_bot import ( + _check_line_issue, _create_or_find_branch_milestone, _find_issue, _set_lines_issue, @@ -70,3 +71,25 @@ def test_set_lines_issue(gh): for old_body, new_body_expected in body_transformation: new_body, _ = _set_lines_issue(gh_pr_user_login, gh_pr_number, old_body, module) assert new_body == new_body_expected + + +@pytest.mark.vcr() +def test_check_line_issue(gh): + module = "mis_builder" + gh_pr_user_login = "sbidoul" + gh_pr_number = 11 + + old_body = ( + f"Issue with list containing the module\n" + f"- [ ] a_module_1 - By @legalsylvain - #1\n" + f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" + f"- [ ] z_module_1 - By @pedrobaeza - #2" + ) + new_body_expected = ( + f"Issue with list containing the module\n" + f"- [ ] a_module_1 - By @legalsylvain - #1\n" + f"- [x] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" + f"- [ ] z_module_1 - By @pedrobaeza - #2" + ) + new_body = _check_line_issue(gh_pr_number, old_body) + assert new_body == new_body_expected From fd2107475cccbdc1e76efc7061e8326f1c63b10f Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Sat, 8 Apr 2023 12:10:16 +0200 Subject: [PATCH 2/2] [REF] create a dedicated function _mark_migration_done_in_migration_issue --- src/oca_github_bot/tasks/merge_bot.py | 7 ++----- src/oca_github_bot/tasks/migration_issue_bot.py | 7 +++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/oca_github_bot/tasks/merge_bot.py b/src/oca_github_bot/tasks/merge_bot.py index 48c7bf14..1c7262ac 100644 --- a/src/oca_github_bot/tasks/merge_bot.py +++ b/src/oca_github_bot/tasks/merge_bot.py @@ -27,7 +27,7 @@ from ..utils import hide_secrets from ..version_branch import make_merge_bot_branch, parse_merge_bot_branch from .main_branch_bot import main_branch_bot_actions -from .migration_issue_bot import _check_line_issue, _find_issue +from .migration_issue_bot import _mark_migration_done_in_migration_issue _logger = getLogger(__name__) @@ -204,10 +204,7 @@ def _merge_bot_merge_pr(org, repo, merge_bot_branch, cwd, dry_run=False): github.gh_call(gh_pr.close) # Check line in migration issue if required - migration_issue = _find_issue(gh_repo, target_branch) - if migration_issue: - new_body = _check_line_issue(gh_pr.number, migration_issue.body) - migration_issue.edit(body=new_body) + _mark_migration_done_in_migration_issue(gh_repo, target_branch, gh_pr) return True diff --git a/src/oca_github_bot/tasks/migration_issue_bot.py b/src/oca_github_bot/tasks/migration_issue_bot.py index c07328c1..a563673e 100644 --- a/src/oca_github_bot/tasks/migration_issue_bot.py +++ b/src/oca_github_bot/tasks/migration_issue_bot.py @@ -83,6 +83,13 @@ def _set_lines_issue(gh_pr_user_login, gh_pr_number, issue_body, module): return "\n".join(lines), old_pr_number +def _mark_migration_done_in_migration_issue(gh_repo, target_branch, gh_pr): + migration_issue = _find_issue(gh_repo, target_branch) + if migration_issue: + new_body = _check_line_issue(gh_pr.number, migration_issue.body) + migration_issue.edit(body=new_body) + + @task() @switchable("migration_issue_bot") def migration_issue_start(org, repo, pr, username, module=None, dry_run=False):