Skip to content

Commit e8c37cc

Browse files
authored
fix(studio): refetch GitHub repositories after new install (supabase#39021)
* fix(studio): add refetch after github pop-up close * fix: github.ts * fix: add delay * chore: increase to 2s * chore: remove debug logs
1 parent 9cfa6e8 commit e8c37cc

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

apps/studio/components/interfaces/Settings/Integrations/GithubIntegration/GitHubIntegrationConnectionForm.tsx

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
4747

4848
const GITHUB_ICON = (
4949
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 98 96" className="w-6">
50+
<title>GitHub icon</title>
5051
<path
5152
fill="#ffffff"
5253
fillRule="evenodd"
@@ -70,7 +71,7 @@ const GitHubIntegrationConnectionForm = ({
7071
const [isConfirmingBranchChange, setIsConfirmingBranchChange] = useState(false)
7172
const [isConfirmingRepoChange, setIsConfirmingRepoChange] = useState(false)
7273
const [repoComboBoxOpen, setRepoComboboxOpen] = useState(false)
73-
const isParentProject = !Boolean(selectedProject?.parent_project_ref)
74+
const isParentProject = !selectedProject?.parent_project_ref
7475

7576
const { can: canUpdateGitHubConnection } = useAsyncCheckPermissions(
7677
PermissionAction.UPDATE,
@@ -81,14 +82,24 @@ const GitHubIntegrationConnectionForm = ({
8182
'integrations.github_connections'
8283
)
8384

84-
const { data: gitHubAuthorization } = useGitHubAuthorizationQuery()
85+
const { data: gitHubAuthorization, refetch: refetchGitHubAuthorization } =
86+
useGitHubAuthorizationQuery()
8587

86-
const { data: githubReposData, isLoading: isLoadingGitHubRepos } = useGitHubRepositoriesQuery<
87-
any[]
88-
>({
88+
const {
89+
data: githubReposData,
90+
isLoading: isLoadingGitHubRepos,
91+
refetch: refetchGitHubRepositories,
92+
} = useGitHubRepositoriesQuery({
8993
enabled: Boolean(gitHubAuthorization),
9094
})
9195

96+
const refetchGitHubAuthorizationAndRepositories = () => {
97+
setTimeout(() => {
98+
refetchGitHubAuthorization()
99+
refetchGitHubRepositories()
100+
}, 2000) // 2 second to delay to let github authorization and repositories to be updated
101+
}
102+
92103
const { mutate: updateBranch } = useBranchUpdateMutation({
93104
onSuccess: () => {
94105
toast.success('Production branch settings successfully updated')
@@ -130,7 +141,7 @@ const GitHubIntegrationConnectionForm = ({
130141

131142
const githubRepos = useMemo(
132143
() =>
133-
githubReposData?.map((repo: any) => ({
144+
githubReposData?.map((repo) => ({
134145
id: repo.id.toString(),
135146
name: repo.name,
136147
installation_id: repo.installation_id,
@@ -161,7 +172,7 @@ const GitHubIntegrationConnectionForm = ({
161172
repositoryId: Number(repositoryId),
162173
branchName: val.branchName,
163174
})
164-
} catch (error) {
175+
} catch {
165176
const selectedRepo = githubRepos.find((repo) => repo.id === repositoryId)
166177
const repoName =
167178
selectedRepo?.name || connection?.repository.name || 'selected repository'
@@ -337,11 +348,11 @@ const GitHubIntegrationConnectionForm = ({
337348
const data = githubSettingsForm.getValues()
338349
const selectedRepo = githubRepos.find((repo) => repo.id === data.repositoryId)
339350

340-
if (!selectedRepo || !connection) return
351+
if (!selectedRepo || !connection || !selectedOrganization?.id) return
341352

342353
try {
343354
await deleteConnection({
344-
organizationId: selectedOrganization!.id,
355+
organizationId: selectedOrganization.id,
345356
connectionId: connection.id,
346357
})
347358

@@ -390,7 +401,10 @@ const GitHubIntegrationConnectionForm = ({
390401
</p>
391402
<Button
392403
onClick={() => {
393-
openInstallGitHubIntegrationWindow('authorize')
404+
openInstallGitHubIntegrationWindow(
405+
'authorize',
406+
refetchGitHubAuthorizationAndRepositories
407+
)
394408
}}
395409
>
396410
Authorize GitHub
@@ -482,7 +496,12 @@ const GitHubIntegrationConnectionForm = ({
482496
<CommandGroup_Shadcn_>
483497
<CommandItem_Shadcn_
484498
className="flex gap-2 items-center cursor-pointer"
485-
onSelect={() => openInstallGitHubIntegrationWindow('install')}
499+
onSelect={() =>
500+
openInstallGitHubIntegrationWindow(
501+
'install',
502+
refetchGitHubAuthorizationAndRepositories
503+
)
504+
}
486505
>
487506
<PlusIcon size={16} />
488507
Add GitHub Repositories

apps/studio/data/integrations/github-repositories-query.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ export async function getGitHubRepositories(signal?: AbortSignal) {
1010
})
1111

1212
if (error) handleError(error)
13-
// [Alaister]: temp fix until we have a proper response type
14-
return (data as any).repositories
13+
return data.repositories
1514
}
1615

1716
export type GitHubRepositoriesData = Awaited<ReturnType<typeof getGitHubRepositories>>

apps/studio/lib/github.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ const GITHUB_INTEGRATION_AUTHORIZATION_URL = `https://github.com/login/oauth/aut
1919
export const GITHUB_INTEGRATION_INSTALLATION_URL = `https://github.com/apps/${GITHUB_INTEGRATION_APP_NAME}/installations/new`
2020
export const GITHUB_INTEGRATION_REVOKE_AUTHORIZATION_URL = `https://github.com/settings/connections/applications/${GITHUB_INTEGRATION_CLIENT_ID}`
2121

22-
export function openInstallGitHubIntegrationWindow(type: 'install' | 'authorize') {
22+
export function openInstallGitHubIntegrationWindow(
23+
type: 'install' | 'authorize',
24+
closeCallback?: () => void
25+
) {
2326
const w = 600
2427
const h = 800
2528

@@ -37,10 +40,10 @@ export function openInstallGitHubIntegrationWindow(type: 'install' | 'authorize'
3740
? document.documentElement.clientHeight
3841
: screen.height
3942

40-
let windowUrl
43+
let windowUrl: string | undefined
4144
if (type === 'install') {
4245
windowUrl = GITHUB_INTEGRATION_INSTALLATION_URL
43-
} else if (type === 'authorize') {
46+
} else {
4447
const state = makeRandomString(32)
4548
localStorage.setItem(LOCAL_STORAGE_KEYS.GITHUB_AUTHORIZATION_STATE, state)
4649
windowUrl = `${GITHUB_INTEGRATION_AUTHORIZATION_URL}&state=${state}`
@@ -60,6 +63,20 @@ export function openInstallGitHubIntegrationWindow(type: 'install' | 'authorize'
6063
`
6164
)
6265
if (newWindow) {
66+
if (closeCallback) {
67+
// Poll to check if window is closed
68+
const checkClosed = setInterval(() => {
69+
if (newWindow.closed) {
70+
clearInterval(checkClosed)
71+
closeCallback()
72+
}
73+
}, 500) // Check every 500ms
74+
75+
// Add a timeout to prevent infinite polling
76+
setTimeout(() => {
77+
clearInterval(checkClosed)
78+
}, 300000) // 5 minutes timeout
79+
}
6380
newWindow.focus()
6481
}
6582
}

0 commit comments

Comments
 (0)