diff --git a/python/envctl_engine/pr_preview_controller.py b/python/envctl_engine/pr_preview_controller.py index cc53301b..d306463c 100644 --- a/python/envctl_engine/pr_preview_controller.py +++ b/python/envctl_engine/pr_preview_controller.py @@ -1942,6 +1942,11 @@ def start( self.remove_label(pr.number) return 0 + self.comment( + pr.number, + self.render_starting_comment(pr, reason), + ) + external_dependencies = self.acquire_external_dependency_leases( pr, existing_state, @@ -2819,6 +2824,18 @@ def expire_overdue_previews( if now < expires_at: continue expired.add(pr.number) + if ( + state + and state.status == "stopped" + and state.label_added_at == isoformat(active_since) + ): + print( + f"Preview PR #{pr.number} already stopped for this expired " + "label window; removing the stale label without another comment.", + flush=True, + ) + self.remove_label(pr.number) + continue self.comment( pr.number, self.render_ttl_comment(pr, active_since, expires_at), @@ -3466,6 +3483,31 @@ def render_started_comment( f"{endpoint_lines}" ) + def render_starting_comment( + self, + pr: PullRequestInfo, + reason: str, + ) -> str: + server_url = os.environ.get("GITHUB_SERVER_URL", "").rstrip("/") + repository = os.environ.get("GITHUB_REPOSITORY", "").strip("/") + run_id = os.environ.get("GITHUB_RUN_ID", "").strip() + run_url = ( + f"{server_url}/{repository}/actions/runs/{run_id}" + if server_url and repository and run_id + else "" + ) + run_line = f"\n- Workflow run: {run_url}" if run_url else "" + return ( + f"{COMMENT_MARKER}\n" + f"Envctl preview request received for PR #{pr.number}. Provisioning has started; " + "this can take several minutes. I will post the preview links or the exact failure " + "reason when it finishes.\n\n" + f"- Reason: {reason}\n" + f"- Branch: `{pr.head_ref}`\n" + f"- Head: `{pr.head_sha[:12]}`" + f"{run_line}" + ) + def render_external_dependency_mode(self, state: PreviewState) -> str: if not self.config.external_dependency_pools: return "`isolated` forced by workflow" diff --git a/tests/python/runtime/test_pr_preview_controller.py b/tests/python/runtime/test_pr_preview_controller.py index ec97e547..71d8a888 100644 --- a/tests/python/runtime/test_pr_preview_controller.py +++ b/tests/python/runtime/test_pr_preview_controller.py @@ -1461,6 +1461,33 @@ def test_started_comment_omits_qa_credentials_when_qa_user_disabled(tmp_path): assert "Pele-QA-2026!" not in comment +def test_labeled_event_comments_immediately_before_provisioning(tmp_path): + controller = load_controller() + root = tmp_path / "control" / "trees" / "imported" / "feature-demo" + root.mkdir(parents=True) + runner = FakeRunner( + controller, + projects={"projects": [{"name": "feature/demo", "root": str(root), "running": True}]}, + endpoints={"frontend": {"port": 9000}, "backend": {"port": 8000}}, + root_to_branch={str(root): "feature/demo"}, + ) + instance = controller.PreviewController(make_config(controller, tmp_path), runner) + + exit_code = instance.run_pull_request_event( + pr_payload( + action="labeled", + labels=["deploy-app"], + event_label="deploy-app", + ) + ) + + assert exit_code == 0 + assert "preview request received" in runner.comments[0] + assert "this can take several minutes" in runner.comments[0] + assert "- Reason: label added" in runner.comments[0] + assert "Envctl preview is running" in runner.comments[-1] + + def test_labeled_event_leases_external_supabase_without_storing_tokens(tmp_path): controller = load_controller() root = tmp_path / "control" / "trees" / "imported" / "feature-demo" @@ -3036,6 +3063,40 @@ def test_sweep_expires_label_stops_preview_and_removes_label(tmp_path): assert "TTL expired" in runner.comments[0] +def test_sweep_does_not_repeat_ttl_comment_for_already_stopped_label_window(tmp_path): + controller = load_controller() + config = make_config(controller, tmp_path, ttl_minutes=45) + runner = FakeRunner( + controller, + active_prs=[pr_list_payload(789, "Preview me")], + timeline="labeled\t2000-01-01T00:00:00Z\tdeploy-app\n", + ) + instance = controller.PreviewController(config, runner) + instance.save_state( + controller.PreviewState( + pr_number=789, + label="deploy-app", + project="feature/demo", + root=str(tmp_path / "control" / "trees" / "imported" / "feature-demo"), + head_ref="feature/demo", + head_sha="abc123456789", + status="stopped", + label_added_at="2000-01-01T00:00:00Z", + started_at="2000-01-01T00:00:00Z", + expires_at="2000-01-01T00:45:00Z", + updated_at="2000-01-01T00:45:00Z", + endpoints={}, + ) + ) + + exit_code = instance.sweep() + + assert exit_code == 0 + assert runner.comments == [] + assert runner.removed_labels == ["deploy-app"] + assert command_argvs(runner, "envctl", "stop") == [] + + def test_sweep_redeploys_running_preview_when_head_sha_changes(tmp_path): controller = load_controller() root = tmp_path / "control" / "trees" / "imported" / "feature-demo"