diff --git a/apps/studio/components/interfaces/BranchManagement/CreateBranchModal.tsx b/apps/studio/components/interfaces/BranchManagement/CreateBranchModal.tsx
index e4d72526823..239731dba60 100644
--- a/apps/studio/components/interfaces/BranchManagement/CreateBranchModal.tsx
+++ b/apps/studio/components/interfaces/BranchManagement/CreateBranchModal.tsx
@@ -146,7 +146,7 @@ export const CreateBranchModal = () => {
const isError = isErrorAuthorization || isErrorConnections
const error = authorizationError || connectionsError
- const { data: branches } = useQuery(
+ const { data: branches, isSuccess: isSuccessBranches } = useQuery(
branchesQueryOptions({ projectRef, grant: branchesGrant.grant })
)
const { data: addons, isSuccess: isSuccessAddons } = useProjectAddonsQuery(
@@ -572,7 +572,7 @@ export const CreateBranchModal = () => {
)}
- {githubConnection && (
+ {githubConnection && isSuccessBranches && (
diff --git a/apps/studio/components/interfaces/Branching/MergeRequest.tsx b/apps/studio/components/interfaces/Branching/MergeRequest.tsx
index 69d78d46331..7898906be05 100644
--- a/apps/studio/components/interfaces/Branching/MergeRequest.tsx
+++ b/apps/studio/components/interfaces/Branching/MergeRequest.tsx
@@ -117,10 +117,12 @@ export const MergeSubtitle = () => {
export const MergeActions = ({
isWorkflowRunning,
isSubmitting,
+ canMerge,
onSelectMerge,
}: {
isWorkflowRunning: boolean
isSubmitting: boolean
+ canMerge: boolean
onSelectMerge: () => void
}) => {
const router = useRouter()
@@ -165,6 +167,7 @@ export const MergeActions = ({
hasChanges: combinedHasChanges,
} = useBranchMergeDiff({
currentBranchRef: ref,
+ isCurrentBranchDefault: currentBranch?.is_default,
parentProjectRef,
currentBranchConnectionString: project?.connectionString || undefined,
parentBranchConnectionString: parentProject?.connectionString || undefined,
@@ -172,6 +175,7 @@ export const MergeActions = ({
})
const isMergeDisabled =
+ !canMerge ||
!combinedHasChanges ||
isCombinedDiffLoading ||
isBranchOutOfDateOverall ||
@@ -192,13 +196,15 @@ export const MergeActions = ({
tooltip={{
content: {
side: 'bottom',
- text: !combinedHasChanges
- ? 'No changes to merge'
- : isWorkflowRunning
- ? 'Workflow is currently running'
- : !!ghConnection && Boolean(mainBranch?.git_branch)
- ? 'Deploy to production from GitHub is enabled'
- : 'Unable to merge at this time',
+ text: !canMerge
+ ? 'You need additional permissions to merge branches'
+ : !combinedHasChanges
+ ? 'No changes to merge'
+ : isWorkflowRunning
+ ? 'Workflow is currently running'
+ : !!ghConnection && Boolean(mainBranch?.git_branch)
+ ? 'Deploy to production from GitHub is enabled'
+ : 'Unable to merge at this time',
},
}}
variant="primary"
diff --git a/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.test.tsx b/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.test.tsx
index 0c35ff7b567..d72f67e764f 100644
--- a/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.test.tsx
+++ b/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.test.tsx
@@ -216,4 +216,37 @@ describe('GitHubIntegrationConnectionForm', () => {
expect(await screen.findAllByText('Failed to load branches')).not.toHaveLength(0)
expect(saveButton).toHaveAttribute('aria-disabled', 'true')
})
+
+ test('keeps saving blocked while the branch list is still loading', async () => {
+ mockEndpoints([
+ FGA_PERMISSIONS.PROJECT.BRANCHING_PRODUCTION_READ,
+ FGA_PERMISSIONS.PROJECT.BRANCHING_PRODUCTION_CREATE,
+ FGA_PERMISSIONS.PROJECT.BRANCHING_PRODUCTION_WRITE,
+ ])
+ let resolveBranches: () => void = () => {}
+ const branchesLoaded = new Promise((resolve) => {
+ resolveBranches = resolve
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/v1/projects/:ref/branches',
+ response: async () => {
+ await branchesLoaded
+ return HttpResponse.json<(typeof PRODUCTION_BRANCH)[]>([PRODUCTION_BRANCH])
+ },
+ })
+
+ renderForm()
+
+ const workingDirectory = await screen.findByLabelText('Working directory')
+ await waitFor(() => expect(workingDirectory).toBeEnabled())
+ await userEvent.type(workingDirectory, '/app')
+ const saveButton = await screen.findByRole('button', { name: 'Save changes' })
+ expect(saveButton).toBeDisabled()
+
+ resolveBranches()
+ await editWorkingDirectory()
+
+ await waitFor(() => expect(saveButton).toBeEnabled())
+ })
})
diff --git a/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.tsx b/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.tsx
index efefddcab92..4c551e1c977 100644
--- a/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.tsx
+++ b/apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.tsx
@@ -345,6 +345,11 @@ export const GitHubIntegrationConnectionForm = ({
currentConnection: GitHubConnection
) => {
if (!selectedProject?.ref || !selectedOrganization?.id) return
+ if (!canManageProductionBranch) {
+ setIsConfirmingBranchChange(false)
+ toast.error(productionBranchPermissionMessage)
+ return
+ }
const connectionRequest = updateConnectionSettings({
connectionId: currentConnection.id,
diff --git a/apps/studio/data/branches/branch-diff-query.ts b/apps/studio/data/branches/branch-diff-query.ts
index 4db21232a05..3191d86cae8 100644
--- a/apps/studio/data/branches/branch-diff-query.ts
+++ b/apps/studio/data/branches/branch-diff-query.ts
@@ -1,22 +1,32 @@
-import { useQuery } from '@tanstack/react-query'
+import { queryOptions, skipToken } from '@tanstack/react-query'
import { branchKeys } from './keys'
-import { get, handleError, unguarded } from '@/data/fetchers'
+import { get, handleError } from '@/data/fetchers'
import { IS_PLATFORM } from '@/lib/constants'
-import type { ResponseError, UseCustomQueryOptions } from '@/types'
+import type { GrantForVariants } from '@/lib/grants'
+import type { ResponseError } from '@/types'
+
+export type BranchDiffGrant = GrantForVariants<'get', '/v1/branches/{branch_id_or_ref}/diff'>
export type BranchDiffVariables = {
- branchRef: string
- projectRef: string
+ branchRef?: string
+ projectRef?: string
includedSchemas?: string
pgdelta?: boolean
+ grant?: BranchDiffGrant
}
export async function getBranchDiff({
branchRef,
includedSchemas,
pgdelta,
-}: Pick) {
+ grant,
+}: {
+ branchRef: string
+ includedSchemas?: string
+ pgdelta?: boolean
+ grant: BranchDiffGrant
+}) {
const query: { included_schemas?: string; pgdelta?: string } = {}
if (includedSchemas) query.included_schemas = includedSchemas
if (pgdelta === true) query.pgdelta = 'true'
@@ -30,7 +40,7 @@ export async function getBranchDiff({
Accept: 'text/plain',
},
parseAs: 'text',
- grant: unguarded.migrating,
+ grant,
})
if (error) {
@@ -45,18 +55,20 @@ export async function getBranchDiff({
return diffData || ''
}
-type BranchDiffData = Awaited>
+export type BranchDiffData = Awaited>
+export type BranchDiffError = ResponseError
-export const useBranchDiffQuery = (
- { branchRef, projectRef, includedSchemas, pgdelta }: BranchDiffVariables,
- {
- enabled = true,
- ...options
- }: Omit, 'queryKey' | 'queryFn'> = {}
-) =>
- useQuery({
+export const branchDiffQueryOptions = ({
+ branchRef,
+ projectRef,
+ includedSchemas,
+ pgdelta,
+ grant,
+}: BranchDiffVariables) =>
+ queryOptions({
queryKey: branchKeys.diff(projectRef, branchRef, pgdelta),
- queryFn: () => getBranchDiff({ branchRef, includedSchemas, pgdelta }),
- enabled: IS_PLATFORM && enabled && Boolean(branchRef),
- ...options,
+ queryFn:
+ IS_PLATFORM && branchRef && grant
+ ? () => getBranchDiff({ branchRef, includedSchemas, pgdelta, grant })
+ : skipToken,
})
diff --git a/apps/studio/data/branches/branch-merge-mutation.ts b/apps/studio/data/branches/branch-merge-mutation.ts
index 5d6f5c30e7b..5f4a52582cc 100644
--- a/apps/studio/data/branches/branch-merge-mutation.ts
+++ b/apps/studio/data/branches/branch-merge-mutation.ts
@@ -1,13 +1,18 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { getBranchDiff } from './branch-diff-query'
+import { getBranchDiff, type BranchDiffGrant } from './branch-diff-query'
import { branchKeys } from './keys'
import { upsertMigration } from '@/data/database/migration-upsert-mutation'
-import { handleError, post, unguarded } from '@/data/fetchers'
+import { handleError, post } from '@/data/fetchers'
+import type { GrantForVariants } from '@/lib/grants'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
+export type BranchMergeGrant = GrantForVariants<'post', '/v1/branches/{branch_id_or_ref}/merge'>
+
export type BranchMergeVariables = {
+ grant: BranchMergeGrant
+ diffGrant: BranchDiffGrant
branchProjectRef: string
baseProjectRef: string
migration_version?: string
@@ -18,9 +23,15 @@ 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 })
+ const diffContent = await getBranchDiff({
+ branchRef: branchProjectRef,
+ pgdelta,
+ grant: diffGrant,
+ })
let migrationCreated = false
@@ -43,7 +54,7 @@ export async function mergeBranch({
const { data, error } = await post('/v1/branches/{branch_id_or_ref}/merge', {
params: { path: { branch_id_or_ref: branchProjectRef } },
body: { migration_version },
- grant: unguarded.migrating,
+ grant,
})
if (error) {
diff --git a/apps/studio/hooks/branches/useBranchMergeDiff.ts b/apps/studio/hooks/branches/useBranchMergeDiff.ts
index 58f9b026eb3..1b56c8a069f 100644
--- a/apps/studio/hooks/branches/useBranchMergeDiff.ts
+++ b/apps/studio/hooks/branches/useBranchMergeDiff.ts
@@ -1,12 +1,15 @@
+import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
+import { useBranchGrant } from './useBranchGrant'
import { useEdgeFunctionsDiff, type EdgeFunctionsDiffResult } from './useEdgeFunctionsDiff'
import { useIsPgDeltaDiffEnabled } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
-import { useBranchDiffQuery } from '@/data/branches/branch-diff-query'
+import { branchDiffQueryOptions } from '@/data/branches/branch-diff-query'
import { useMigrationsQuery } from '@/data/database/migrations-query'
interface UseBranchMergeDiffProps {
currentBranchRef?: string
+ isCurrentBranchDefault?: boolean
parentProjectRef?: string
currentBranchConnectionString?: string
parentBranchConnectionString?: string
@@ -48,6 +51,7 @@ export interface BranchMergeDiffResult {
export const useBranchMergeDiff = ({
currentBranchRef,
+ isCurrentBranchDefault,
parentProjectRef,
currentBranchConnectionString,
parentBranchConnectionString,
@@ -55,26 +59,39 @@ export const useBranchMergeDiff = ({
}: UseBranchMergeDiffProps): BranchMergeDiffResult => {
const pgDeltaDiffEnabled = useIsPgDeltaDiffEnabled()
+ const diffGrant = useBranchGrant('GET /v1/branches/{branch_id_or_ref}/diff', {
+ projectRef: parentProjectRef,
+ isDefault: isCurrentBranchDefault,
+ })
+
// Get database diff
const {
data: diffContent,
- isPending: isDatabaseDiffLoading,
+ isPending: isDiffQueryPending,
isRefetching: isDatabaseDiffRefetching,
- error: databaseDiffError,
+ error: diffQueryError,
refetch: refetchDatabaseDiff,
- } = useBranchDiffQuery(
- {
- branchRef: currentBranchRef || '',
- projectRef: parentProjectRef || '',
+ } = useQuery({
+ ...branchDiffQueryOptions({
+ branchRef: currentBranchRef,
+ projectRef: parentProjectRef,
pgdelta: pgDeltaDiffEnabled,
- },
- {
- enabled: !!currentBranchRef && !!parentProjectRef,
- refetchOnMount: 'always',
- refetchOnWindowFocus: false,
- staleTime: 0,
- }
- )
+ grant: diffGrant.grant,
+ }),
+ enabled: !!currentBranchRef && !!parentProjectRef,
+ refetchOnMount: 'always',
+ refetchOnWindowFocus: false,
+ staleTime: 0,
+ })
+
+ const isDatabaseDiffLoading =
+ diffGrant.status === 'loading' || (diffGrant.status === 'granted' && isDiffQueryPending)
+ let databaseDiffError: unknown = diffQueryError
+ if (diffGrant.status === 'denied') {
+ databaseDiffError = new Error('You need additional permissions to view branch diffs')
+ } else if (diffGrant.status === 'error') {
+ databaseDiffError = diffGrant.error
+ }
// Get migrations for both current branch and main branch
const { data: currentBranchMigrations, refetch: refetchCurrentBranchMigrations } =
diff --git a/apps/studio/pages/project/[ref]/merge.tsx b/apps/studio/pages/project/[ref]/merge.tsx
index 0128c82c221..b134f30e956 100644
--- a/apps/studio/pages/project/[ref]/merge.tsx
+++ b/apps/studio/pages/project/[ref]/merge.tsx
@@ -87,6 +87,14 @@ const MergePage: NextPageWithLayout = () => {
projectRef: parentProjectRef,
isDefault: currentBranch?.is_default,
})
+ const mergeBranchGrant = useBranchGrant('POST /v1/branches/{branch_id_or_ref}/merge', {
+ projectRef: parentProjectRef,
+ isDefault: currentBranch?.is_default,
+ })
+ const diffGrant = useBranchGrant('GET /v1/branches/{branch_id_or_ref}/diff', {
+ projectRef: parentProjectRef,
+ isDefault: currentBranch?.is_default,
+ })
const {
diffContent,
@@ -106,6 +114,7 @@ const MergePage: NextPageWithLayout = () => {
modifiedFunctionsCount,
} = useBranchMergeDiff({
currentBranchRef: ref,
+ isCurrentBranchDefault: currentBranch?.is_default,
parentProjectRef,
currentBranchConnectionString: project?.connectionString || undefined,
parentBranchConnectionString: parentProject?.connectionString || undefined,
@@ -278,12 +287,21 @@ const MergePage: NextPageWithLayout = () => {
}
const handleMerge = () => {
- if (!ref || !parentProjectRef) return
+ if (
+ !ref ||
+ !parentProjectRef ||
+ mergeBranchGrant.status !== 'granted' ||
+ diffGrant.status !== 'granted'
+ ) {
+ return
+ }
setIsSubmitting(true)
track('branch_merge_submitted', undefined, { project: parentProjectRef })
mergeBranch({
+ grant: mergeBranchGrant.grant,
+ diffGrant: diffGrant.grant,
branchProjectRef: ref,
baseProjectRef: parentProjectRef,
migration_version: undefined,
@@ -380,6 +398,7 @@ const MergePage: NextPageWithLayout = () => {
setShowConfirmDialog(true)}
/>
}