Skip to content

Commit 16a0546

Browse files
authored
Merge pull request #192 from legalsylvain/IMP-ocabot-merge-check-migration-issue
[IMP] ocabot merge : check line in migration issue
2 parents 9f5d5d5 + fd21074 commit 16a0546

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

newsfragments/192.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Automaticaly check line in Migration issue when merging the according Pull Request.

src/oca_github_bot/tasks/merge_bot.py

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ..utils import hide_secrets
2828
from ..version_branch import make_merge_bot_branch, parse_merge_bot_branch
2929
from .main_branch_bot import main_branch_bot_actions
30+
from .migration_issue_bot import _mark_migration_done_in_migration_issue
3031

3132
_logger = getLogger(__name__)
3233

@@ -186,6 +187,7 @@ def _merge_bot_merge_pr(org, repo, merge_bot_branch, cwd, dry_run=False):
186187
_git_delete_branch("origin", merge_bot_branch, cwd=cwd)
187188
with github.login() as gh:
188189
gh_pr = gh.pull_request(org, repo, pr)
190+
gh_repo = gh.repository(org, repo)
189191
merge_sha = github.git_get_head_sha(cwd=cwd)
190192
github.gh_call(
191193
gh_pr.create_comment,
@@ -200,6 +202,9 @@ def _merge_bot_merge_pr(org, repo, merge_bot_branch, cwd, dry_run=False):
200202
_logger.info(f"add {LABEL_MERGED} label to PR {gh_pr.url}")
201203
github.gh_call(gh_issue.add_labels, LABEL_MERGED)
202204
github.gh_call(gh_pr.close)
205+
206+
# Check line in migration issue if required
207+
_mark_migration_done_in_migration_issue(gh_repo, target_branch, gh_pr)
203208
return True
204209

205210

src/oca_github_bot/tasks/migration_issue_bot.py

+19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ def _find_issue(gh_repo, milestone, target_branch):
2929
return issue
3030

3131

32+
def _check_line_issue(gh_pr_number, issue_body):
33+
lines = []
34+
regex = r"\#%s\b" % gh_pr_number
35+
for line in issue_body.split("\n"):
36+
if re.findall(regex, line):
37+
checked_line = line.replace("[ ]", "[x]", 1)
38+
lines.append(checked_line)
39+
continue
40+
lines.append(line)
41+
return "\n".join(lines)
42+
43+
3244
def _set_lines_issue(gh_pr_user_login, gh_pr_number, issue_body, module):
3345
lines = []
3446
added = False
@@ -71,6 +83,13 @@ def _set_lines_issue(gh_pr_user_login, gh_pr_number, issue_body, module):
7183
return "\n".join(lines), old_pr_number
7284

7385

86+
def _mark_migration_done_in_migration_issue(gh_repo, target_branch, gh_pr):
87+
migration_issue = _find_issue(gh_repo, target_branch)
88+
if migration_issue:
89+
new_body = _check_line_issue(gh_pr.number, migration_issue.body)
90+
migration_issue.edit(body=new_body)
91+
92+
7493
@task()
7594
@switchable("migration_issue_bot")
7695
def migration_issue_start(org, repo, pr, username, module=None, dry_run=False):

tests/test_migration_issue_bot.py

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from oca_github_bot.tasks.migration_issue_bot import (
7+
_check_line_issue,
78
_create_or_find_branch_milestone,
89
_find_issue,
910
_set_lines_issue,
@@ -70,3 +71,25 @@ def test_set_lines_issue(gh):
7071
for old_body, new_body_expected in body_transformation:
7172
new_body, _ = _set_lines_issue(gh_pr_user_login, gh_pr_number, old_body, module)
7273
assert new_body == new_body_expected
74+
75+
76+
@pytest.mark.vcr()
77+
def test_check_line_issue(gh):
78+
module = "mis_builder"
79+
gh_pr_user_login = "sbidoul"
80+
gh_pr_number = 11
81+
82+
old_body = (
83+
f"Issue with list containing the module\n"
84+
f"- [ ] a_module_1 - By @legalsylvain - #1\n"
85+
f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n"
86+
f"- [ ] z_module_1 - By @pedrobaeza - #2"
87+
)
88+
new_body_expected = (
89+
f"Issue with list containing the module\n"
90+
f"- [ ] a_module_1 - By @legalsylvain - #1\n"
91+
f"- [x] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n"
92+
f"- [ ] z_module_1 - By @pedrobaeza - #2"
93+
)
94+
new_body = _check_line_issue(gh_pr_number, old_body)
95+
assert new_body == new_body_expected

0 commit comments

Comments
 (0)