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(review_hog): handle flag-gated 403s once for the code review scene (#116633)

posthog[bot]5751deea7ec5
diff --git a/products/review_hog/frontend/CodeReviewScene.tsx b/products/review_hog/frontend/CodeReviewScene.tsx
index 378a748916d2..b66d3d740b3d 100644
--- a/products/review_hog/frontend/CodeReviewScene.tsx
+++ b/products/review_hog/frontend/CodeReviewScene.tsx
@@ -923,20 +923,37 @@ function SettingsTab(): JSX.Element {
*/
export function CodeReviewScene(): JSX.Element {
const { featureFlags } = useValues(featureFlagLogic)
- const { activeTab, initialLoadFailed } = useValues(reviewHogSettingsLogic)
+ const { activeTab, initialLoadFailed, featureFlagGated } = useValues(reviewHogSettingsLogic)
const { setActiveTab, loadAll } = useActions(reviewHogSettingsLogic)
if (!featureFlags[FEATURE_FLAGS.REVIEW_HOG]) {
return <NotFound object="page" />
}
+ const titleSection = (
+ <SceneTitleSection
+ name="Code review"
+ description="PostHog Review reads your pull requests and posts the findings worth fixing. Start a review, see recent ones, and choose what gets reviewed."
+ resourceType={{ type: 'code_review' }}
+ />
+ )
+
+ // The browser flag shows the page, but the server flag gate refuses its API.
+ if (featureFlagGated) {
+ return (
+ <SceneContent>
+ {titleSection}
+ <LemonBanner type="info" data-attr="code-review-feature-flag-gated">
+ PostHog Review is not available for this project yet. If you just turned on the feature preview,
+ refresh the page in a moment. Otherwise, ask an admin of your PostHog instance to turn it on.
+ </LemonBanner>
+ </SceneContent>
+ )
+ }
+
return (
<SceneContent>
- <SceneTitleSection
- name="Code review"
- description="PostHog Review reads your pull requests and posts the findings worth fixing. Start a review, see recent ones, and choose what gets reviewed."
- resourceType={{ type: 'code_review' }}
- />
+ {titleSection}
<LemonTabs<CodeReviewTab>
activeKey={activeTab}
onChange={setActiveTab}
diff --git a/products/review_hog/frontend/repositories/reviewHogProjectSettingsLogic.ts b/products/review_hog/frontend/repositories/reviewHogProjectSettingsLogic.ts
index d76235396da4..f602a4e0888e 100644
--- a/products/review_hog/frontend/repositories/reviewHogProjectSettingsLogic.ts
+++ b/products/review_hog/frontend/repositories/reviewHogProjectSettingsLogic.ts
@@ -26,7 +26,10 @@ import type {
ReviewRepositoryPersonKindEnumApi,
} from 'products/review_hog/frontend/generated/api.schemas'
import { ReviewHogRepositoryOverviewRetrieveView } from 'products/review_hog/frontend/generated/api.schemas'
-import { reviewHogSettingsLogic } from 'products/review_hog/frontend/reviewHogSettingsLogic'
+import {
+ requestUnlessFeatureFlagGated,
+ reviewHogSettingsLogic,
+} from 'products/review_hog/frontend/reviewHogSettingsLogic'
export function currentProjectId(): string {
return String(teamLogic.values.currentTeamId)
@@ -62,6 +65,7 @@ export type ClaimScopeChoice = ReviewInstallationClaimScopeEnumApi | 'none'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface reviewHogProjectSettingsLogicValues {
+ featureFlagGated: boolean // reviewHogSettingsLogic
canEdit: boolean
claimSaving: boolean
editDisabledReason: string | undefined
@@ -74,6 +78,9 @@ export interface reviewHogProjectSettingsLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface reviewHogProjectSettingsLogicActions {
loadSettings: () => any // reviewHogSettingsLogic
+ markFeatureFlagGated: () => {
+ value: true
+ } // reviewHogSettingsLogic
addProjectPerson: ({ userId, kind }: { kind: ReviewRepositoryPersonKindEnumApi; userId: number }) => {
userId: number
kind: ReviewRepositoryPersonKindEnumApi
@@ -114,10 +121,10 @@ export interface reviewHogProjectSettingsLogicActions {
errorObject?: any
}
loadProjectSettingsSuccess: (
- projectSettings: ReviewProjectSettingsApi,
+ projectSettings: ReviewProjectSettingsApi | null,
payload?: any
) => {
- projectSettings: ReviewProjectSettingsApi
+ projectSettings: ReviewProjectSettingsApi | null
payload?: any
}
removeProjectPerson: (person: ReviewRepositoryPersonApi) => ReviewRepositoryPersonApi
@@ -190,7 +197,8 @@ export const reviewHogProjectSettingsLogic = kea<reviewHogProjectSettingsLogicTy
path(['products', 'review_hog', 'frontend', 'repositories', 'reviewHogProjectSettingsLogic']),
connect(() => ({
- actions: [reviewHogSettingsLogic, ['loadSettings']],
+ values: [reviewHogSettingsLogic, ['featureFlagGated']],
+ actions: [reviewHogSettingsLogic, ['loadSettings', 'markFeatureFlagGated']],
})),
actions({
@@ -200,11 +208,16 @@ export const reviewHogProjectSettingsLogic = kea<reviewHogProjectSettingsLogicTy
setClaimScopeFinished: true,
}),
- loaders(() => ({
+ loaders(({ values, actions }) => ({
projectSettings: [
null as ReviewProjectSettingsApi | null,
{
- loadProjectSettings: async () => await reviewHogProjectSettingsRetrieve(currentProjectId()),
+ loadProjectSettings: async () =>
+ await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () => reviewHogProjectSettingsRetrieve(currentProjectId()),
+ values.projectSettings
+ ),
updateProjectSettings: async (patch: PatchedReviewProjectSettingsApi) =>
await reviewHogProjectSettingsPartialUpdate(currentProjectId(), patch),
addProjectPerson: async ({
diff --git a/products/review_hog/frontend/repositories/reviewHogRepositoriesLogic.ts b/products/review_hog/frontend/repositories/reviewHogRepositoriesLogic.ts
index 00656f3a0d1b..54558b729c04 100644
--- a/products/review_hog/frontend/repositories/reviewHogRepositoriesLogic.ts
+++ b/products/review_hog/frontend/repositories/reviewHogRepositoriesLogic.ts
@@ -73,11 +73,11 @@ export interface reviewHogRepositoriesLogicActions {
} // reviewHogProjectSettingsLogic
loadProjectSettings: () => any // reviewHogProjectSettingsLogic
loadProjectSettingsSuccess: (
- projectSettings: ReviewProjectSettingsApi,
+ projectSettings: ReviewProjectSettingsApi | null,
payload?: any
) => {
payload?: any
- projectSettings: ReviewProjectSettingsApi
+ projectSettings: ReviewProjectSettingsApi | null
} // reviewHogProjectSettingsLogic
removeProjectPersonSuccess: (
projectSettings: ReviewProjectSettingsApi,
diff --git a/products/review_hog/frontend/reviewHogSettingsLogic.test.ts b/products/review_hog/frontend/reviewHogSettingsLogic.test.ts
index 554fbc0d0ab1..3540c3f56717 100644
--- a/products/review_hog/frontend/reviewHogSettingsLogic.test.ts
+++ b/products/review_hog/frontend/reviewHogSettingsLogic.test.ts
@@ -615,6 +615,43 @@ describe('reviewHogSettingsLogic', () => {
expect(logic.values).toMatchObject({ reviewsPage: null, reviewsFailed: true, initialLoadFailed: false })
})
+ it('turns a feature flag 403 into one page state and stops sending requests', async () => {
+ const requestedPaths: string[] = []
+ const gated = ({ request }: { request: Request }): [number, Record<string, string>] => {
+ requestedPaths.push(new URL(request.url).pathname)
+ return [403, { type: 'authentication_error', code: 'feature_flag_required', detail: 'Not enabled.' }]
+ }
+ useMocks({
+ get: {
+ '/api/projects/:team_id/review_hog/reviews/table/': gated,
+ '/api/projects/:team_id/review_hog/reviews/perspective_stats/': gated,
+ '/api/projects/:team_id/review_hog/settings/': gated,
+ '/api/projects/:team_id/review_hog/perspectives/': gated,
+ '/api/projects/:team_id/review_hog/blind_spots/': gated,
+ '/api/projects/:team_id/review_hog/validators/': gated,
+ '/api/projects/:team_id/review_hog/resolution/': gated,
+ },
+ })
+ logic.mount()
+
+ await expectLogic(logic)
+ .toDispatchActions(['markFeatureFlagGated'])
+ .toFinishAllListeners()
+ .toNotHaveDispatchedActions([
+ 'loadSettingsFailure',
+ 'loadReviewsFailure',
+ 'loadPerspectiveStatsFailure',
+ 'loadDeepSkillStatsFailure',
+ ])
+ expect(logic.values).toMatchObject({ featureFlagGated: true, initialLoadFailed: false, reviewsFailed: false })
+
+ const requestCount = requestedPaths.length
+ await expectLogic(logic, () => {
+ document.dispatchEvent(new Event('visibilitychange'))
+ }).toDispatchActions(['loadReviewsSuccess'])
+ expect(requestedPaths).toHaveLength(requestCount)
+ })
+
it('checks the list immediately when the tab becomes visible again', async () => {
// The poll interval is paused while hidden and resumes with a full interval still to wait,
// which reads as stale exactly when the user comes back to look.
diff --git a/products/review_hog/frontend/reviewHogSettingsLogic.ts b/products/review_hog/frontend/reviewHogSettingsLogic.ts
index a02a2eb3e47f..14cde40d6306 100644
--- a/products/review_hog/frontend/reviewHogSettingsLogic.ts
+++ b/products/review_hog/frontend/reviewHogSettingsLogic.ts
@@ -5,6 +5,7 @@ import { actionToUrl, router, urlToAction } from 'kea-router'
import { lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
+import { isFeatureFlagGatedError } from 'lib/api-error'
import type { SkillPickerGroup } from 'lib/components/SkillPicker/SkillPicker'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
@@ -87,6 +88,31 @@ interface ReviewRunMarker {
// it must not keep the list polling forever.
const TRIGGERED_REVIEW_WATCH_TIMEOUT_MS = 2 * 60 * 1000
+/**
+ * Runs a review-hog request, but treats a feature flag 403 as a page state, not a failure. The
+ * frontend can show the scene while the backend flag gate still refuses its API, for example on a
+ * self-hosted instance whose flags come from the persisted baseline. Each loader would otherwise
+ * report the same 403 as its own issue. After the first gated response, no loader sends a request.
+ */
+export async function requestUnlessFeatureFlagGated<T>(
+ logic: { values: { featureFlagGated: boolean }; actions: { markFeatureFlagGated: () => void } },
+ request: () => Promise<T>,
+ fallback: T
+): Promise<T> {
+ if (logic.values.featureFlagGated) {
+ return fallback
+ }
+ try {
+ return await request()
+ } catch (error) {
+ if (!isFeatureFlagGatedError(error)) {
+ throw error
+ }
+ logic.actions.markFeatureFlagGated()
+ return fallback
+ }
+}
+
export const REVIEWS_PAGE_SIZE = 25
// The reviews table API caps `offset` at 1,000,000.
const MAX_REVIEWS_PAGE = Math.floor(1_000_000 / REVIEWS_PAGE_SIZE) + 1
@@ -250,6 +276,7 @@ export interface reviewHogSettingsLogicValues {
judged: number
kept: number
} | null
+ featureFlagGated: boolean
hasUserChosenReviewsScope: boolean
initialLoadFailed: boolean
openedReview: ReviewRecentReviewApi | null
@@ -347,10 +374,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadBlindSpotsSuccess: (
- blindSpots: ReviewBlindSpotsConfigApi[],
+ blindSpots: ReviewBlindSpotsConfigApi[] | null,
payload?: any
) => {
- blindSpots: ReviewBlindSpotsConfigApi[]
+ blindSpots: ReviewBlindSpotsConfigApi[] | null
payload?: any
}
loadDeepSkillStats: () => any
@@ -362,10 +389,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadDeepSkillStatsSuccess: (
- deepSkillStats: ReviewPerspectiveStatsApi,
+ deepSkillStats: ReviewPerspectiveStatsApi | null,
payload?: any
) => {
- deepSkillStats: ReviewPerspectiveStatsApi
+ deepSkillStats: ReviewPerspectiveStatsApi | null
payload?: any
}
loadPerspectiveStats: (_?: any) => any
@@ -377,10 +404,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadPerspectiveStatsSuccess: (
- perspectiveStats: ReviewPerspectiveStatsApi,
+ perspectiveStats: ReviewPerspectiveStatsApi | null,
payload?: any
) => {
- perspectiveStats: ReviewPerspectiveStatsApi
+ perspectiveStats: ReviewPerspectiveStatsApi | null
payload?: any
}
loadPerspectives: () => any
@@ -392,10 +419,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadPerspectivesSuccess: (
- perspectives: ReviewPerspectiveConfigApi[],
+ perspectives: ReviewPerspectiveConfigApi[] | null,
payload?: any
) => {
- perspectives: ReviewPerspectiveConfigApi[]
+ perspectives: ReviewPerspectiveConfigApi[] | null
payload?: any
}
loadResolutionSkills: () => any
@@ -407,10 +434,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadResolutionSkillsSuccess: (
- resolutionSkills: ReviewResolutionConfigApi[],
+ resolutionSkills: ReviewResolutionConfigApi[] | null,
payload?: any
) => {
- resolutionSkills: ReviewResolutionConfigApi[]
+ resolutionSkills: ReviewResolutionConfigApi[] | null
payload?: any
}
loadReviewDetail: (reviewId: string) => string
@@ -422,10 +449,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadReviewDetailSuccess: (
- reviewDetail: ReviewDetailApi,
+ reviewDetail: ReviewDetailApi | null,
payload?: string
) => {
- reviewDetail: ReviewDetailApi
+ reviewDetail: ReviewDetailApi | null
payload?: string
}
loadReviews: (_?: any) => any
@@ -437,10 +464,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadReviewsSuccess: (
- reviewsPage: ReviewReviewsTablePageApi,
+ reviewsPage: ReviewReviewsTablePageApi | null,
payload?: any
) => {
- reviewsPage: ReviewReviewsTablePageApi
+ reviewsPage: ReviewReviewsTablePageApi | null
payload?: any
}
loadSettings: () => any
@@ -452,10 +479,10 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadSettingsSuccess: (
- settings: ReviewUserSettingsApi,
+ settings: ReviewUserSettingsApi | null,
payload?: any
) => {
- settings: ReviewUserSettingsApi
+ settings: ReviewUserSettingsApi | null
payload?: any
}
loadValidators: () => any
@@ -467,12 +494,15 @@ export interface reviewHogSettingsLogicActions {
errorObject?: any
}
loadValidatorsSuccess: (
- validators: ReviewValidatorConfigApi[],
+ validators: ReviewValidatorConfigApi[] | null,
payload?: any
) => {
- validators: ReviewValidatorConfigApi[]
+ validators: ReviewValidatorConfigApi[] | null
payload?: any
}
+ markFeatureFlagGated: () => {
+ value: true
+ }
markInitialLoadFailed: () => {
value: true
}
@@ -723,13 +753,19 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
// Flags a load failure as the page-level one. Dispatched by the failure listeners only when
// the failing surface has nothing loaded yet, so a background poll blip stays silent.
markInitialLoadFailed: true,
+ markFeatureFlagGated: true,
}),
- loaders(({ values }) => ({
+ loaders(({ values, actions }) => ({
settings: [
null as ReviewUserSettingsApi | null,
{
- loadSettings: async () => await reviewHogSettingsRetrieve(currentProjectId()),
+ loadSettings: async () =>
+ await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () => reviewHogSettingsRetrieve(currentProjectId()),
+ values.settings
+ ),
updateSettings: async (patch: PatchedReviewUserSettingsApi, breakpoint) => {
const response = await reviewHogSettingsPartialUpdate(currentProjectId(), patch)
// A newer update is already in flight — drop this now-stale response.
@@ -741,25 +777,45 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
perspectives: [
null as ReviewPerspectiveConfigApi[] | null,
{
- loadPerspectives: async () => await reviewHogPerspectivesList(currentProjectId()),
+ loadPerspectives: async () =>
+ await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () => reviewHogPerspectivesList(currentProjectId()),
+ values.perspectives
+ ),
},
],
blindSpots: [
null as ReviewBlindSpotsConfigApi[] | null,
{
- loadBlindSpots: async () => await reviewHogBlindSpotsList(currentProjectId()),
+ loadBlindSpots: async () =>
+ await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () => reviewHogBlindSpotsList(currentProjectId()),
+ values.blindSpots
+ ),
},
],
validators: [
null as ReviewValidatorConfigApi[] | null,
{
- loadValidators: async () => await reviewHogValidatorsList(currentProjectId()),
+ loadValidators: async () =>
+ await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () => reviewHogValidatorsList(currentProjectId()),
+ values.validators
+ ),
},
],
resolutionSkills: [
null as ReviewResolutionConfigApi[] | null,
{
- loadResolutionSkills: async () => await reviewHogResolutionList(currentProjectId()),
+ loadResolutionSkills: async () =>
+ await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () => reviewHogResolutionList(currentProjectId()),
+ values.resolutionSkills
+ ),
},
],
// Every live team skill, for the adopt picker. Fetched fresh on each modal open so a skill
@@ -795,15 +851,20 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
loadReviews: async (_ = null, breakpoint) => {
// urlToAction replays each deep-link filter in turn; debounce so only the settled set loads.
await breakpoint(1)
- const response = await reviewHogReviewsTableRetrieve(currentProjectId(), {
- scope: values.reviewsScope,
- limit: REVIEWS_PAGE_SIZE,
- offset: (values.reviewsCurrentPage - 1) * REVIEWS_PAGE_SIZE,
- ...(values.reviewsRepository ? { repository: values.reviewsRepository } : {}),
- ...(values.reviewsMode ? { review_mode: values.reviewsMode } : {}),
- ...(values.reviewsStatus ? { status: values.reviewsStatus } : {}),
- ...(values.reviewsPublished !== null ? { published: values.reviewsPublished } : {}),
- })
+ const response = await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () =>
+ reviewHogReviewsTableRetrieve(currentProjectId(), {
+ scope: values.reviewsScope,
+ limit: REVIEWS_PAGE_SIZE,
+ offset: (values.reviewsCurrentPage - 1) * REVIEWS_PAGE_SIZE,
+ ...(values.reviewsRepository ? { repository: values.reviewsRepository } : {}),
+ ...(values.reviewsMode ? { review_mode: values.reviewsMode } : {}),
+ ...(values.reviewsStatus ? { status: values.reviewsStatus } : {}),
+ ...(values.reviewsPublished !== null ? { published: values.reviewsPublished } : {}),
+ }),
+ values.reviewsPage
+ )
// A filter or page change mid-flight dispatched a newer load — drop this stale response.
breakpoint()
return response
@@ -814,7 +875,11 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
null as ReviewDetailApi | null,
{
loadReviewDetail: async (reviewId: string, breakpoint) => {
- const response = await reviewHogReviewsRetrieve(currentProjectId(), reviewId)
+ const response = await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () => reviewHogReviewsRetrieve(currentProjectId(), reviewId),
+ values.reviewDetail
+ )
// A newer open (row click, ?review= navigation) dispatched a fresh load
// mid-flight — drop this stale response so out-of-order responses can't
// attach the wrong review's findings to the drawer.
@@ -828,9 +893,14 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
{
// The default param keeps the action zero-arg in the generated logic type.
loadPerspectiveStats: async (_ = null, breakpoint) => {
- const response = await reviewHogReviewsPerspectiveStatsRetrieve(currentProjectId(), {
- scope: values.reviewsScope,
- })
+ const response = await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () =>
+ reviewHogReviewsPerspectiveStatsRetrieve(currentProjectId(), {
+ scope: values.reviewsScope,
+ }),
+ values.perspectiveStats
+ )
// A scope change mid-flight dispatched a newer load — drop this stale response.
breakpoint()
return response
@@ -844,9 +914,14 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
null as ReviewPerspectiveStatsApi | null,
{
loadDeepSkillStats: async () =>
- await reviewHogReviewsPerspectiveStatsRetrieve(currentProjectId(), {
- scope: ReviewHogReviewsPerspectiveStatsRetrieveScope.OwnDeep,
- }),
+ await requestUnlessFeatureFlagGated(
+ { values, actions },
+ () =>
+ reviewHogReviewsPerspectiveStatsRetrieve(currentProjectId(), {
+ scope: ReviewHogReviewsPerspectiveStatsRetrieveScope.OwnDeep,
+ }),
+ values.deepSkillStats
+ ),
},
],
})),
@@ -1008,6 +1083,9 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
[] as string[],
{
loadReviewsSuccess: (state, { reviewsPage }) => {
+ if (!reviewsPage) {
+ return state
+ }
const merged = Array.from(
new Set([...state, ...reviewsPage.results.map((review) => review.repository)])
).sort()
@@ -1052,6 +1130,12 @@ export const reviewHogSettingsLogic = kea<reviewHogSettingsLogicType>([
markInitialLoadFailed: () => true,
},
],
+ featureFlagGated: [
+ false,
+ {
+ markFeatureFlagGated: () => true,
+ },
+ ],
creatingSkillKind: [
// Guards the "Create your own …" buttons against double-submission while the task spins up.
null as ReviewSkillKind | null,