Skip to content

Commit

Permalink
dev/setup-github: set more env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Nov 8, 2023
1 parent 5d4e8de commit a6041bf
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
1 change: 0 additions & 1 deletion .devcontainer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Requires no configuration.

Run the storybook in the terminal with `cd /workspace/clients && pnpm storybook`


# Testing

## Testing the api
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ env.bak/
venv.bak/
!.env.template
!.env.testing
.env.devcontainer

# Spyder project settings
.spyderproject
Expand All @@ -117,7 +118,6 @@ venv.bak/

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
Expand Down
2 changes: 1 addition & 1 deletion clients/apps/setup-github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev --port 3001",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand Down
15 changes: 14 additions & 1 deletion clients/apps/setup-github/src/app/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ const addEnv = (contents: string, key: string, value: any): string => {
const serverEnv = async (path: string, app: any) => {
const file = await fs.readFile(path, 'utf8')

const host = `${process.env.NEXT_PUBLIC_CODESPACE_NAME}.${process.env.NEXT_PUBLIC_GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`
const hostProto = `https://${host}`

const adds: Record<string, any> = {
POLAR_GITHUB_APP_IDENTIFIER: app.id,
POLAR_GITHUB_APP_WEBHOOK_SECRET: app.webhook_secret,
POLAR_GITHUB_APP_PRIVATE_KEY: app.pem,
POLAR_GITHUB_CLIENT_ID: app.client_id,
POLAR_GITHUB_CLIENT_SECRET: app.client_secret,

POLAR_CORS_ORIGINS: `["http://127.0.0.1:3000", "http://localhost:3000", "https://github.com", "${hostProto}:3000"]`,
POLAR_BASE_URL: `${hostProto}:8000/api/v1`,
POLAR_FRONTEND_BASE_URL: `${hostProto}:3000`,
POLAR_AUTH_COOKIE_DOMAIN: `${host}`,
POLAR_GITHUB_REDIRECT_URL: `${hostProto}:3000/github/session`,
}

let newFile = file
Expand All @@ -37,8 +46,12 @@ const serverEnv = async (path: string, app: any) => {
const webEnv = async (path: string, app: any) => {
const file = await fs.readFile(path, 'utf8')

const host = `${process.env.NEXT_PUBLIC_CODESPACE_NAME}.${process.env.NEXT_PUBLIC_GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`
const hostProto = `https://${host}`

const adds: Record<string, any> = {
NEXT_PUBLIC_GITHUB_APP_NAMESPACE: app.slug,
NEXT_PUBLIC_API_URL: `${hostProto}:8000`,
}

let newFile = file
Expand Down Expand Up @@ -82,7 +95,7 @@ export async function GET(request: Request): Promise<NextResponse> {
const newWebEnv = await webEnv(webEnvPath, parsed)
await fs.writeFile(webEnvPath, newWebEnv)

return NextResponse.redirect('/done')
return NextResponse.redirect(new URL('/done', request.url))
} catch (e) {
console.error(e)
if (e instanceof Error) {
Expand Down
40 changes: 35 additions & 5 deletions clients/apps/setup-github/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,43 @@ import { useEffect, useState } from 'react'

export default function Home() {
const [manifest, setManifest] = useState<string>('')
const [isGitHubCodespace, setIsGitHubCodespace] = useState(false)

useEffect(() => {
let host = window.location.host

if (process.env.NEXT_PUBLIC_CODESPACE_NAME) {
host = `https://${process.env.NEXT_PUBLIC_CODESPACE_NAME}.${process.env.NEXT_PUBLIC_GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`
setIsGitHubCodespace(true)
} else {
setIsGitHubCodespace(false)
}

const m = JSON.stringify(
{
// Redirect URL in the github app manifest setup flow
// Will not be a part of the app
redirect_url: `http://${window.location.host}/callback`,

name: 'My-Polar-Dev',
name: `polar-${process.env.NEXT_PUBLIC_CODESPACE_NAME}`.substring(
0,
32,
),
url: 'http://localhost',
hook_attributes: {
url: 'https://polarzegl.eu.ngrok.io/api/v1/integrations/github/webhook',
url: `${host}:8000/api/v1/integrations/github/webhook`,
},

setup_url:
'https://polarzegl.eu.ngrok.io/github/installation?provider=github',
setup_url: `${host}:3000/github/installation?provider=github`,
setup_on_update: true,
callback_urls: ['https://polarzegl.eu.ngrok.io/github/session'],
callback_urls: [`${host}:3000/github/session`],
public: true,
default_permissions: {
issues: 'write',
pull_requests: 'write',
members: 'read',
organization_events: 'read',
emails: "read",
},
default_events: [
'issues',
Expand All @@ -47,6 +60,12 @@ export default function Home() {
setManifest(m)
}, [])

if (!manifest) {
return (
<main className="flex min-h-screen flex-col gap-12 p-24">Loading..</main>
)
}

return (
<main className="flex min-h-screen flex-col gap-12 p-24">
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
Expand Down Expand Up @@ -81,6 +100,17 @@ export default function Home() {
&nbsp;👈👈
</form>
</div>
{isGitHubCodespace ? (
<div>
You&apos;re running on GitHub Codespace, this should work out of the
box!
</div>
) : (
<div>
Warning: You&apos;re not running in a tested environment (like GitHub
Codespace), this might not work.
</div>
)}
<div className="text-gray-600">
<h3>Using the following Manifest</h3>
<pre>
Expand Down
1 change: 0 additions & 1 deletion server/.env.devcontainer
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
POLAR_POSTGRES_USER="polar"
POLAR_POSTGRES_PWD="polar"
# It's a good idea to not re-use the same database as for local dev
POLAR_POSTGRES_DATABASE="polar"
POLAR_POSTGRES_PORT="5432"
POLAR_POSTGRES_HOST="db"
Expand Down

0 comments on commit a6041bf

Please sign in to comment.