|
16 | 16 |
|
17 | 17 | import datetime |
18 | 18 | import json |
| 19 | +import typing |
19 | 20 | from typing import TYPE_CHECKING |
20 | 21 | from unittest import mock |
21 | 22 |
|
@@ -260,29 +261,35 @@ async def test_stack_create( |
260 | 261 |
|
261 | 262 | # First stack comment is created |
262 | 263 | assert len(post_comment1_mock.calls) == 1 |
263 | | - expected_body = """This pull request is part of a [Mergify stack](https://docs.mergify.com/stacks/): |
264 | | -
|
265 | | -| # | Pull Request | Link | | |
266 | | -|--:|---|---|---| |
267 | | -| 1 | Title commit 1 | [#1](https://github.com/repo/user/pull/1) | 👈 | |
268 | | -| 2 | Title commit 2 | [#2](https://github.com/repo/user/pull/2) | | |
269 | | -""" |
270 | | - assert json.loads(post_comment1_mock.calls.last.request.content) == { |
271 | | - "body": expected_body, |
272 | | - } |
| 264 | + request_body1 = json.loads(post_comment1_mock.calls.last.request.content)["body"] |
| 265 | + assert request_body1.startswith( |
| 266 | + "This pull request is part of a [Mergify stack](https://docs.mergify.com/stacks/):\n", |
| 267 | + ) |
| 268 | + assert ( |
| 269 | + "| 1 | Title commit 1 | [#1](https://github.com/repo/user/pull/1) | 👈 |" |
| 270 | + in request_body1 |
| 271 | + ) |
| 272 | + assert ( |
| 273 | + "| 2 | Title commit 2 | [#2](https://github.com/repo/user/pull/2) | |" |
| 274 | + in request_body1 |
| 275 | + ) |
| 276 | + assert "<!-- mergify-stack-data: " in request_body1 |
273 | 277 |
|
274 | 278 | # Second stack comment is created |
275 | 279 | assert len(post_comment2_mock.calls) == 1 |
276 | | - expected_body = """This pull request is part of a [Mergify stack](https://docs.mergify.com/stacks/): |
277 | | -
|
278 | | -| # | Pull Request | Link | | |
279 | | -|--:|---|---|---| |
280 | | -| 1 | Title commit 1 | [#1](https://github.com/repo/user/pull/1) | | |
281 | | -| 2 | Title commit 2 | [#2](https://github.com/repo/user/pull/2) | 👈 | |
282 | | -""" |
283 | | - assert json.loads(post_comment2_mock.calls.last.request.content) == { |
284 | | - "body": expected_body, |
285 | | - } |
| 280 | + request_body2 = json.loads(post_comment2_mock.calls.last.request.content)["body"] |
| 281 | + assert request_body2.startswith( |
| 282 | + "This pull request is part of a [Mergify stack](https://docs.mergify.com/stacks/):\n", |
| 283 | + ) |
| 284 | + assert ( |
| 285 | + "| 1 | Title commit 1 | [#1](https://github.com/repo/user/pull/1) | |" |
| 286 | + in request_body2 |
| 287 | + ) |
| 288 | + assert ( |
| 289 | + "| 2 | Title commit 2 | [#2](https://github.com/repo/user/pull/2) | 👈 |" |
| 290 | + in request_body2 |
| 291 | + ) |
| 292 | + assert "<!-- mergify-stack-data: " in request_body2 |
286 | 293 |
|
287 | 294 |
|
288 | 295 | @pytest.mark.respx(base_url="https://api.github.com/") |
@@ -2297,3 +2304,135 @@ async def test_push_branches_skips_notes_when_local_ref_absent( |
2297 | 2304 | "commit1_sha:refs/heads/stack/title--29617d37", |
2298 | 2305 | "+refs/notes/mergify/stack:refs/notes/mergify/stack", |
2299 | 2306 | ) |
| 2307 | + |
| 2308 | + |
| 2309 | +def _make_local_change( |
| 2310 | + *, |
| 2311 | + change_id: str, |
| 2312 | + pull_number: int, |
| 2313 | + head_sha: str, |
| 2314 | + base_branch: str, |
| 2315 | + dest_branch: str, |
| 2316 | + title: str = "t", |
| 2317 | +) -> tuple[changes.LocalChange, github_types.PullRequest]: |
| 2318 | + pull = typing.cast( |
| 2319 | + "github_types.PullRequest", |
| 2320 | + { |
| 2321 | + "number": pull_number, |
| 2322 | + "title": title, |
| 2323 | + "html_url": f"https://github.com/owner/repo/pull/{pull_number}", |
| 2324 | + "head": {"ref": dest_branch, "sha": head_sha}, |
| 2325 | + "base": {"ref": base_branch}, |
| 2326 | + "draft": False, |
| 2327 | + "merged_at": None, |
| 2328 | + "merge_commit_sha": None, |
| 2329 | + "body": "", |
| 2330 | + "state": "open", |
| 2331 | + }, |
| 2332 | + ) |
| 2333 | + change = changes.LocalChange( |
| 2334 | + id=changes.ChangeId(change_id), |
| 2335 | + pull=pull, |
| 2336 | + commit_sha=head_sha, |
| 2337 | + title=title, |
| 2338 | + message="", |
| 2339 | + base_branch=base_branch, |
| 2340 | + dest_branch=dest_branch, |
| 2341 | + action="update", |
| 2342 | + ) |
| 2343 | + return change, pull |
| 2344 | + |
| 2345 | + |
| 2346 | +def test_stack_comment_body_contains_json_marker() -> None: |
| 2347 | + c1, c1_pull = _make_local_change( |
| 2348 | + change_id="Iaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 2349 | + pull_number=1, |
| 2350 | + head_sha="1111111111111111111111111111111111111111", |
| 2351 | + base_branch="main", |
| 2352 | + dest_branch="jd/feature/Iaaaaaaa", |
| 2353 | + ) |
| 2354 | + c2, _ = _make_local_change( |
| 2355 | + change_id="Ibbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 2356 | + pull_number=2, |
| 2357 | + head_sha="2222222222222222222222222222222222222222", |
| 2358 | + base_branch="jd/feature/Iaaaaaaa", |
| 2359 | + dest_branch="jd/feature/Ibbbbbbb", |
| 2360 | + ) |
| 2361 | + comment = push.StackComment([c1, c2]) |
| 2362 | + body = comment.body(c1_pull, stack_id="feature") |
| 2363 | + |
| 2364 | + marker_prefix = "<!-- mergify-stack-data: " |
| 2365 | + marker_lines = [ |
| 2366 | + line for line in body.splitlines() if line.startswith(marker_prefix) |
| 2367 | + ] |
| 2368 | + assert len(marker_lines) == 1 |
| 2369 | + assert marker_lines[0].endswith(" -->") |
| 2370 | + |
| 2371 | + payload = json.loads(marker_lines[0][len(marker_prefix) : -len(" -->")]) |
| 2372 | + assert payload == { |
| 2373 | + "schema_version": 1, |
| 2374 | + "stack_id": "feature", |
| 2375 | + "pulls": [ |
| 2376 | + { |
| 2377 | + "number": 1, |
| 2378 | + "change_id": "Iaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 2379 | + "head_sha": "1111111111111111111111111111111111111111", |
| 2380 | + "base_branch": "main", |
| 2381 | + "dest_branch": "jd/feature/Iaaaaaaa", |
| 2382 | + "is_current": True, |
| 2383 | + }, |
| 2384 | + { |
| 2385 | + "number": 2, |
| 2386 | + "change_id": "Ibbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 2387 | + "head_sha": "2222222222222222222222222222222222222222", |
| 2388 | + "base_branch": "jd/feature/Iaaaaaaa", |
| 2389 | + "dest_branch": "jd/feature/Ibbbbbbb", |
| 2390 | + "is_current": False, |
| 2391 | + }, |
| 2392 | + ], |
| 2393 | + } |
| 2394 | + |
| 2395 | + |
| 2396 | +def test_stack_comment_is_current_flips_per_pull() -> None: |
| 2397 | + c1, _ = _make_local_change( |
| 2398 | + change_id="Iaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 2399 | + pull_number=1, |
| 2400 | + head_sha="1111111111111111111111111111111111111111", |
| 2401 | + base_branch="main", |
| 2402 | + dest_branch="jd/feature/Iaaaaaaa", |
| 2403 | + ) |
| 2404 | + c2, c2_pull = _make_local_change( |
| 2405 | + change_id="Ibbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 2406 | + pull_number=2, |
| 2407 | + head_sha="2222222222222222222222222222222222222222", |
| 2408 | + base_branch="jd/feature/Iaaaaaaa", |
| 2409 | + dest_branch="jd/feature/Ibbbbbbb", |
| 2410 | + ) |
| 2411 | + comment = push.StackComment([c1, c2]) |
| 2412 | + |
| 2413 | + body2 = comment.body(c2_pull, stack_id="feature") |
| 2414 | + marker_line = next( |
| 2415 | + line |
| 2416 | + for line in body2.splitlines() |
| 2417 | + if line.startswith("<!-- mergify-stack-data: ") |
| 2418 | + ) |
| 2419 | + payload = json.loads(marker_line[len("<!-- mergify-stack-data: ") : -len(" -->")]) |
| 2420 | + assert [p["is_current"] for p in payload["pulls"]] == [False, True] |
| 2421 | + |
| 2422 | + |
| 2423 | +def test_stack_comment_body_json_marker_is_single_line_compact() -> None: |
| 2424 | + c1, c1_pull = _make_local_change( |
| 2425 | + change_id="Iaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 2426 | + pull_number=1, |
| 2427 | + head_sha="1111111111111111111111111111111111111111", |
| 2428 | + base_branch="main", |
| 2429 | + dest_branch="jd/feature/Iaaaaaaa", |
| 2430 | + ) |
| 2431 | + comment = push.StackComment([c1]) |
| 2432 | + body = comment.body(c1_pull, stack_id="feature") |
| 2433 | + marker_prefix = "<!-- mergify-stack-data: " |
| 2434 | + marker_line = next( |
| 2435 | + line for line in body.splitlines() if line.startswith(marker_prefix) |
| 2436 | + ) |
| 2437 | + assert '", ' not in marker_line |
| 2438 | + assert '": ' not in marker_line |
0 commit comments