feat: gitlab dual branch
This commit is contained in:
@@ -4,7 +4,6 @@ import crypto from "crypto";
|
|||||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||||
import { errorHandler, getAPIUrl, isDev, listSettings, prisma } from "../../../lib/common";
|
import { 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 { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers";
|
import { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers";
|
||||||
|
|
||||||
import type { ConfigureGitLabApp, GitLabEvents } from "./types";
|
import type { ConfigureGitLabApp, GitLabEvents } from "./types";
|
||||||
@@ -40,7 +39,6 @@ export async function configureGitLabApp(request: FastifyRequest<ConfigureGitLab
|
|||||||
export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
||||||
const { object_kind: objectKind, ref, project_id } = request.body
|
const { object_kind: objectKind, ref, project_id } = request.body
|
||||||
try {
|
try {
|
||||||
const buildId = cuid();
|
|
||||||
|
|
||||||
const allowedActions = ['opened', 'reopen', 'close', 'open', 'update'];
|
const allowedActions = ['opened', 'reopen', 'close', 'open', 'update'];
|
||||||
|
|
||||||
@@ -51,48 +49,46 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
|||||||
if (objectKind === 'push') {
|
if (objectKind === 'push') {
|
||||||
const projectId = Number(project_id);
|
const projectId = Number(project_id);
|
||||||
const branch = ref.split('/')[2];
|
const branch = ref.split('/')[2];
|
||||||
const applicationFound = await getApplicationFromDBWebhook(projectId, branch);
|
const applicationsFound = await getApplicationFromDBWebhook(projectId, branch);
|
||||||
if (applicationFound) {
|
if (applicationsFound && applicationsFound.length > 0) {
|
||||||
if (!applicationFound.configHash) {
|
for (const application of applicationsFound) {
|
||||||
const configHash = crypto
|
const buildId = cuid();
|
||||||
.createHash('sha256')
|
if (!application.configHash) {
|
||||||
.update(
|
const configHash = crypto
|
||||||
JSON.stringify({
|
.createHash('sha256')
|
||||||
buildPack: applicationFound.buildPack,
|
.update(
|
||||||
port: applicationFound.port,
|
JSON.stringify({
|
||||||
exposePort: applicationFound.exposePort,
|
buildPack: application.buildPack,
|
||||||
installCommand: applicationFound.installCommand,
|
port: application.port,
|
||||||
buildCommand: applicationFound.buildCommand,
|
exposePort: application.exposePort,
|
||||||
startCommand: applicationFound.startCommand
|
installCommand: application.installCommand,
|
||||||
})
|
buildCommand: application.buildCommand,
|
||||||
)
|
startCommand: application.startCommand
|
||||||
.digest('hex');
|
})
|
||||||
await prisma.application.updateMany({
|
)
|
||||||
where: { branch, projectId },
|
.digest('hex');
|
||||||
data: { configHash }
|
await prisma.application.update({
|
||||||
|
where: { id: application.id },
|
||||||
|
data: { configHash }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await prisma.application.update({
|
||||||
|
where: { id: application.id },
|
||||||
|
data: { updatedAt: new Date() }
|
||||||
|
});
|
||||||
|
await prisma.build.create({
|
||||||
|
data: {
|
||||||
|
id: buildId,
|
||||||
|
applicationId: application.id,
|
||||||
|
destinationDockerId: application.destinationDocker.id,
|
||||||
|
gitSourceId: application.gitSource.id,
|
||||||
|
githubAppId: application.gitSource.githubApp?.id,
|
||||||
|
gitlabAppId: application.gitSource.gitlabApp?.id,
|
||||||
|
status: 'queued',
|
||||||
|
type: 'webhook_commit'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await prisma.application.update({
|
|
||||||
where: { id: applicationFound.id },
|
|
||||||
data: { updatedAt: new Date() }
|
|
||||||
});
|
|
||||||
await prisma.build.create({
|
|
||||||
data: {
|
|
||||||
id: buildId,
|
|
||||||
applicationId: applicationFound.id,
|
|
||||||
destinationDockerId: applicationFound.destinationDocker.id,
|
|
||||||
gitSourceId: applicationFound.gitSource.id,
|
|
||||||
githubAppId: applicationFound.gitSource.githubApp?.id,
|
|
||||||
gitlabAppId: applicationFound.gitSource.gitlabApp?.id,
|
|
||||||
status: 'queued',
|
|
||||||
type: 'webhook_commit'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
message: 'Queued. Thank you!'
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} else if (objectKind === 'merge_request') {
|
} else if (objectKind === 'merge_request') {
|
||||||
const { object_attributes: { work_in_progress: isDraft, action, source_branch: sourceBranch, target_branch: targetBranch, iid: pullmergeRequestId }, project: { id } } = request.body
|
const { object_attributes: { work_in_progress: isDraft, action, source_branch: sourceBranch, target_branch: targetBranch, iid: pullmergeRequestId }, project: { id } } = request.body
|
||||||
@@ -105,64 +101,63 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
|||||||
throw { status: 500, message: 'Draft MR, do nothing.' }
|
throw { status: 500, message: 'Draft MR, do nothing.' }
|
||||||
}
|
}
|
||||||
|
|
||||||
const applicationFound = await getApplicationFromDBWebhook(projectId, targetBranch);
|
const applicationsFound = await getApplicationFromDBWebhook(projectId, targetBranch);
|
||||||
if (applicationFound) {
|
if (applicationsFound && applicationsFound.length > 0) {
|
||||||
if (applicationFound.settings.previews) {
|
for (const application of applicationsFound) {
|
||||||
if (applicationFound.destinationDockerId) {
|
const buildId = cuid();
|
||||||
const isRunning = await checkContainer(
|
if (application.settings.previews) {
|
||||||
{
|
if (application.destinationDockerId) {
|
||||||
dockerId: applicationFound.destinationDocker.id,
|
const isRunning = await checkContainer(
|
||||||
container: applicationFound.id
|
{
|
||||||
|
dockerId: application.destinationDocker.id,
|
||||||
|
container: application.id
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (!isRunning) {
|
||||||
|
throw { status: 500, message: 'Application not running.' }
|
||||||
}
|
}
|
||||||
);
|
|
||||||
if (!isRunning) {
|
|
||||||
throw { status: 500, message: 'Application not running.' }
|
|
||||||
}
|
}
|
||||||
}
|
if (!isDev && application.gitSource.gitlabApp.webhookToken !== webhookToken) {
|
||||||
if (!isDev && applicationFound.gitSource.gitlabApp.webhookToken !== webhookToken) {
|
throw { status: 500, message: 'Invalid webhookToken. Are you doing something nasty?!' }
|
||||||
throw { status: 500, message: 'Invalid webhookToken. Are you doing something nasty?!' }
|
}
|
||||||
}
|
if (
|
||||||
if (
|
action === 'opened' ||
|
||||||
action === 'opened' ||
|
action === 'reopen' ||
|
||||||
action === 'reopen' ||
|
action === 'open' ||
|
||||||
action === 'open' ||
|
action === 'update'
|
||||||
action === 'update'
|
) {
|
||||||
) {
|
await prisma.application.update({
|
||||||
await prisma.application.update({
|
where: { id: application.id },
|
||||||
where: { id: applicationFound.id },
|
data: { updatedAt: new Date() }
|
||||||
data: { updatedAt: new Date() }
|
});
|
||||||
});
|
await prisma.build.create({
|
||||||
await prisma.build.create({
|
data: {
|
||||||
data: {
|
id: buildId,
|
||||||
id: buildId,
|
pullmergeRequestId,
|
||||||
pullmergeRequestId,
|
sourceBranch,
|
||||||
sourceBranch,
|
applicationId: application.id,
|
||||||
applicationId: applicationFound.id,
|
destinationDockerId: application.destinationDocker.id,
|
||||||
destinationDockerId: applicationFound.destinationDocker.id,
|
gitSourceId: application.gitSource.id,
|
||||||
gitSourceId: applicationFound.gitSource.id,
|
githubAppId: application.gitSource.githubApp?.id,
|
||||||
githubAppId: applicationFound.gitSource.githubApp?.id,
|
gitlabAppId: application.gitSource.gitlabApp?.id,
|
||||||
gitlabAppId: applicationFound.gitSource.gitlabApp?.id,
|
status: 'queued',
|
||||||
status: 'queued',
|
type: 'webhook_mr'
|
||||||
type: 'webhook_mr'
|
}
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
message: 'Queued. Thank you!'
|
||||||
|
};
|
||||||
|
} else if (action === 'close') {
|
||||||
|
if (application.destinationDockerId) {
|
||||||
|
const id = `${application.id}-${pullmergeRequestId}`;
|
||||||
|
await removeContainer({ id, dockerId: application.destinationDocker.id });
|
||||||
}
|
}
|
||||||
});
|
|
||||||
return {
|
|
||||||
message: 'Queued. Thank you!'
|
|
||||||
};
|
|
||||||
} else if (action === 'close') {
|
|
||||||
if (applicationFound.destinationDockerId) {
|
|
||||||
const id = `${applicationFound.id}-${pullmergeRequestId}`;
|
|
||||||
await removeContainer({ id, dockerId: applicationFound.destinationDocker.id });
|
|
||||||
}
|
}
|
||||||
return {
|
|
||||||
message: 'Removed preview. Thank you!'
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw { status: 500, message: 'Merge request previews are not enabled.' }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw { status: 500, message: 'Not handled event.' }
|
|
||||||
} catch ({ status, message }) {
|
} catch ({ status, message }) {
|
||||||
return errorHandler({ status, message })
|
return errorHandler({ status, message })
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user