From 0c24134ac22b70c0833eefce73f81332f2c53c63 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 18 Aug 2022 11:53:42 +0200 Subject: [PATCH 1/6] feat: import public repos (wip) --- .../migration.sql | 42 ++++++ apps/api/prisma/schema.prisma | 22 ++-- apps/api/prisma/seed.js | 28 ++++ apps/api/src/lib/importers/github.ts | 1 - .../routes/api/v1/applications/handlers.ts | 39 ++++-- .../src/routes/api/v1/applications/types.ts | 2 +- .../configuration/_PublicRepository.svelte | 121 ++++++++++++++++++ .../[id]/configuration/buildpack.svelte | 3 +- .../[id]/configuration/repository.svelte | 1 + .../[id]/configuration/source.svelte | 5 + 10 files changed, 238 insertions(+), 26 deletions(-) create mode 100644 apps/api/prisma/migrations/20220818093615_public_repositories/migration.sql create mode 100644 apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte diff --git a/apps/api/prisma/migrations/20220818093615_public_repositories/migration.sql b/apps/api/prisma/migrations/20220818093615_public_repositories/migration.sql new file mode 100644 index 000000000..7f08c29fd --- /dev/null +++ b/apps/api/prisma/migrations/20220818093615_public_repositories/migration.sql @@ -0,0 +1,42 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_GitSource" ( + "id" TEXT NOT NULL PRIMARY KEY, + "name" TEXT NOT NULL, + "forPublic" BOOLEAN NOT NULL DEFAULT false, + "type" TEXT, + "apiUrl" TEXT, + "htmlUrl" TEXT, + "customPort" INTEGER NOT NULL DEFAULT 22, + "organization" TEXT, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "githubAppId" TEXT, + "gitlabAppId" TEXT, + CONSTRAINT "GitSource_githubAppId_fkey" FOREIGN KEY ("githubAppId") REFERENCES "GithubApp" ("id") ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT "GitSource_gitlabAppId_fkey" FOREIGN KEY ("gitlabAppId") REFERENCES "GitlabApp" ("id") ON DELETE SET NULL ON UPDATE CASCADE +); +INSERT INTO "new_GitSource" ("apiUrl", "createdAt", "customPort", "githubAppId", "gitlabAppId", "htmlUrl", "id", "name", "organization", "type", "updatedAt") SELECT "apiUrl", "createdAt", "customPort", "githubAppId", "gitlabAppId", "htmlUrl", "id", "name", "organization", "type", "updatedAt" FROM "GitSource"; +DROP TABLE "GitSource"; +ALTER TABLE "new_GitSource" RENAME TO "GitSource"; +CREATE UNIQUE INDEX "GitSource_githubAppId_key" ON "GitSource"("githubAppId"); +CREATE UNIQUE INDEX "GitSource_gitlabAppId_key" ON "GitSource"("gitlabAppId"); +CREATE TABLE "new_ApplicationSettings" ( + "id" TEXT NOT NULL PRIMARY KEY, + "applicationId" TEXT NOT NULL, + "dualCerts" BOOLEAN NOT NULL DEFAULT false, + "debug" BOOLEAN NOT NULL DEFAULT false, + "previews" BOOLEAN NOT NULL DEFAULT false, + "autodeploy" BOOLEAN NOT NULL DEFAULT true, + "isBot" BOOLEAN NOT NULL DEFAULT false, + "isPublicRepository" BOOLEAN NOT NULL DEFAULT false, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + CONSTRAINT "ApplicationSettings_applicationId_fkey" FOREIGN KEY ("applicationId") REFERENCES "Application" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_ApplicationSettings" ("applicationId", "autodeploy", "createdAt", "debug", "dualCerts", "id", "isBot", "previews", "updatedAt") SELECT "applicationId", "autodeploy", "createdAt", "debug", "dualCerts", "id", "isBot", "previews", "updatedAt" FROM "ApplicationSettings"; +DROP TABLE "ApplicationSettings"; +ALTER TABLE "new_ApplicationSettings" RENAME TO "ApplicationSettings"; +CREATE UNIQUE INDEX "ApplicationSettings_applicationId_key" ON "ApplicationSettings"("applicationId"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 695478cef..6af0c4535 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -119,16 +119,17 @@ model Application { } model ApplicationSettings { - id String @id @default(cuid()) - applicationId String @unique - dualCerts Boolean @default(false) - debug Boolean @default(false) - previews Boolean @default(false) - autodeploy Boolean @default(true) - isBot Boolean @default(false) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - application Application @relation(fields: [applicationId], references: [id]) + id String @id @default(cuid()) + applicationId String @unique + dualCerts Boolean @default(false) + debug Boolean @default(false) + previews Boolean @default(false) + autodeploy Boolean @default(true) + isBot Boolean @default(false) + isPublicRepository Boolean @default(false) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + application Application @relation(fields: [applicationId], references: [id]) } model ApplicationPersistentStorage { @@ -238,6 +239,7 @@ model SshKey { model GitSource { id String @id @default(cuid()) name String + forPublic Boolean @default(false) type String? apiUrl String? htmlUrl String? diff --git a/apps/api/prisma/seed.js b/apps/api/prisma/seed.js index 96b55b105..9fa55136f 100644 --- a/apps/api/prisma/seed.js +++ b/apps/api/prisma/seed.js @@ -66,6 +66,34 @@ async function main() { } }); } + const github = await prisma.gitSource.findFirst({ + where: { htmlUrl: 'https://github.com', forPublic: true } + }); + const gitlab = await prisma.gitSource.findFirst({ + where: { htmlUrl: 'https://gitlab.com', forPublic: true } + }); + if (!github) { + await prisma.gitSource.create({ + data: { + apiUrl: 'https://api.github.com', + htmlUrl: 'https://github.com', + forPublic: true, + name: 'Github Public', + type: 'github' + } + }); + } + if (!gitlab) { + await prisma.gitSource.create({ + data: { + apiUrl: 'https://gitlab.com/api/v4', + htmlUrl: 'https://gitlab.com', + forPublic: true, + name: 'Gitlab Public', + type: 'gitlab' + } + }); + } } main() .catch((e) => { diff --git a/apps/api/src/lib/importers/github.ts b/apps/api/src/lib/importers/github.ts index bac460eca..afe1483a3 100644 --- a/apps/api/src/lib/importers/github.ts +++ b/apps/api/src/lib/importers/github.ts @@ -31,7 +31,6 @@ export default async function ({ const body = await prisma.githubApp.findUnique({ where: { id: githubAppId } }); if (body.privateKey) body.privateKey = decrypt(body.privateKey); const { privateKey, appId, installationId } = body - const githubPrivateKey = privateKey.replace(/\\n/g, '\n').replace(/"/g, ''); const payload = { diff --git a/apps/api/src/routes/api/v1/applications/handlers.ts b/apps/api/src/routes/api/v1/applications/handlers.ts index 5f0f2aa2d..0e2596f79 100644 --- a/apps/api/src/routes/api/v1/applications/handlers.ts +++ b/apps/api/src/routes/api/v1/applications/handlers.ts @@ -499,11 +499,21 @@ export async function deployApplication(request: FastifyRequest, reply: FastifyReply) { try { const { id } = request.params - const { gitSourceId } = request.body - await prisma.application.update({ - where: { id }, - data: { gitSource: { connect: { id: gitSourceId } } } - }); + const { gitSourceId, forPublic, type } = request.body + console.log({ id, gitSourceId, forPublic, type }) + if (forPublic) { + const publicGit = await prisma.gitSource.findFirst({ where: { type, forPublic } }); + await prisma.application.update({ + where: { id }, + data: { gitSource: { connect: { id: publicGit.id } } } + }); + } else { + await prisma.application.update({ + where: { id }, + data: { gitSource: { connect: { id: gitSourceId } } } + }); + } + return reply.code(201).send() } catch ({ status, message }) { return errorHandler({ status, message }) @@ -557,7 +567,7 @@ export async function checkRepository(request: FastifyRequest) export async function saveRepository(request, reply) { try { const { id } = request.params - let { repository, branch, projectId, autodeploy, webhookToken } = request.body + let { repository, branch, projectId, autodeploy, webhookToken, isPublicRepository = false } = request.body repository = repository.toLowerCase(); branch = branch.toLowerCase(); @@ -565,17 +575,19 @@ export async function saveRepository(request, reply) { if (webhookToken) { await prisma.application.update({ where: { id }, - data: { repository, branch, projectId, gitSource: { update: { gitlabApp: { update: { webhookToken: webhookToken ? webhookToken : undefined } } } }, settings: { update: { autodeploy } } } + data: { repository, branch, projectId, gitSource: { update: { gitlabApp: { update: { webhookToken: webhookToken ? webhookToken : undefined } } } }, settings: { update: { autodeploy, isPublicRepository } } } }); } else { await prisma.application.update({ where: { id }, - data: { repository, branch, projectId, settings: { update: { autodeploy } } } + data: { repository, branch, projectId, settings: { update: { autodeploy, isPublicRepository } } } }); } - const isDouble = await checkDoubleBranch(branch, projectId); - if (isDouble) { - await prisma.applicationSettings.updateMany({ where: { application: { branch, projectId } }, data: { autodeploy: false } }) + if (!isPublicRepository) { + const isDouble = await checkDoubleBranch(branch, projectId); + if (isDouble) { + await prisma.applicationSettings.updateMany({ where: { application: { branch, projectId } }, data: { autodeploy: false, isPublicRepository } }) + } } return reply.code(201).send() } catch ({ status, message }) { @@ -607,7 +619,8 @@ export async function getBuildPack(request) { projectId: application.projectId, repository: application.repository, branch: application.branch, - apiUrl: application.gitSource.apiUrl + apiUrl: application.gitSource.apiUrl, + isPublicRepository: application.settings.isPublicRepository } } catch ({ status, message }) { return errorHandler({ status, message }) @@ -658,7 +671,7 @@ export async function saveSecret(request: FastifyRequest, reply: Fas throw { status: 500, message: `Secret ${name} already exists.` } } else { value = encrypt(value.trim()); - console.log({value}) + console.log({ value }) await prisma.secret.create({ data: { name, value, isBuildSecret, isPRMRSecret, application: { connect: { id } } } }); diff --git a/apps/api/src/routes/api/v1/applications/types.ts b/apps/api/src/routes/api/v1/applications/types.ts index 1f06a37a8..f404e9bcb 100644 --- a/apps/api/src/routes/api/v1/applications/types.ts +++ b/apps/api/src/routes/api/v1/applications/types.ts @@ -50,7 +50,7 @@ export interface GetImages { Body: { buildPack: string, deploymentType: string } } export interface SaveApplicationSource extends OnlyId { - Body: { gitSourceId: string } + Body: { gitSourceId?: string | null, forPublic?: boolean, type?: string } } export interface CheckRepository extends OnlyId { Querystring: { repository: string, branch: string } diff --git a/apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte b/apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte new file mode 100644 index 000000000..a2543e381 --- /dev/null +++ b/apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte @@ -0,0 +1,121 @@ + + +
Public repository link
+ +
+ + + {#if branchSelectOptions.length > 0} +
+ + {#if isDisabled || application.settings.isPublicRepository} + {:else} {$t('application.git_repository')} - {#if isDisabled} - + {#if isDisabled || application.settings.isPublicRepository} + {:else} changeSettings('isBot')} title="Is your application a bot?" description="You can deploy applications without domains.
They will listen on IP:EXPOSEDPORT instead.

Useful to host Twitch bots." + disabled={$status.application.isRunning} />
{#if !isBot} @@ -770,15 +778,17 @@
{$t('application.features')}
-
- changeSettings('autodeploy')} - title={$t('application.enable_automatic_deployment')} - description={$t('application.enable_auto_deploy_webhooks')} - /> -
+ {#if !application.settings.isPublicRepository} +
+ changeSettings('autodeploy')} + title={$t('application.enable_automatic_deployment')} + description={$t('application.enable_auto_deploy_webhooks')} + /> +
+ {/if} {#if !application.settings.isBot}
-
- -
+ @@ -109,4 +107,7 @@
+
+ +
From 0922fd66a4807cd5d9688cdfcce207ad3f850469 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 18 Aug 2022 16:33:32 +0200 Subject: [PATCH 3/6] feat: force rebuild + env.PORT for port + public repo build --- apps/api/src/jobs/deployApplication.ts | 8 +- apps/api/src/lib/common.ts | 2 - apps/api/src/lib/docker.ts | 1 - .../routes/api/v1/applications/handlers.ts | 8 +- .../src/routes/api/v1/applications/types.ts | 5 +- .../routes/applications/[id]/__layout.svelte | 11 +- .../[id]/configuration/_BuildPack.svelte | 2 +- .../configuration/_PublicRepository.svelte | 209 +++++++++++----- .../[id]/configuration/buildpack.svelte | 1 - .../[id]/configuration/source.svelte | 231 +++++++++--------- .../src/routes/applications/[id]/index.svelte | 5 +- 11 files changed, 286 insertions(+), 197 deletions(-) diff --git a/apps/api/src/jobs/deployApplication.ts b/apps/api/src/jobs/deployApplication.ts index 4db0d5e26..9259e3976 100644 --- a/apps/api/src/jobs/deployApplication.ts +++ b/apps/api/src/jobs/deployApplication.ts @@ -56,6 +56,7 @@ import * as buildpacks from '../lib/buildPacks'; baseImage, baseBuildImage, deploymentType, + forceRebuild } = message let { branch, @@ -174,7 +175,6 @@ import * as buildpacks from '../lib/buildPacks'; if (!pullmergeRequestId) { - if (configHash !== currentHash) { deployNeeded = true; if (configHash) { @@ -198,6 +198,8 @@ import * as buildpacks from '../lib/buildPacks'; // } await copyBaseConfigurationFiles(buildPack, workdir, buildId, applicationId, baseImage); + + if (forceRebuild) deployNeeded = true if (!imageFound || deployNeeded) { // if (true) { if (buildpacks[buildPack]) @@ -248,7 +250,9 @@ import * as buildpacks from '../lib/buildPacks'; } catch (error) { // } - const envs = []; + const envs = [ + `PORT=${port}` + ]; if (secrets.length > 0) { secrets.forEach((secret) => { if (pullmergeRequestId) { diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 78a23eb2b..9533b9473 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -1189,9 +1189,7 @@ export async function checkExposedPort({ id, configuredPort, exposePort, dockerI } } } else { - const availablePort = await getFreeExposedPort(id, exposePort, dockerId, remoteIpAddress); -console.log(availablePort, exposePort) if (availablePort.toString() !== exposePort.toString()) { throw { status: 500, message: `Port ${exposePort} is already in use.` } } diff --git a/apps/api/src/lib/docker.ts b/apps/api/src/lib/docker.ts index aa6a29cfd..c1ae977e5 100644 --- a/apps/api/src/lib/docker.ts +++ b/apps/api/src/lib/docker.ts @@ -71,7 +71,6 @@ export async function removeContainer({ }): Promise { try { const { stdout } = await executeDockerCmd({ dockerId, command: `docker inspect --format '{{json .State}}' ${id}` }) - console.log(id) if (JSON.parse(stdout).Running) { await executeDockerCmd({ dockerId, command: `docker stop -t 0 ${id}` }) await executeDockerCmd({ dockerId, command: `docker rm ${id}` }) diff --git a/apps/api/src/routes/api/v1/applications/handlers.ts b/apps/api/src/routes/api/v1/applications/handlers.ts index 7ddeb6b1c..57da91478 100644 --- a/apps/api/src/routes/api/v1/applications/handlers.ts +++ b/apps/api/src/routes/api/v1/applications/handlers.ts @@ -428,7 +428,7 @@ export async function deployApplication(request: FastifyRequest { @@ -256,7 +255,7 @@ -
+ handleDeploySubmit(true)}> - {#if branchSelectOptions.length > 0} -
- + {#if branchSelectOptions.length > 0} +
+ +
{/if}
From b76caabd32fc65dad471a3bda1359c779b910e1e Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 18 Aug 2022 16:37:46 +0200 Subject: [PATCH 4/6] examples --- .../[id]/configuration/_PublicRepository.svelte | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte b/apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte index b76c7da53..efad2263d 100644 --- a/apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte +++ b/apps/ui/src/routes/applications/[id]/configuration/_PublicRepository.svelte @@ -10,7 +10,7 @@ const { id } = $page.params; - let publicRepositoryLink: string = 'https://gitlab.com/aleveha/fastify-example'; + let publicRepositoryLink: string; let projectId: number; let repositoryName: string; let branchName: string; @@ -167,11 +167,12 @@ >
- + {#if branchSelectOptions.length > 0}
+ {#if branchSelectOptions.length > 0}