From 9f9d4297cae21bc39c1686198f85c98d9c9d7a0d Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 12 Jul 2022 13:08:47 +0000 Subject: [PATCH] feat: Gitpod integration --- apps/api/src/lib/common.ts | 22 ++++++++++++++++--- .../src/routes/webhooks/github/handlers.ts | 6 ++--- .../src/routes/webhooks/gitlab/handlers.ts | 6 ++--- apps/ui/src/lib/api.ts | 20 ++++++++++++++--- .../configuration/_GitlabRepositories.svelte | 6 ++--- .../ui/src/routes/sources/[id]/_Github.svelte | 6 ++--- .../ui/src/routes/sources/[id]/_Gitlab.svelte | 4 ++-- 7 files changed, 50 insertions(+), 20 deletions(-) diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 741d33452..328861e70 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -15,7 +15,7 @@ import { checkContainer, getEngine, removeContainer } from './docker'; import { day } from './dayjs'; import * as serviceFields from './serviceFields' -export const version = '3.1.0'; +export const version = '3.2.0'; export const isDev = process.env.NODE_ENV === 'development'; const algorithm = 'aes-256-ctr'; @@ -30,13 +30,29 @@ export const defaultProxyImage = `coolify-haproxy-alpine:latest`; export const defaultProxyImageTcp = `coolify-haproxy-tcp-alpine:latest`; export const defaultProxyImageHttp = `coolify-haproxy-http-alpine:latest`; export const defaultTraefikImage = `traefik:v2.6`; +export function getAPIUrl() { + if (process.env.GITPOD_WORKSPACE_URL) { + const { href } = new URL(process.env.GITPOD_WORKSPACE_URL) + const newURL = href.replace('https://', 'https://3001-').replace(/\/$/, '') + return newURL + } + return isDev ? 'http://localhost:3001' : 'http://localhost:3000'; +} +export function getUIUrl() { + if (process.env.GITPOD_WORKSPACE_URL) { + const { href } = new URL(process.env.GITPOD_WORKSPACE_URL) + const newURL = href.replace('https://', 'https://3000-').replace(/\/$/, '') + return newURL + } + return 'http://localhost:3000'; +} const mainTraefikEndpoint = isDev - ? 'http://host.docker.internal:3001/webhooks/traefik/main.json' + ? `${getAPIUrl()}/webhooks/traefik/main.json` : 'http://coolify:3000/webhooks/traefik/main.json'; const otherTraefikEndpoint = isDev - ? 'http://host.docker.internal:3001/webhooks/traefik/other.json' + ? `${getAPIUrl()}/webhooks/traefik/other.json` : 'http://coolify:3000/webhooks/traefik/other.json'; diff --git a/apps/api/src/routes/webhooks/github/handlers.ts b/apps/api/src/routes/webhooks/github/handlers.ts index 1ff6731ea..5e9e403ee 100644 --- a/apps/api/src/routes/webhooks/github/handlers.ts +++ b/apps/api/src/routes/webhooks/github/handlers.ts @@ -2,7 +2,7 @@ import axios from "axios"; import cuid from "cuid"; import crypto from "crypto"; import type { FastifyReply, FastifyRequest } from "fastify"; -import { encrypt, errorHandler, isDev, prisma } from "../../../lib/common"; +import { encrypt, errorHandler, getAPIUrl, getUIUrl, isDev, prisma } from "../../../lib/common"; import { checkContainer, removeContainer } from "../../../lib/docker"; import { scheduler } from "../../../lib/scheduler"; import { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers"; @@ -19,7 +19,7 @@ export async function installGithub(request: FastifyRequest, reply: FastifyReply data: { installationId: Number(installation_id) } }); if (isDev) { - return reply.redirect(`http://localhost:3000/sources/${gitSourceId}`) + return reply.redirect(`${getUIUrl()}/sources/${gitSourceId}`) } else { return reply.redirect(`/sources/${gitSourceId}`) } @@ -55,7 +55,7 @@ export async function configureGitHubApp(request, reply) { } }); if (isDev) { - return reply.redirect(`http://localhost:3000/sources/${state}`) + return reply.redirect(`${getUIUrl()}/sources/${state}`) } else { return reply.redirect(`/sources/${state}`) } diff --git a/apps/api/src/routes/webhooks/gitlab/handlers.ts b/apps/api/src/routes/webhooks/gitlab/handlers.ts index 9b1dd3e3c..fab40cf05 100644 --- a/apps/api/src/routes/webhooks/gitlab/handlers.ts +++ b/apps/api/src/routes/webhooks/gitlab/handlers.ts @@ -2,7 +2,7 @@ import axios from "axios"; import cuid from "cuid"; import crypto from "crypto"; import type { FastifyReply, FastifyRequest } from "fastify"; -import { encrypt, errorHandler, isDev, listSettings, prisma } from "../../../lib/common"; +import { encrypt, errorHandler, getAPIUrl, isDev, listSettings, prisma } from "../../../lib/common"; import { checkContainer, removeContainer } from "../../../lib/docker"; import { scheduler } from "../../../lib/scheduler"; import { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers"; @@ -16,7 +16,7 @@ export async function configureGitLabApp(request: FastifyRequest, reply: Fastify let domain = `http://${request.hostname}`; if (fqdn) domain = fqdn; if (isDev) { - domain = `http://localhost:3001`; + domain = getAPIUrl(); } const params = new URLSearchParams({ client_id: appId, @@ -28,7 +28,7 @@ export async function configureGitLabApp(request: FastifyRequest, reply: Fastify }); const { data } = await axios.post(`${htmlUrl}/oauth/token`, params) if (isDev) { - return reply.redirect(`http://localhost:3000/webhooks/success?token=${data.access_token}`) + return reply.redirect(`${getAPIUrl()}/webhooks/success?token=${data.access_token}`) } return reply.redirect(`/webhooks/success?token=${data.access_token}`) } catch ({ status, message, ...other }) { diff --git a/apps/ui/src/lib/api.ts b/apps/ui/src/lib/api.ts index 84c4279a4..fc11e09d3 100644 --- a/apps/ui/src/lib/api.ts +++ b/apps/ui/src/lib/api.ts @@ -1,14 +1,28 @@ -import { browser, dev } from '$app/env'; +import { dev } from '$app/env'; import Cookies from 'js-cookie'; export function getAPIUrl() { if (GITPOD_WORKSPACE_URL) { - const {href} = new URL(GITPOD_WORKSPACE_URL) - const newURL = href.replace('https://','https://3001-').replace(/\/$/,'') + const { href } = new URL(GITPOD_WORKSPACE_URL) + const newURL = href.replace('https://', 'https://3001-').replace(/\/$/, '') return newURL } return dev ? 'http://localhost:3001' : 'http://localhost:3000'; } +export function getWebhookUrl(type: string) { + console.log(GITPOD_WORKSPACE_URL) + if (GITPOD_WORKSPACE_URL) { + const { href } = new URL(GITPOD_WORKSPACE_URL) + const newURL = href.replace('https://', 'https://3001-').replace(/\/$/, '') + if (type === 'github') { + return `${newURL}/webhooks/github/events` + } + if (type === 'gitlab') { + return `${newURL}/webhooks/gitlab/events` + } + } + return `https://webhook.site/0e5beb2c-4e9b-40e2-a89e-32295e570c21/events`; +} async function send({ method, path, diff --git a/apps/ui/src/routes/applications/[id]/configuration/_GitlabRepositories.svelte b/apps/ui/src/routes/applications/[id]/configuration/_GitlabRepositories.svelte index eb6729f5d..a91ebcc27 100644 --- a/apps/ui/src/routes/applications/[id]/configuration/_GitlabRepositories.svelte +++ b/apps/ui/src/routes/applications/[id]/configuration/_GitlabRepositories.svelte @@ -8,7 +8,7 @@ import { onMount } from 'svelte'; import { dev } from '$app/env'; import { goto } from '$app/navigation'; - import { del, get, post } from '$lib/api'; + import { del, get, getAPIUrl, getWebhookUrl, post } from '$lib/api'; import { t } from '$lib/translations'; import { errorNotification } from '$lib/common'; import { appSession } from '$lib/store'; @@ -18,7 +18,7 @@ const from = $page.url.searchParams.get('from'); let url = settings?.fqdn ? settings.fqdn : window.location.origin; - if (dev) url = `http://localhost:3001`; + if (dev) url = getAPIUrl(); const updateDeployKeyIdUrl = `/applications/${id}/configuration/deploykey`; @@ -228,7 +228,7 @@ } async function setWebhook(url: any, webhookToken: any) { const host = dev - ? 'https://webhook.site/0e5beb2c-4e9b-40e2-a89e-32295e570c21' + ? getWebhookUrl('gitlab') : `${window.location.origin}/webhooks/gitlab/events`; try { await post( diff --git a/apps/ui/src/routes/sources/[id]/_Github.svelte b/apps/ui/src/routes/sources/[id]/_Github.svelte index 21294fa27..176d233ff 100644 --- a/apps/ui/src/routes/sources/[id]/_Github.svelte +++ b/apps/ui/src/routes/sources/[id]/_Github.svelte @@ -2,7 +2,7 @@ export let source: any; export let settings: any; import { page } from '$app/stores'; - import { post } from '$lib/api'; + import { getAPIUrl, getWebhookUrl, post } from '$lib/api'; import Explainer from '$lib/components/Explainer.svelte'; import { toast } from '@zerodevx/svelte-toast'; import { t } from '$lib/translations'; @@ -67,7 +67,7 @@ const { organization, htmlUrl } = source; const { fqdn } = settings; const host = dev - ? 'http://localhost:3001' + ? getAPIUrl() : fqdn ? fqdn : `http://${window.location.host}` || ''; @@ -81,7 +81,7 @@ url: host, hook_attributes: { url: dev - ? 'https://webhook.site/0e5beb2c-4e9b-40e2-a89e-32295e570c21/events' + ? getWebhookUrl('github') : `${host}/webhooks/github/events` }, redirect_url: `${host}/webhooks/github`, diff --git a/apps/ui/src/routes/sources/[id]/_Gitlab.svelte b/apps/ui/src/routes/sources/[id]/_Gitlab.svelte index 25cb6d126..77db14eea 100644 --- a/apps/ui/src/routes/sources/[id]/_Gitlab.svelte +++ b/apps/ui/src/routes/sources/[id]/_Gitlab.svelte @@ -4,7 +4,7 @@ import Explainer from '$lib/components/Explainer.svelte'; import { page } from '$app/stores'; import { onMount } from 'svelte'; - import { post } from '$lib/api'; + import { getAPIUrl, post } from '$lib/api'; import { dev } from '$app/env'; import CopyPasswordField from '$lib/components/CopyPasswordField.svelte'; import { toast } from '@zerodevx/svelte-toast'; @@ -17,7 +17,7 @@ let url = settings.fqdn ? settings.fqdn : window.location.origin; if (dev) { - url = `http://localhost:3001`; + url = getAPIUrl(); } let loading = false;