Files
supabase/apps/studio/hooks/misc/useGitHubConfigDrift.ts
Colum Ferry 037ee2afad fix(studio): name the actual config file on the code configuration page (#53518)
## Problem

The Code configuration page (`ConfigDrift`) hard-codes `config.toml` in
its copy. The GitHub connection config endpoint (`GET
/platform/integrations/github/connections/{id}/config`) now also reads
`supabase/config.json` and `supabase/config.jsonc` and returns the file
it read in `path`, so a project with a JSON config sees wording about a
file it doesn't have.

Linear: CLI-2954

## Solution

- `useSelectedGitHubConfigDrift` returns `configFileName`, the basename
of the response's `path`.
- The drift page uses it in the column header, drift banner, decode
error title, overwrite warning and the Matching / Not tracked sections.
Where no file is known (the no-connection empty state) it says "config
file".
- `formatGitHubConfigDecodeMessage` takes the file name, falling back to
"The config file".
- The `config-toml` error source is renamed to `config-file`.
- Small copy fix in the warning: "redeploys … and overwrites changes
made here".

TOML projects see the same text as before, since `path` resolves to
`config.toml`.

## Review instructions

1. Open the Code configuration page for a project connected to a repo
with `supabase/config.toml` and confirm the copy is unchanged.
2. Repeat for a repo with only `supabase/config.jsonc` and confirm the
page names `config.jsonc` throughout.
3. `pnpm --filter studio exec vitest run
components/interfaces/ConfigDrift` covers both cases, including a JSONC
decode error.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-20 14:20:08 +01:00

168 lines
6.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useParams } from 'common'
import { useCallback, useMemo } from 'react'
import {
formatGitHubConfigDecodeMessage,
fromDashboardProjectConfig,
getConfigDriftSummary,
type GitHubConfigDriftSummary,
} from '@/components/interfaces/ConfigDrift/github-config-drift'
import type { Branch } from '@/data/branches/branches-query'
import { branchesQueryOptions } 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 { usePermissionsQueryV2 } from '@/data/permissions/permissions-query-v2'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { IS_PLATFORM } from '@/lib/constants'
import { useGrant } from '@/lib/grants'
const EMPTY_SUMMARY: GitHubConfigDriftSummary = {
driftedFields: [],
matchedFields: [],
unmanagedFields: [],
}
type ConfigDriftMemoResult =
| { status: 'success'; summary: GitHubConfigDriftSummary }
| { status: 'error'; source: 'project-config' | 'config-file'; error: Error }
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 branchesGrant = useGrant('GET /v1/projects/{ref}/branches', {
projectRef: parentProjectRef,
})
const isPermissionDenied = branchesGrant.status === 'denied'
const shouldLoad = IS_PLATFORM && Boolean(projectRef) && Boolean(project) && !isPermissionDenied
const branchesQuery = useQuery(
branchesQueryOptions(
{ projectRef: parentProjectRef, grant: branchesGrant.grant },
{ 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: permissionsRefetch } = usePermissionsQueryV2()
const { refetch: branchesRefetch } = branchesQuery
const { refetch: connectionRefetch } = connectionQuery
const { refetch: projectConfigRefetch } = projectConfigQuery
const { refetch: githubConfigRefetch } = githubConfigQuery
const isGrantError = branchesGrant.status === 'error'
const refetch = useCallback(() => {
if (isGrantError) return Promise.all([permissionsRefetch(), connectionRefetch()])
return Promise.all([
branchesRefetch(),
connectionRefetch(),
projectConfigRefetch(),
githubConfigRefetch(),
])
}, [
isGrantError,
permissionsRefetch,
branchesRefetch,
connectionRefetch,
projectConfigRefetch,
githubConfigRefetch,
])
const configFileName = githubConfigQuery.data?.path?.split('/').pop()
const driftResult = useMemo((): ConfigDriftMemoResult => {
let dashboardConfig
try {
dashboardConfig = fromDashboardProjectConfig(projectConfigQuery.data?.attributes)
} catch (error) {
console.error('Failed to read project configuration:', error)
return {
status: 'error',
source: 'project-config',
error: new Error('Could not read the project configuration returned by the API.'),
}
}
const result = getConfigDriftSummary({
dashboardConfig,
githubConfig: githubConfigQuery.data?.config,
})
if (result.status === 'invalid-config') {
return {
status: 'error',
source: 'config-file',
error: new Error(formatGitHubConfigDecodeMessage(result.issues, configFileName)),
}
}
return { status: 'success', summary: result.summary }
}, [projectConfigQuery.data?.attributes, githubConfigQuery.data?.config, configFileName])
const summary = driftResult.status === 'success' ? driftResult.summary : EMPTY_SUMMARY
const conversionError = driftResult.status === 'error' ? driftResult.error : undefined
const branchesState = {
isPending:
branchesGrant.status === 'loading' ||
(branchesGrant.status === 'granted' && branchesQuery.isPending),
isFetching: branchesQuery.isFetching,
isError: branchesQuery.isError || branchesGrant.status === 'error',
error: branchesGrant.status === 'error' ? branchesGrant.error : branchesQuery.error,
}
const activeQueries = [
projectQuery,
...(shouldLoad ? [branchesState, connectionQuery] : []),
...(queriesEnabled ? [projectConfigQuery, githubConfigQuery] : []),
]
const isReady =
shouldLoad && hasConnection && projectConfigQuery.isSuccess && githubConfigQuery.isSuccess
const issueCount = summary.driftedFields.length
const queryError = activeQueries.find((query) => query.error)?.error
let errorSource: 'query' | 'project-config' | 'config-file' | undefined = undefined
if (queryError) {
errorSource = 'query'
} else if (driftResult.status === 'error') {
errorSource = driftResult.source
}
return {
requestedGitBranch: gitBranch,
isReady,
isPermissionDenied,
isPending: activeQueries.some((query) => query.isPending),
isFetching: activeQueries.some((query) => query.isFetching),
isError: activeQueries.some((query) => query.isError) || conversionError !== undefined,
error: queryError ?? conversionError,
errorSource,
configFileName,
hasConfigurationIssues: isReady && issueCount > 0,
summary,
refetch,
}
}