mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 16:09:06 +00:00
Regenerates the API types with `pnpm api:codegen:prod` and fixes the
Studio type errors the new types caused.
## Changes from the API
- **Branch merge now requires production write permission.** `POST
/v1/branches/{branch_id_or_ref}/merge` changed from accepting either
`branching_development_write` or `branching_production_write` to
requiring `branching_production_write`. Studio's FGA refinement for
merge no longer matched and failed typecheck.
- Removed the merge entry from `lib/fga-refinements.ts`.
- `merge.tsx` checks merge permission with `useGrant` instead of
`useBranchGrant`.
- `BranchMergeGrant` uses `GrantFor` instead of `GrantForVariants`.
- Removed merge from the "requires a variant" test in
`guarded-fetch.test.ts`.
- Users with only development branching permissions can no longer merge
in Studio. This matches what the API now enforces.
- **Replication pipelines have a required `cluster_id`.** Added it to
the pipeline fixtures in six test files.
- **`MemberWithFreeProjectLimit` has a required
`free_plan_organizations`.** Added it to the mock in
`tests/pages/new/[slug].test.tsx`. No app code reads it yet.
## Testing
- `pnpm typecheck` passes.
- Vitest passes for the affected Studio tests.
103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { getBranchDiff, type BranchDiffGrant } from './branch-diff-query'
|
|
import { branchKeys } from './keys'
|
|
import { upsertMigration } from '@/data/database/migration-upsert-mutation'
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { GrantFor } from '@/lib/grants'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
export type BranchMergeGrant = GrantFor<'post', '/v1/branches/{branch_id_or_ref}/merge'>
|
|
|
|
export type BranchMergeVariables = {
|
|
grant: BranchMergeGrant
|
|
diffGrant: BranchDiffGrant
|
|
branchProjectRef: string
|
|
baseProjectRef: string
|
|
migration_version?: string
|
|
pgdelta?: boolean
|
|
}
|
|
|
|
export async function mergeBranch({
|
|
branchProjectRef,
|
|
migration_version,
|
|
pgdelta,
|
|
grant,
|
|
diffGrant,
|
|
}: BranchMergeVariables) {
|
|
// Step 1: Get the diff output from the branch
|
|
const diffContent = await getBranchDiff({
|
|
branchRef: branchProjectRef,
|
|
pgdelta,
|
|
grant: diffGrant,
|
|
})
|
|
|
|
let migrationCreated = false
|
|
|
|
// Step 2: If there are changes, create a migration before merging
|
|
if (diffContent && diffContent.trim() !== '') {
|
|
// Generate a descriptive migration name based on current timestamp
|
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5)
|
|
const migrationName = `branch_merge_${timestamp}`
|
|
|
|
await upsertMigration({
|
|
projectRef: branchProjectRef,
|
|
query: diffContent,
|
|
name: migrationName,
|
|
})
|
|
|
|
migrationCreated = true
|
|
}
|
|
|
|
// Step 3: Call POST /v1/branches/id/merge to merge the branch
|
|
const { data, error } = await post('/v1/branches/{branch_id_or_ref}/merge', {
|
|
params: { path: { branch_id_or_ref: branchProjectRef } },
|
|
body: { migration_version },
|
|
grant,
|
|
})
|
|
|
|
if (error) {
|
|
handleError(error)
|
|
}
|
|
|
|
return {
|
|
data,
|
|
migrationCreated,
|
|
hadChanges: diffContent && diffContent.trim() !== '',
|
|
workflowRunId: data?.workflow_run_id,
|
|
}
|
|
}
|
|
|
|
type BranchMergeData = Awaited<ReturnType<typeof mergeBranch>>
|
|
|
|
export const useBranchMergeMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<BranchMergeData, StudioError, BranchMergeVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<BranchMergeData, StudioError, BranchMergeVariables>({
|
|
mutationFn: (vars) => mergeBranch(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { baseProjectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: branchKeys.list(baseProjectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
let errorMessage = data.message || 'Unknown error occurred'
|
|
|
|
toast.error(`Failed to merge branch: ${errorMessage}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|