Files
supabase/apps/studio/hooks/misc/useGitHubConfigDrift.ts
Ivan Vasilov e10f7cc808 feat: Add a config drift page in Studio (#48906)
## New Features
- Initial work for showing configuration drift in Studio
- This only works for Github-connected projects and it'll show a banner
if the project state differs from the git-tracked `config.toml`
- Currently behind a feature-flag `ConfigDrift`, enabled on local and
staging.
- There might be drift shown without changing any setting, this is
work-in-progress.

<img width="1217" height="1195" alt="Screenshot 2026-08-19 at 23 12 10"
src="https://github.com/user-attachments/assets/fb0b18d8-1a93-4595-85cc-e8b8a3462847"
/>

## How to test
1. Connect a project to a Github repo
2. Resync the branch on `/dashboard/project/_/branches`. This will
trigger deployment of the `config.toml` on your project
3. Change some settings (I recommend
`dashboard/project/_/auth/providers`
4. A banner should appear on all project pages with a link 


## Tests
- Added coverage for configuration conversion, normalization, matching,
drift detection, and unmanaged settings.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-25 13:56:40 +02:00

94 lines
3.8 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useParams } from 'common'
import { useCallback, useMemo } from 'react'
import { convertProjectConfigToGitHubConfig } from '@/components/interfaces/ConfigDrift/github-config-convert'
import { getConfigDriftSummary } from '@/components/interfaces/ConfigDrift/github-config-drift'
import type { Branch } from '@/data/branches/branches-query'
import { useBranchesQuery } from '@/data/branches/branches-query'
import { useGitHubConfigQuery } from '@/data/config/github-config-query'
import { projectConfigV2QueryOptions } from '@/data/config/project-config-query'
import { useProjectGitHubConnectionQuery } from '@/data/integrations/github-connections-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { IS_PLATFORM } from '@/lib/constants'
export function getGitBranchName(branch?: Branch): string | undefined {
return branch?.git_branch?.trim() || (branch?.is_default ? undefined : branch?.name?.trim())
}
export function useSelectedGitHubConfigDrift() {
const { ref: projectRef } = useParams()
const projectQuery = useSelectedProjectQuery()
const project = projectQuery.data
const parentProjectRef = project?.parentRef ?? projectRef
const shouldLoad = IS_PLATFORM && Boolean(projectRef) && Boolean(project)
const branchesQuery = useBranchesQuery({ projectRef: parentProjectRef }, { enabled: shouldLoad })
const connectionQuery = useProjectGitHubConnectionQuery({ ref: parentProjectRef })
const connection = connectionQuery.data
const hasConnection = connection !== undefined
const branches = branchesQuery.data ?? []
const selectedBranch = branches.find((branch) => branch.project_ref === projectRef)
const gitBranch = getGitBranchName(selectedBranch)
const queriesEnabled =
shouldLoad && branchesQuery.isSuccess && connectionQuery.isSuccess && hasConnection
const projectConfigQuery = useQuery({
...projectConfigV2QueryOptions({ projectRef }),
enabled: queriesEnabled,
staleTime: 30_000,
})
const githubConfigQuery = useGitHubConfigQuery(
{ connectionId: connection?.id, branch: gitBranch },
{ enabled: queriesEnabled }
)
const { refetch: branchesRefetch } = branchesQuery
const { refetch: connectionRefetch } = connectionQuery
const { refetch: projectConfigRefetch } = projectConfigQuery
const { refetch: githubConfigRefetch } = githubConfigQuery
const refetch = useCallback(
() =>
Promise.all([
branchesRefetch(),
connectionRefetch(),
projectConfigRefetch(),
githubConfigRefetch(),
]),
[branchesRefetch, connectionRefetch, projectConfigRefetch, githubConfigRefetch]
)
const summary = useMemo(() => {
const dashboardConfig = convertProjectConfigToGitHubConfig(projectConfigQuery.data?.attributes)
return getConfigDriftSummary({
dashboardConfig: dashboardConfig,
githubConfig: githubConfigQuery.data?.config,
})
}, [projectConfigQuery.data?.attributes, githubConfigQuery.data?.config])
const activeQueries = [
projectQuery,
...(shouldLoad ? [branchesQuery, connectionQuery] : []),
...(shouldLoad && hasConnection ? [projectConfigQuery, githubConfigQuery] : []),
]
const isReady =
shouldLoad && hasConnection && projectConfigQuery.isSuccess && githubConfigQuery.isSuccess
const issueCount = summary.driftedFields.length
return {
requestedGitBranch: gitBranch,
isReady,
isPending: activeQueries.some((query) => query.isPending),
isFetching: activeQueries.some((query) => query.isFetching),
isError: activeQueries.some((query) => query.isError),
error: activeQueries.find((query) => query.error)?.error,
hasConfigurationIssues: isReady && issueCount > 0,
unmanagedFields: summary.unmanagedFields,
summary,
refetch,
}
}