Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions python/envctl_engine/pr_preview_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
):
Comment on lines +2827 to +2831

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retry cleanup before dropping stale preview labels

When a PR still has the preview label but the saved state is already stopped, this branch now removes the label without calling stop() again. That loses the only retry path for best-effort cleanup that stop() performs but does not record failures for, such as remove_public_routes() and mark_deployment_inactive(); for example, after a manual/TTL stop saved stopped but Docker route removal or the GitHub deployment-status API failed transiently, the next sweep will drop the label and leave stale public routes or an active deployment instead of retrying cleanup.

Useful? React with 👍 / 👎.

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),
Expand Down Expand Up @@ -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"
Expand Down
61 changes: 61 additions & 0 deletions tests/python/runtime/test_pr_preview_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading