feat: Gitpod integration

This commit is contained in:
Andras Bacsai 2022-07-12 13:08:47 +00:00
parent 002637ec5f
commit 9f9d4297ca
7 changed files with 50 additions and 20 deletions

View File

@ -15,7 +15,7 @@ import { checkContainer, getEngine, removeContainer } from './docker';
import { day } from './dayjs'; import { day } from './dayjs';
import * as serviceFields from './serviceFields' 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'; export const isDev = process.env.NODE_ENV === 'development';
const algorithm = 'aes-256-ctr'; 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 defaultProxyImageTcp = `coolify-haproxy-tcp-alpine:latest`;
export const defaultProxyImageHttp = `coolify-haproxy-http-alpine:latest`; export const defaultProxyImageHttp = `coolify-haproxy-http-alpine:latest`;
export const defaultTraefikImage = `traefik:v2.6`; 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 const mainTraefikEndpoint = isDev
? 'http://host.docker.internal:3001/webhooks/traefik/main.json' ? `${getAPIUrl()}/webhooks/traefik/main.json`
: 'http://coolify:3000/webhooks/traefik/main.json'; : 'http://coolify:3000/webhooks/traefik/main.json';
const otherTraefikEndpoint = isDev const otherTraefikEndpoint = isDev
? 'http://host.docker.internal:3001/webhooks/traefik/other.json' ? `${getAPIUrl()}/webhooks/traefik/other.json`
: 'http://coolify:3000/webhooks/traefik/other.json'; : 'http://coolify:3000/webhooks/traefik/other.json';

View File

@ -2,7 +2,7 @@ import axios from "axios";
import cuid from "cuid"; import cuid from "cuid";
import crypto from "crypto"; import crypto from "crypto";
import type { FastifyReply, FastifyRequest } from "fastify"; 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 { checkContainer, removeContainer } from "../../../lib/docker";
import { scheduler } from "../../../lib/scheduler"; import { scheduler } from "../../../lib/scheduler";
import { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers"; 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) } data: { installationId: Number(installation_id) }
}); });
if (isDev) { if (isDev) {
return reply.redirect(`http://localhost:3000/sources/${gitSourceId}`) return reply.redirect(`${getUIUrl()}/sources/${gitSourceId}`)
} else { } else {
return reply.redirect(`/sources/${gitSourceId}`) return reply.redirect(`/sources/${gitSourceId}`)
} }
@ -55,7 +55,7 @@ export async function configureGitHubApp(request, reply) {
} }
}); });
if (isDev) { if (isDev) {
return reply.redirect(`http://localhost:3000/sources/${state}`) return reply.redirect(`${getUIUrl()}/sources/${state}`)
} else { } else {
return reply.redirect(`/sources/${state}`) return reply.redirect(`/sources/${state}`)
} }

View File

@ -2,7 +2,7 @@ import axios from "axios";
import cuid from "cuid"; import cuid from "cuid";
import crypto from "crypto"; import crypto from "crypto";
import type { FastifyReply, FastifyRequest } from "fastify"; 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 { checkContainer, removeContainer } from "../../../lib/docker";
import { scheduler } from "../../../lib/scheduler"; import { scheduler } from "../../../lib/scheduler";
import { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers"; 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}`; let domain = `http://${request.hostname}`;
if (fqdn) domain = fqdn; if (fqdn) domain = fqdn;
if (isDev) { if (isDev) {
domain = `http://localhost:3001`; domain = getAPIUrl();
} }
const params = new URLSearchParams({ const params = new URLSearchParams({
client_id: appId, client_id: appId,
@ -28,7 +28,7 @@ export async function configureGitLabApp(request: FastifyRequest, reply: Fastify
}); });
const { data } = await axios.post(`${htmlUrl}/oauth/token`, params) const { data } = await axios.post(`${htmlUrl}/oauth/token`, params)
if (isDev) { 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}`) return reply.redirect(`/webhooks/success?token=${data.access_token}`)
} catch ({ status, message, ...other }) { } catch ({ status, message, ...other }) {

View File

@ -1,4 +1,4 @@
import { browser, dev } from '$app/env'; import { dev } from '$app/env';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
export function getAPIUrl() { export function getAPIUrl() {
@ -9,6 +9,20 @@ export function getAPIUrl() {
} }
return dev ? 'http://localhost:3001' : 'http://localhost:3000'; 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({ async function send({
method, method,
path, path,

View File

@ -8,7 +8,7 @@
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { dev } from '$app/env'; import { dev } from '$app/env';
import { goto } from '$app/navigation'; 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 { t } from '$lib/translations';
import { errorNotification } from '$lib/common'; import { errorNotification } from '$lib/common';
import { appSession } from '$lib/store'; import { appSession } from '$lib/store';
@ -18,7 +18,7 @@
const from = $page.url.searchParams.get('from'); const from = $page.url.searchParams.get('from');
let url = settings?.fqdn ? settings.fqdn : window.location.origin; 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`; const updateDeployKeyIdUrl = `/applications/${id}/configuration/deploykey`;
@ -228,7 +228,7 @@
} }
async function setWebhook(url: any, webhookToken: any) { async function setWebhook(url: any, webhookToken: any) {
const host = dev const host = dev
? 'https://webhook.site/0e5beb2c-4e9b-40e2-a89e-32295e570c21' ? getWebhookUrl('gitlab')
: `${window.location.origin}/webhooks/gitlab/events`; : `${window.location.origin}/webhooks/gitlab/events`;
try { try {
await post( await post(

View File

@ -2,7 +2,7 @@
export let source: any; export let source: any;
export let settings: any; export let settings: any;
import { page } from '$app/stores'; 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 Explainer from '$lib/components/Explainer.svelte';
import { toast } from '@zerodevx/svelte-toast'; import { toast } from '@zerodevx/svelte-toast';
import { t } from '$lib/translations'; import { t } from '$lib/translations';
@ -67,7 +67,7 @@
const { organization, htmlUrl } = source; const { organization, htmlUrl } = source;
const { fqdn } = settings; const { fqdn } = settings;
const host = dev const host = dev
? 'http://localhost:3001' ? getAPIUrl()
: fqdn : fqdn
? fqdn ? fqdn
: `http://${window.location.host}` || ''; : `http://${window.location.host}` || '';
@ -81,7 +81,7 @@
url: host, url: host,
hook_attributes: { hook_attributes: {
url: dev url: dev
? 'https://webhook.site/0e5beb2c-4e9b-40e2-a89e-32295e570c21/events' ? getWebhookUrl('github')
: `${host}/webhooks/github/events` : `${host}/webhooks/github/events`
}, },
redirect_url: `${host}/webhooks/github`, redirect_url: `${host}/webhooks/github`,

View File

@ -4,7 +4,7 @@
import Explainer from '$lib/components/Explainer.svelte'; import Explainer from '$lib/components/Explainer.svelte';
import { page } from '$app/stores'; import { page } from '$app/stores';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { post } from '$lib/api'; import { getAPIUrl, post } from '$lib/api';
import { dev } from '$app/env'; import { dev } from '$app/env';
import CopyPasswordField from '$lib/components/CopyPasswordField.svelte'; import CopyPasswordField from '$lib/components/CopyPasswordField.svelte';
import { toast } from '@zerodevx/svelte-toast'; import { toast } from '@zerodevx/svelte-toast';
@ -17,7 +17,7 @@
let url = settings.fqdn ? settings.fqdn : window.location.origin; let url = settings.fqdn ? settings.fqdn : window.location.origin;
if (dev) { if (dev) {
url = `http://localhost:3001`; url = getAPIUrl();
} }
let loading = false; let loading = false;