Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(signals): only comment on and close implementation PR when it's still open (#69473)

Rafael Audibert9d10a379c7be
diff --git a/products/signals/backend/implementation_pr.py b/products/signals/backend/implementation_pr.py
index f141896786b7..d5c0f0e2340f 100644
--- a/products/signals/backend/implementation_pr.py
+++ b/products/signals/backend/implementation_pr.py
@@ -69,7 +69,9 @@ def close_implementation_pr_for_report(
) -> bool:
"""Best-effort: comment on and close the GitHub PR opened for this report's implementation task.
- Called when a report is suppressed or snoozed — the open PR shouldn't linger. Leaves an
+ Called when a report is suppressed or snoozed — the open PR shouldn't linger. Only acts on a PR
+ that is still open: an already-closed or merged PR is left untouched (no comment, no close), so
+ we never leave a confusing "closing this PR" note on a PR that shipped months ago. Leaves an
explanatory comment, then closes the PR. Returns True when the PR was closed, False when there
was nothing to close or the close couldn't be completed. Never raises: the state transition
must succeed regardless.
@@ -91,6 +93,30 @@ def close_implementation_pr_for_report(
logger.info("close_implementation_pr_no_integration", report_id=str(report_id), repository=repository)
return False
+ # Only comment on and close a PR that's still open. A merged PR reports state "closed" with
+ # merged=True, and an already-closed PR reports state "closed" — in either case there's
+ # nothing to close, and leaving a comment would just be noise. If the state can't be
+ # confirmed, skip rather than risk commenting on a PR that already shipped.
+ pr_status = github.get_pull_request(repository, pr_number)
+ if not pr_status.get("success"):
+ logger.warning(
+ "close_implementation_pr_status_fetch_failed",
+ report_id=str(report_id),
+ pr_url=pr_url,
+ error=pr_status.get("error"),
+ status_code=pr_status.get("status_code"),
+ )
+ return False
+ if pr_status.get("state") != "open" or pr_status.get("merged"):
+ logger.info(
+ "close_implementation_pr_not_open",
+ report_id=str(report_id),
+ pr_url=pr_url,
+ state=pr_status.get("state"),
+ merged=pr_status.get("merged"),
+ )
+ return False
+
# Explain first, close second — a failed comment shouldn't stop the close.
comment_outcome = github.comment_on_pull_request(repository, pr_number, _PR_CLOSE_COMMENTS[reason])
if not comment_outcome.get("success"):
diff --git a/products/signals/backend/test/test_dismissal_pr_close.py b/products/signals/backend/test/test_dismissal_pr_close.py
index c7875b2de496..626d596a9572 100644
--- a/products/signals/backend/test/test_dismissal_pr_close.py
+++ b/products/signals/backend/test/test_dismissal_pr_close.py
@@ -116,6 +116,7 @@ class TestCloseImplementationPrForReport(BaseTest):
)
def test_comments_on_and_closes_linked_pr(self, _name: str, reason: PrCloseReason):
github = MagicMock()
+ github.get_pull_request.return_value = {"success": True, "state": "open", "merged": False}
github.comment_on_pull_request.return_value = {"success": True}
github.close_pull_request.return_value = {"success": True, "number": 123, "state": "closed"}
with (
@@ -159,3 +160,43 @@ class TestCloseImplementationPrForReport(BaseTest):
),
):
assert close_implementation_pr_for_report(self.team.id, "report-1") is False
+
+ @parameterized.expand(
+ [
+ ("already_closed", {"success": True, "state": "closed", "merged": False}),
+ ("already_merged", {"success": True, "state": "closed", "merged": True}),
+ ]
+ )
+ def test_skips_comment_and_close_when_pr_not_open(self, _name: str, pr_status: dict):
+ github = MagicMock()
+ github.get_pull_request.return_value = pr_status
+ with (
+ patch(
+ "products.signals.backend.implementation_pr.fetch_implementation_pr_urls_for_reports",
+ return_value={"report-1": _PR_URL},
+ ),
+ patch(
+ "products.signals.backend.implementation_pr.GitHubIntegration.first_for_team_repository",
+ return_value=github,
+ ),
+ ):
+ assert close_implementation_pr_for_report(self.team.id, "report-1") is False
+ github.comment_on_pull_request.assert_not_called()
+ github.close_pull_request.assert_not_called()
+
+ def test_skips_comment_and_close_when_status_unavailable(self):
+ github = MagicMock()
+ github.get_pull_request.return_value = {"success": False, "error": "boom", "status_code": 404}
+ with (
+ patch(
+ "products.signals.backend.implementation_pr.fetch_implementation_pr_urls_for_reports",
+ return_value={"report-1": _PR_URL},
+ ),
+ patch(
+ "products.signals.backend.implementation_pr.GitHubIntegration.first_for_team_repository",
+ return_value=github,
+ ),
+ ):
+ assert close_implementation_pr_for_report(self.team.id, "report-1") is False
+ github.comment_on_pull_request.assert_not_called()
+ github.close_pull_request.assert_not_called()