Skip to content

Commit

Permalink
Add github callback route to proxy backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ZIJ committed Aug 19, 2024
1 parent 5ef1504 commit c06b572
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"@unkey/nextjs": "^0.15.0",
"@vercel/analytics": "^1.0.1",
"ai": "^3.1.12",
"async-retry": "^1.3.3",
"autoprefixer": "^10.4.13",
"axios": "^1.2.1",
"checkbox": "^0.0.1",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>
</>
)
}
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>
</>
)
}
81 changes: 81 additions & 0 deletions src/app/api/github-callback/route.ts
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...`);
},
},
);
}

0 comments on commit c06b572

Please sign in to comment.