Fix use as draft (#9718)

- remove delete serverless function when archiving workflow version
- update copy serverless function to reset serverless function to old
version
- remove createNewWorkflowVersion and use createDraftFromWorkflowVersion
- fix step update issue and optimistic rendering when generate draft
from active version
This commit is contained in:
martmull
2025-01-21 15:44:52 +01:00
committed by GitHub
parent d8815d7ebf
commit ed7c48e12a
22 changed files with 207 additions and 309 deletions

View File

@ -58,6 +58,29 @@ export class ServerlessFunctionService {
return this.serverlessFunctionRepository.findBy(where);
}
async findOneOrFail({
workspaceId,
id,
}: {
workspaceId: string;
id: string;
}) {
const serverlessFunction =
await this.serverlessFunctionRepository.findOneBy({
id,
workspaceId,
});
if (!serverlessFunction) {
throw new ServerlessFunctionException(
`Function does not exist`,
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_NOT_FOUND,
);
}
return serverlessFunction;
}
async hasServerlessFunctionPublishedVersion(serverlessFunctionId: string) {
return await this.serverlessFunctionRepository.exists({
where: {
@ -72,18 +95,10 @@ export class ServerlessFunctionService {
id: string,
version: string,
): Promise<{ [filePath: string]: string } | undefined> {
const serverlessFunction =
await this.serverlessFunctionRepository.findOneBy({
id,
workspaceId,
});
if (!serverlessFunction) {
throw new ServerlessFunctionException(
`Function does not exist`,
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_NOT_FOUND,
);
}
const serverlessFunction = await this.findOneOrFail({
id,
workspaceId,
});
try {
const folderPath = getServerlessFolder({
@ -121,19 +136,10 @@ export class ServerlessFunctionService {
): Promise<ServerlessExecuteResult> {
await this.throttleExecution(workspaceId);
const functionToExecute = await this.serverlessFunctionRepository.findOneBy(
{
id,
workspaceId,
},
);
if (!functionToExecute) {
throw new ServerlessFunctionException(
`Function does not exist`,
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_NOT_FOUND,
);
}
const functionToExecute = await this.findOneOrFail({
id,
workspaceId,
});
const resultServerlessFunction = await this.serverlessService.execute(
functionToExecute,
@ -163,15 +169,10 @@ export class ServerlessFunctionService {
}
async publishOneServerlessFunction(id: string, workspaceId: string) {
const existingServerlessFunction =
await this.serverlessFunctionRepository.findOneBy({ id, workspaceId });
if (!existingServerlessFunction) {
throw new ServerlessFunctionException(
`Function does not exist`,
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_NOT_FOUND,
);
}
const existingServerlessFunction = await this.findOneOrFail({
id,
workspaceId,
});
if (isDefined(existingServerlessFunction.latestVersion)) {
const latestCode = await this.getServerlessFunctionSourceCode(
@ -223,18 +224,10 @@ export class ServerlessFunctionService {
workspaceId: string;
isHardDeletion?: boolean;
}) {
const existingServerlessFunction =
await this.serverlessFunctionRepository.findOneBy({
id,
workspaceId,
});
if (!existingServerlessFunction) {
throw new ServerlessFunctionException(
`Function does not exist`,
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_NOT_FOUND,
);
}
const existingServerlessFunction = await this.findOneOrFail({
id,
workspaceId,
});
if (isHardDeletion) {
await this.serverlessFunctionRepository.delete(id);
@ -254,18 +247,10 @@ export class ServerlessFunctionService {
serverlessFunctionInput: UpdateServerlessFunctionInput,
workspaceId: string,
) {
const existingServerlessFunction =
await this.serverlessFunctionRepository.findOneBy({
id: serverlessFunctionInput.id,
workspaceId,
});
if (!existingServerlessFunction) {
throw new ServerlessFunctionException(
`Function does not exist`,
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_NOT_FOUND,
);
}
const existingServerlessFunction = await this.findOneOrFail({
id: serverlessFunctionInput.id,
workspaceId,
});
await this.serverlessFunctionRepository.update(
existingServerlessFunction.id,
@ -367,67 +352,40 @@ export class ServerlessFunctionService {
});
}
async copyOneServerlessFunction({
serverlessFunctionToCopyId,
serverlessFunctionToCopyVersion,
async usePublishedVersionAsDraft({
id,
version,
workspaceId,
}: {
serverlessFunctionToCopyId: string;
serverlessFunctionToCopyVersion: string;
id: string;
version: string;
workspaceId: string;
}) {
const serverlessFunctionToCopy =
await this.serverlessFunctionRepository.findOneBy({
workspaceId,
id: serverlessFunctionToCopyId,
latestVersion: serverlessFunctionToCopyVersion,
});
if (!serverlessFunctionToCopy) {
throw new ServerlessFunctionException(
'Function does not exist',
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_NOT_FOUND,
);
}
const serverlessFunctionToCreate = this.serverlessFunctionRepository.create(
{
name: serverlessFunctionToCopy?.name,
description: serverlessFunctionToCopy?.description,
timeoutSeconds: serverlessFunctionToCopy?.timeoutSeconds,
workspaceId,
layerVersion: LAST_LAYER_VERSION,
},
);
const copiedServerlessFunction =
await this.serverlessFunctionRepository.save(serverlessFunctionToCreate);
const serverlessFunctionToCopyFileFolder = getServerlessFolder({
serverlessFunction: serverlessFunctionToCopy,
version: 'latest',
});
const copiedServerlessFunctionFileFolder = getServerlessFolder({
serverlessFunction: copiedServerlessFunction,
version: 'draft',
const serverlessFunction = await this.findOneOrFail({
id,
workspaceId,
});
await this.fileStorageService.copy({
from: {
folderPath: serverlessFunctionToCopyFileFolder,
folderPath: getServerlessFolder({
serverlessFunction: serverlessFunction,
version,
}),
},
to: {
folderPath: copiedServerlessFunctionFileFolder,
folderPath: getServerlessFolder({
serverlessFunction: serverlessFunction,
version: 'draft',
}),
},
});
await this.buildServerlessFunction({
serverlessFunctionId: copiedServerlessFunction.id,
serverlessFunctionId: id,
serverlessFunctionVersion: 'draft',
workspaceId,
});
return copiedServerlessFunction;
}
private async throttleExecution(workspaceId: string) {