Skip to content

Commit edba4f6

Browse files
committed
[REF] refactor to improve _set_lines_issue coverage
1 parent 8e707c6 commit edba4f6

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

newsfragments/190.bugfix

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make the command ``/ocabot migration`` working when migration
2+
issue doesn't contain migration module lines.

src/oca_github_bot/tasks/migration_issue_bot.py

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

3131

32-
def _set_lines_issue(gh_pr, issue, module):
32+
def _set_lines_issue(gh_pr_user_login, gh_pr_number, issue_body, module):
3333
lines = []
3434
added = False
3535
module_list = False
36-
new_line = f"- [ ] {module} - By @{gh_pr.user.login} - #{gh_pr.number}"
37-
for line in issue.body.split("\n"):
36+
new_line = f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}"
37+
for line in issue_body.split("\n"):
3838
if added: # Bypass the checks for faster completion
3939
lines.append(line)
4040
continue
@@ -62,7 +62,7 @@ def _set_lines_issue(gh_pr, issue, module):
6262
# make the addition working on an empty migration issue
6363
if not added:
6464
lines.append(new_line)
65-
return lines
65+
return "\n".join(lines)
6666

6767

6868
@task()
@@ -113,8 +113,10 @@ def migration_issue_start(org, repo, pr, username, module=None, dry_run=False):
113113
)
114114
return
115115
# Change issue to add the PR in the module list
116-
lines = _set_lines_issue(gh_pr, issue, module)
117-
issue.edit(body="\n".join(lines))
116+
new_body = _set_lines_issue(
117+
gh_pr.user.login, gh_pr.number, issue.body, module
118+
)
119+
issue.edit(body=new_body)
118120
except Exception as e:
119121
github.gh_call(
120122
gh_pr.create_comment,

tests/test_migration_issue_bot.py

+31-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,34 @@ def test_find_issue(gh):
3131

3232
@pytest.mark.vcr()
3333
def test_set_lines_issue(gh):
34-
repo = _get_repository(gh, "OCA", "contract")
35-
milestone = _create_or_find_branch_milestone(repo, "14.0")
36-
issue = _find_issue(repo, milestone, "14.0")
37-
gh_pr = gh.pull_request("OCA", "contract", 705)
38-
lines = _set_lines_issue(gh_pr, issue, "contract")
39-
assert len(lines) > 0
34+
module = "mis_builder"
35+
gh_pr_user_login = "sbidoul"
36+
gh_pr_number = 11
37+
38+
body_transformation = [
39+
(
40+
"Issue with list but not the module\n"
41+
"- [ ] a_module_1 - By @legalsylvain - #1\n"
42+
"- [ ] z_module_1 - By @pedrobaeza - #2",
43+
f"Issue with list but not the module\n"
44+
f"- [ ] a_module_1 - By @legalsylvain - #1\n"
45+
f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n"
46+
f"- [ ] z_module_1 - By @pedrobaeza - #2",
47+
),
48+
(
49+
f"Issue with list containing the module\n"
50+
f"- [x] {module} - By @legalsylvain - #1\n"
51+
f"- [ ] z_module_1 - By @pedrobaeza - #2",
52+
f"Issue with list containing the module\n"
53+
f"- [x] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n"
54+
f"- [ ] z_module_1 - By @pedrobaeza - #2",
55+
),
56+
(
57+
"Issue with no list",
58+
f"Issue with no list\n"
59+
f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}",
60+
),
61+
]
62+
for (old_body, new_body_expected) in body_transformation:
63+
new_body = _set_lines_issue(gh_pr_user_login, gh_pr_number, old_body, module)
64+
assert new_body == new_body_expected

0 commit comments

Comments
 (0)