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.

feat(experiments): pick the cleanup default repository from a dropdown (#86192)

Juraj Majerikd5921e553fbb
diff --git a/frontend/src/scenes/experiments/ExperimentsSettings.tsx b/frontend/src/scenes/experiments/ExperimentsSettings.tsx
index 352cd3de71c4..91b0b41b3d11 100644
--- a/frontend/src/scenes/experiments/ExperimentsSettings.tsx
+++ b/frontend/src/scenes/experiments/ExperimentsSettings.tsx
@@ -135,7 +135,7 @@ export function ExperimentsSettings(): JSX.Element {
<SettingsSection title="Flag cleanup">
<SettingsItem
label="Default repository for flag cleanup PRs"
- description="Used when an experiment doesn't have its own repository. Must be connected to this project's GitHub integration."
+ description="Used when an experiment doesn't have its own repository."
>
<FlagCleanupRepository />
</SettingsItem>
diff --git a/frontend/src/scenes/settings/environment/FlagCleanupRepository.tsx b/frontend/src/scenes/settings/environment/FlagCleanupRepository.tsx
index 94112f6eec98..79782204ac6c 100644
--- a/frontend/src/scenes/settings/environment/FlagCleanupRepository.tsx
+++ b/frontend/src/scenes/settings/environment/FlagCleanupRepository.tsx
@@ -1,16 +1,39 @@
import { useActions, useValues } from 'kea'
-import { useEffect, useState } from 'react'
+import { useEffect } from 'react'
-import { LemonButton, LemonInput } from '@posthog/lemon-ui'
+import { LemonButton, LemonInputSelect } from '@posthog/lemon-ui'
import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'
import { TeamMembershipLevel } from 'lib/constants'
+import { githubIntegrationLogic } from 'lib/integrations/githubIntegrationLogic'
+import { integrationsLogic } from 'lib/integrations/integrationsLogic'
+
+import { IntegrationType } from '~/types'
import { experimentsConfigLogic } from './experimentsConfigLogic'
+// Must pick the same integration as the backend's resolve_team_github_integration (org accounts
+// first, then oldest; broken installs skipped), or the dropdown offers repositories the
+// server-side validation then rejects.
+function resolveCleanupIntegration(integrations: IntegrationType[]): IntegrationType | undefined {
+ return integrations
+ .filter(
+ (integration) =>
+ integration.errors !== 'TOKEN_REFRESH_FAILED' && !integration.config?.installation_unavailable_since
+ )
+ .sort(
+ (a, b) =>
+ // Missing account type sorts last, like Postgres NULLS LAST.
+ (a.config?.account?.type ?? '\uffff').localeCompare(b.config?.account?.type ?? '\uffff') ||
+ a.created_at.localeCompare(b.created_at) ||
+ a.id - b.id
+ )[0]
+}
+
export function FlagCleanupRepository(): JSX.Element {
const { experimentsConfig, experimentsConfigUpdating } = useValues(experimentsConfigLogic)
const { updateExperimentsConfig } = useActions(experimentsConfigLogic)
+ const { githubIntegrations, integrationsLoading } = useValues(integrationsLogic)
const restrictionReason = useRestrictedArea({
scope: RestrictionScope.Project,
@@ -18,45 +41,90 @@ export function FlagCleanupRepository(): JSX.Element {
})
const savedValue = experimentsConfig?.flag_cleanup_repository ?? null
- const [localValue, setLocalValue] = useState<string>(savedValue ?? '')
+ const integration = resolveCleanupIntegration(githubIntegrations)
- useEffect(() => {
- setLocalValue(savedValue ?? '')
- }, [savedValue])
+ if (!integration) {
+ if (integrationsLoading) {
+ return (
+ <div className="max-w-160">
+ <LemonInputSelect mode="single" options={[]} loading placeholder="Select a repository" />
+ </div>
+ )
+ }
+ return (
+ <div className="flex items-center gap-2 max-w-160">
+ <p className="mb-0 text-secondary">Connect GitHub in your project settings to choose a repository.</p>
+ {savedValue !== null && (
+ <LemonButton
+ type="secondary"
+ onClick={() => updateExperimentsConfig({ flag_cleanup_repository: null })}
+ loading={experimentsConfigUpdating}
+ disabledReason={restrictionReason}
+ >
+ Clear
+ </LemonButton>
+ )}
+ </div>
+ )
+ }
- const trimmed = localValue.trim()
- const unchanged = trimmed === (savedValue ?? '')
+ return (
+ <RepositoryPicker
+ integrationId={integration.id}
+ value={savedValue}
+ updating={experimentsConfigUpdating}
+ restrictionReason={restrictionReason}
+ onChange={(repository) => updateExperimentsConfig({ flag_cleanup_repository: repository })}
+ />
+ )
+}
- const save = (): void => {
- if (restrictionReason || unchanged || experimentsConfigUpdating) {
- return
- }
- updateExperimentsConfig({ flag_cleanup_repository: trimmed || null })
+// Separate component so githubIntegrationLogic only mounts once an integration id exists.
+function RepositoryPicker({
+ integrationId,
+ value,
+ updating,
+ restrictionReason,
+ onChange,
+}: {
+ integrationId: number
+ value: string | null
+ updating: boolean
+ restrictionReason: string | null
+ onChange: (repository: string | null) => void
+}): JSX.Element {
+ const logic = githubIntegrationLogic({ id: integrationId })
+ const { repositories, repositoriesLoading } = useValues(logic)
+ const { loadRepositories } = useActions(logic)
+
+ useEffect(() => {
+ loadRepositories()
+ }, [loadRepositories])
+
+ const options = repositories.map((repo) => ({ key: repo.full_name, label: repo.full_name }))
+ // A saved default can predate the current repository cache; keep it visible in the control.
+ if (value && !options.some((option) => option.key === value)) {
+ options.push({ key: value, label: value })
}
return (
<div className="flex items-center gap-2 max-w-160">
- <LemonInput
- value={localValue}
- onChange={setLocalValue}
- onPressEnter={save}
- placeholder="organization/repository"
- disabled={!!restrictionReason || experimentsConfigUpdating}
+ <LemonInputSelect
+ mode="single"
+ value={value ? [value] : []}
+ onChange={(selected) => selected[0] && selected[0] !== value && onChange(selected[0])}
+ options={options}
+ loading={repositoriesLoading || updating}
+ disabledReason={restrictionReason ?? undefined}
+ placeholder="Select a repository"
+ data-attr="experiment-flag-cleanup-default-repository"
className="flex-1"
/>
- <LemonButton
- type="primary"
- onClick={save}
- loading={experimentsConfigUpdating}
- disabledReason={restrictionReason || (unchanged ? 'No changes to save' : null)}
- >
- Save
- </LemonButton>
- {savedValue !== null && (
+ {value !== null && (
<LemonButton
type="secondary"
- onClick={() => updateExperimentsConfig({ flag_cleanup_repository: null })}
- loading={experimentsConfigUpdating}
+ onClick={() => onChange(null)}
+ loading={updating}
disabledReason={restrictionReason}
>
Clear