console/packages/web/app/pages/api/github/setup-callback.ts
Laurin Quast a03cc58e5b
feat: remove app next.js built-time environment variable requirement (#382)
* feat: retreive stripe public key via runtime environment variable instead of a build-time environment variable

* feat: retreive google and github enabled environment variable  via runtime environment variable instead of a build-time environment variable

* feat: retreive app base url  environment variable via runtime environment variable instead of a build-time environment variable

* feat: retreive mixpanel token environment variable via runtime environment variable instead of a build-time environment variable

* lazy initialize app info

* feat: provide ga tarcking id and crisp website id via environment variable

* make docs url optional

* feat: load sentry config from environment variables

* document hive app environment variables

* add application dockerfile

* add docker build instructions

* lul

* set working directory

* pls fix

* lol this is apollo-router not graphql-hive

* feat: only show sentry stuff if sentry environment variables are set

* use LTS node version

* No mixpanel

* Fallback

Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>
2022-09-21 12:47:40 +02:00

48 lines
1.3 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from 'next';
import { graphql } from '@/lib/api/utils';
import { ensureGithubIntegration } from './callback';
export default async function githubSetupCallback(req: NextApiRequest, res: NextApiResponse) {
console.log('GitHub Integration Setup Callback');
const installationId = req.query.installation_id as string;
let orgId = req.query.state as string | undefined;
if (orgId) {
await ensureGithubIntegration(req, {
installationId,
orgId,
});
} else {
const result = await graphql<{
organizationByGitHubInstallationId?: {
cleanId: string;
};
}>({
url: `${process.env['APP_BASE_URL'].replace(/\/$/, '')}/api/proxy`,
headers: {
...req.headers,
'content-type': 'application/json',
},
operationName: 'getOrganizationByGitHubInstallationId',
query: /* GraphQL */ `
query getOrganizationByGitHubInstallationId($installation: ID!) {
organizationByGitHubInstallationId(input: $input) {
id
cleanId
}
}
`,
variables: {
installation: installationId,
},
});
orgId = result.data?.organizationByGitHubInstallationId?.cleanId;
}
if (orgId) {
res.redirect(`/${orgId}/settings`);
} else {
res.redirect('/');
}
}