|
2 | 2 | import unittest.mock as mock |
3 | 3 | from unittest.mock import MagicMock |
4 | 4 |
|
| 5 | +import sync2jira.downstream_issue as d_issue |
5 | 6 | import sync2jira.downstream_pr as d |
6 | 7 |
|
7 | 8 | PATH = "sync2jira.downstream_pr." |
@@ -397,6 +398,112 @@ def test_update_pr_transition(self, mock_d_issue): |
397 | 398 | mock_client, self.mock_existing, "CUSTOM_TRANSITION1", self.mock_pr |
398 | 399 | ) |
399 | 400 |
|
| 401 | + @mock.patch(PATH + "d_issue.change_status") |
| 402 | + @mock.patch(PATH + "d_issue.update_jira_issue", wraps=d_issue.update_jira_issue) |
| 403 | + @mock.patch(PATH + "comment_exists", return_value=True) |
| 404 | + @mock.patch(PATH + "format_comment", return_value="mock_comment") |
| 405 | + @mock.patch(PATH + "d_issue.attach_link") |
| 406 | + @mock.patch(PATH + "issue_link_exists", return_value=True) |
| 407 | + def test_update_jira_issue_transition_noop_when_status_none( |
| 408 | + self, |
| 409 | + mock_issue_link_exists, |
| 410 | + mock_attach_link, |
| 411 | + mock_format_comment, |
| 412 | + mock_comment_exists, |
| 413 | + mock_shared_update, |
| 414 | + mock_change_status, |
| 415 | + ): |
| 416 | + """github.pull_request path: PR has status=None, so _update_transition |
| 417 | + is a no-op even when {'transition': 'Closed'} is in pr_updates. |
| 418 | + """ |
| 419 | + self.mock_pr.status = None |
| 420 | + self.mock_pr.downstream = { |
| 421 | + "pr_updates": [{"transition": "Closed"}], |
| 422 | + } |
| 423 | + |
| 424 | + d.update_jira_issue( |
| 425 | + self.mock_existing, self.mock_pr, self.mock_client, self.mock_config |
| 426 | + ) |
| 427 | + |
| 428 | + mock_change_status.assert_not_called() |
| 429 | + |
| 430 | + @mock.patch(PATH + "d_issue.change_status") |
| 431 | + @mock.patch(PATH + "comment_exists", return_value=True) |
| 432 | + @mock.patch(PATH + "format_comment", return_value="mock_comment") |
| 433 | + @mock.patch(PATH + "d_issue.attach_link") |
| 434 | + @mock.patch(PATH + "issue_link_exists", return_value=True) |
| 435 | + def test_update_jira_issue_merged_pr_no_duplicate_transition( |
| 436 | + self, |
| 437 | + mock_issue_link_exists, |
| 438 | + mock_attach_link, |
| 439 | + mock_format_comment, |
| 440 | + mock_comment_exists, |
| 441 | + mock_change_status, |
| 442 | + ): |
| 443 | + """github.issues path: merged PR with status='Closed'. |
| 444 | +
|
| 445 | + merge_transition fires first (via _update_pr_transitions), then |
| 446 | + _update_transition finds Jira already in target state and skips. |
| 447 | + change_status is called once (for merge_transition) not twice. |
| 448 | + """ |
| 449 | + self.mock_pr.suffix = "merged" |
| 450 | + self.mock_pr.status = "Closed" |
| 451 | + self.mock_pr.downstream = { |
| 452 | + "pr_updates": [ |
| 453 | + {"merge_transition": "Closed"}, |
| 454 | + {"transition": "Closed"}, |
| 455 | + ], |
| 456 | + } |
| 457 | + self.mock_existing.fields.status.name = "Closed" |
| 458 | + |
| 459 | + d.update_jira_issue( |
| 460 | + self.mock_existing, self.mock_pr, self.mock_client, self.mock_config |
| 461 | + ) |
| 462 | + |
| 463 | + # merge_transition fires via _update_pr_transition |
| 464 | + mock_change_status.assert_called_once_with( |
| 465 | + self.mock_client, self.mock_existing, "Closed", self.mock_pr |
| 466 | + ) |
| 467 | + |
| 468 | + @mock.patch(PATH + "d_issue.change_status") |
| 469 | + @mock.patch(PATH + "comment_exists", return_value=True) |
| 470 | + @mock.patch(PATH + "format_comment", return_value="mock_comment") |
| 471 | + @mock.patch(PATH + "d_issue.attach_link") |
| 472 | + @mock.patch(PATH + "issue_link_exists", return_value=True) |
| 473 | + def test_update_jira_issue_closed_without_merge_transitions( |
| 474 | + self, |
| 475 | + mock_issue_link_exists, |
| 476 | + mock_attach_link, |
| 477 | + mock_format_comment, |
| 478 | + mock_comment_exists, |
| 479 | + mock_change_status, |
| 480 | + ): |
| 481 | + """github.issues path: PR closed without merge, status='Closed'. |
| 482 | +
|
| 483 | + merge_transition skips (suffix is 'closed', not 'merged'). |
| 484 | + _update_transition sees status=='Closed' and fires, transitioning |
| 485 | + the Jira issue — covering a case merge_transition cannot handle. |
| 486 | + """ |
| 487 | + self.mock_pr.suffix = "closed" |
| 488 | + self.mock_pr.status = "Closed" |
| 489 | + self.mock_pr.url = "mock_url" |
| 490 | + self.mock_pr.downstream = { |
| 491 | + "pr_updates": [ |
| 492 | + {"merge_transition": "Closed"}, |
| 493 | + {"transition": "Closed"}, |
| 494 | + ], |
| 495 | + } |
| 496 | + self.mock_existing.fields.status.name = "Open" |
| 497 | + |
| 498 | + d.update_jira_issue( |
| 499 | + self.mock_existing, self.mock_pr, self.mock_client, self.mock_config |
| 500 | + ) |
| 501 | + |
| 502 | + # Only _update_transition fires (merge_transition skipped) |
| 503 | + mock_change_status.assert_called_once_with( |
| 504 | + self.mock_client, self.mock_existing, "Closed", self.mock_pr |
| 505 | + ) |
| 506 | + |
400 | 507 | @mock.patch(PATH + "update_jira") |
401 | 508 | @mock.patch(PATH + "d_issue") |
402 | 509 | def test_sync_with_jira_create_pr_issue_enabled( |
|
0 commit comments