|
56 | 56 |
|
57 | 57 | logger = logging.getLogger(__name__) |
58 | 58 |
|
| 59 | +PR_ENVIRONMENT_PLAN_LINK_COMMENT_MARKER = "<!-- sqlmesh-pr-plan-link -->" |
| 60 | +PROD_PLAN_LINK_COMMENT_MARKER = "<!-- sqlmesh-prod-plan-link -->" |
| 61 | + |
59 | 62 |
|
60 | 63 | class TestFailure(Exception): |
61 | 64 | pass |
@@ -678,16 +681,19 @@ def run_linter(self) -> None: |
678 | 681 | self._context.lint_models() |
679 | 682 |
|
680 | 683 | def _get_or_create_comment(self, header: str = BOT_HEADER_MSG) -> IssueComment: |
681 | | - comment = seq_get( |
682 | | - [comment for comment in self._issue.get_comments() if header in comment.body], |
683 | | - 0, |
684 | | - ) |
| 684 | + comment = self._get_comment(header=header) |
685 | 685 | if not comment: |
686 | 686 | logger.debug(f"Did not find comment so creating one with header: {header}") |
687 | 687 | return self._issue.create_comment(header) |
688 | 688 | logger.debug(f"Found comment with header: {header}") |
689 | 689 | return comment |
690 | 690 |
|
| 691 | + def _get_comment(self, header: str) -> t.Optional[IssueComment]: |
| 692 | + return seq_get( |
| 693 | + [comment for comment in self._issue.get_comments() if header in comment.body], |
| 694 | + 0, |
| 695 | + ) |
| 696 | + |
691 | 697 | def _get_merge_state_status(self) -> MergeStateStatus: |
692 | 698 | """ |
693 | 699 | This feature is currently in preview and therefore not available in the python module. |
@@ -741,13 +747,59 @@ def update_sqlmesh_comment_info( |
741 | 747 | comment.edit(body=body) |
742 | 748 | return True, comment |
743 | 749 |
|
| 750 | + def update_pr_environment_plan_comment(self, plan: Plan) -> None: |
| 751 | + self._update_tobiko_cloud_plan_comment( |
| 752 | + plan=plan, |
| 753 | + environment=self.pr_environment_name, |
| 754 | + marker=PR_ENVIRONMENT_PLAN_LINK_COMMENT_MARKER, |
| 755 | + description="SQLMesh PR environment plan progress.", |
| 756 | + link_label=f"Tobiko Cloud / environments / {self.pr_environment_name} / plans / {plan.plan_id}", |
| 757 | + ) |
| 758 | + |
| 759 | + def update_prod_plan_comment(self, plan: Plan) -> None: |
| 760 | + self._update_tobiko_cloud_plan_comment( |
| 761 | + plan=plan, |
| 762 | + environment=c.PROD, |
| 763 | + marker=PROD_PLAN_LINK_COMMENT_MARKER, |
| 764 | + description="SQLMesh production deploy plan.", |
| 765 | + link_label=f"Tobiko Cloud / environments / {c.PROD} / plans / {plan.plan_id}", |
| 766 | + ) |
| 767 | + |
| 768 | + def _update_tobiko_cloud_plan_comment( |
| 769 | + self, |
| 770 | + *, |
| 771 | + plan: Plan, |
| 772 | + environment: str, |
| 773 | + marker: str, |
| 774 | + description: str, |
| 775 | + link_label: str, |
| 776 | + ) -> None: |
| 777 | + cloud_url = os.environ.get("TCLOUD_URL") or os.environ.get("TCLOUD_BASE_URL") |
| 778 | + if not cloud_url: |
| 779 | + return |
| 780 | + if ( |
| 781 | + not plan.context_diff.has_changes |
| 782 | + and not plan.requires_backfill |
| 783 | + and not plan.has_unmodified_unpromoted |
| 784 | + ): |
| 785 | + if comment := self._get_comment(header=marker): |
| 786 | + comment.edit(body=f"{marker}\n\nNo current SQLMesh plan.") |
| 787 | + return |
| 788 | + |
| 789 | + plan_url = f"{cloud_url.rstrip('/')}/environments/{environment}/plans/{plan.plan_id}" |
| 790 | + body = f"{marker}\n\n{description}\n\nPlan: [{link_label}]({plan_url})" |
| 791 | + comment = self._get_or_create_comment(header=marker) |
| 792 | + comment.edit(body=body) |
| 793 | + |
744 | 794 | def update_pr_environment(self) -> None: |
745 | 795 | """ |
746 | 796 | Creates a PR environment from the logic present in the PR. If the PR contains changes that are |
747 | 797 | uncategorized, then an error will be raised. |
748 | 798 | """ |
749 | 799 | self._console.consume_captured_output() # clear output buffer |
750 | | - self._context.apply(self.pr_plan) # will raise if PR environment creation fails |
| 800 | + plan = self.pr_plan |
| 801 | + self._context.apply(plan) # will raise if PR environment creation fails |
| 802 | + self.update_pr_environment_plan_comment(plan) |
751 | 803 |
|
752 | 804 | # update PR info comment |
753 | 805 | vde_title = "- :eyes: To **review** this PR's changes, use virtual data environment:" |
@@ -800,7 +852,9 @@ def deploy_to_prod(self) -> None: |
800 | 852 | value=plan_summary, |
801 | 853 | dedup_regex=None, |
802 | 854 | ) |
803 | | - self._context.apply(self.prod_plan) |
| 855 | + plan = self.prod_plan |
| 856 | + self.update_prod_plan_comment(plan) |
| 857 | + self._context.apply(plan) |
804 | 858 |
|
805 | 859 | def try_invalidate_pr_environment(self) -> None: |
806 | 860 | """ |
|
0 commit comments