-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add github callback route to proxy backend
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/app/(dynamic-pages)/(authenticated-pages)/github_app/error/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Link from "next/link"; | ||
|
||
export default function GithubAppError() { | ||
const appSlug = process.env.NEXT_PUBLIC_GITHUB_APP_SLUG; | ||
return ( | ||
<> | ||
<h1>Something went wrong</h1> | ||
<p>GitHub App installation failed. Maybe <Link href={`https://github.com/apps/${appSlug}/installations/new/`}>try re-install it?</Link></p> | ||
</> | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
src/app/(dynamic-pages)/(authenticated-pages)/github_app/success/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Link from "next/link"; | ||
|
||
export default function GithubAppError() { | ||
return ( | ||
<> | ||
<h1>Something went wrong</h1> | ||
<p>GitHub App installation failed. You can now close this tab or <Link href="/">go to dashboard</Link></p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { createSupabaseUserRouteHandlerClient } from '@/supabase-clients/user/createSupabaseUserRouteHandlerClient'; | ||
import { toSiteURL } from '@/utils/helpers'; | ||
import retry from 'async-retry'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
// Use the environment variable for the callback URL | ||
const AUTH_SERVICE_URL = process.env.GITHUB_CALLBACK_URL; | ||
const DIGGER_WEBHOOK_SECRET = process.env.DIGGER_WEBHOOK_SECRET; | ||
|
||
if (!AUTH_SERVICE_URL) { | ||
throw new Error('GITHUB_CALLBACK_URL environment variable is not set'); | ||
} | ||
|
||
export async function GET(request: NextRequest) { | ||
const searchParams = request.nextUrl.searchParams; | ||
const installationId = searchParams.get('installation_id'); | ||
|
||
if (!installationId) { | ||
return NextResponse.json( | ||
{ error: 'Missing installation_id' }, | ||
{ status: 400 }, | ||
); | ||
} | ||
|
||
try { | ||
console.log( | ||
'Trying to get org id for the following installation ID:', | ||
installationId, | ||
); | ||
const organizationId = await getOrganizationId(installationId); | ||
const response = await fetch( | ||
`${AUTH_SERVICE_URL}?${searchParams.toString()}`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Digger-Org-ID': organizationId, | ||
Authorization: `Bearer ${DIGGER_WEBHOOK_SECRET}`, | ||
}, | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Auth service responded with status: ${response.status}`); | ||
} | ||
return NextResponse.redirect(toSiteURL('/github_app/success')); | ||
} catch (error) { | ||
console.error('Error handling GitHub App installation callback:', error); | ||
return NextResponse.redirect(toSiteURL('/github_app/error')); | ||
} | ||
} | ||
|
||
async function getOrganizationId(installationId: string): Promise<string> { | ||
const supabase = createSupabaseUserRouteHandlerClient(); | ||
return retry( | ||
async (bail) => { | ||
const { data, error } = await supabase | ||
.from('github_app_installation_links') | ||
.select('organization_id') | ||
.eq('github_installation_id', installationId) | ||
.single(); | ||
|
||
if (error) { | ||
if (error.code === '404') bail(new Error('Organization not found')); | ||
throw error; | ||
} | ||
|
||
if (!data?.organization_id) { | ||
throw new Error('Organization ID not found'); | ||
} | ||
|
||
return data.organization_id; | ||
}, | ||
{ | ||
retries: 2, | ||
onRetry: (error, attempt) => { | ||
console.log(`Attempt ${attempt} failed. Retrying...`); | ||
}, | ||
}, | ||
); | ||
} |