Skip to content

Commit 2a80dd0

Browse files
authored
Merge pull request #738 from PROCEED-Labs/fix-editable-version-changes-check
Fix: Changes in Latest Version Check
2 parents 93213cb + 74b00fc commit 2a80dd0

3 files changed

Lines changed: 78 additions & 42 deletions

File tree

src/management-system-v2/app/(dashboard)/[environmentId]/(automation)/executions/deployments-view.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import DeploymentsList from './deployments-list';
99
import { Folder } from '@/lib/data/folder-schema';
1010
import { Process, ProcessMetadata } from '@/lib/data/process-schema';
1111
import { useEnvironment } from '@/components/auth-can';
12-
import { processHasChangesSinceLastVersion } from '@/lib/data/processes';
12+
import { processUnchangedFromBasedOnVersion } from '@/lib/data/processes';
1313
import type { DeployedProcessInfo } from '@/lib/engines/deployment';
1414
import { useRouter } from 'next/navigation';
1515
import { deployProcess as serverDeployProcess } from '@/lib/engines/server-actions';
1616
import { wrapServerCall } from '@/lib/wrap-server-call';
1717
import { SpaceEngine } from '@/lib/engines/machines';
18-
import { userError } from '@/lib/user-error';
18+
import { isUserErrorResponse, userError } from '@/lib/user-error';
1919
import { removeDeployment as serverRemoveDeployment } from '@/lib/engines/server-actions';
2020
import { useQueryClient } from '@tanstack/react-query';
2121

@@ -58,22 +58,31 @@ const DeploymentsView = ({
5858
startCheckingProcessVersion(async () => {
5959
wrapServerCall({
6060
fn: async () => {
61-
const processChangedSinceLastVersion = await processHasChangesSinceLastVersion(
61+
const unchangedVersion = await processUnchangedFromBasedOnVersion(
6262
process.id,
6363
space.spaceId,
6464
);
65-
if (typeof processChangedSinceLastVersion === 'object')
66-
return processChangedSinceLastVersion;
65+
if (isUserErrorResponse(unchangedVersion)) {
66+
return unchangedVersion;
67+
}
6768

68-
let latestVersion = process.versions[0];
69-
for (const version of process.versions)
70-
if (+version.createdOn > +latestVersion.createdOn) latestVersion = version;
69+
let versionToUse = unchangedVersion;
7170

72-
if (!latestVersion) throw userError('Process has no versions').error;
71+
if (!versionToUse) {
72+
let [latestVersion, ...rest] = process.versions;
73+
74+
for (const version of rest) {
75+
if (+version.createdOn > +latestVersion.createdOn) latestVersion = version;
76+
}
77+
78+
versionToUse = latestVersion.id;
79+
}
80+
81+
if (!versionToUse) throw userError('Process has no versions').error;
7382

7483
const res = await serverDeployProcess(
7584
process.id,
76-
latestVersion.id,
85+
versionToUse,
7786
space.spaceId,
7887
'dynamic',
7988
forceEngine,

src/management-system-v2/lib/data/processes.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,23 @@ export const copyProcesses = async (
569569
return copiedProcesses;
570570
};
571571

572-
// TODO: fix: this function doesn't work yet
573-
export const processHasChangesSinceLastVersion = async (processId: string, spaceId: string) => {
572+
/**
573+
* Function that checks if a process' latest version is unchanged from the version it is based on
574+
*
575+
* @returns if unchanged, the version id of the based on version is returned, otherwise undefined is
576+
* returned
577+
**/
578+
export const processUnchangedFromBasedOnVersion = async (processId: string, spaceId: string) => {
574579
const error = await checkValidity(processId, 'view', spaceId);
575580
if (error) return error;
576581

577582
const process = await _getProcess(processId, true);
578583
if (!process) return userError('Process not found', UserErrorType.NotFoundError);
579584

585+
if (!process.versions.length) return;
586+
580587
const bpmnObj = await toBpmnObject(process.bpmn!);
581-
const { versionBasedOn, versionCreatedOn } = await getDefinitionsVersionInformation(bpmnObj);
588+
const { versionBasedOn } = await getDefinitionsVersionInformation(bpmnObj);
582589

583590
const versionedBpmn = await toBpmnXml(bpmnObj);
584591

@@ -589,7 +596,8 @@ export const processHasChangesSinceLastVersion = async (processId: string, space
589596
: undefined;
590597

591598
const versionsAreEqual = basedOnBPMN && (await areVersionsEqual(versionedBpmn, basedOnBPMN));
592-
return !versionsAreEqual;
599+
600+
if (versionsAreEqual) return versionBasedOn;
593601
};
594602

595603
export const createVersion = async (

src/management-system-v2/lib/helpers/processVersioning.ts

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
setScriptTaskData,
1212
getStartFormFileNameMapping,
1313
setStartFormFileName,
14+
getDefinitionsId,
1415
} from '@proceed/bpmn-helper';
1516
import { asyncForEach } from './javascriptHelpers';
1617

@@ -36,39 +37,57 @@ const { diff } = require('bpmn-js-differ');
3637
// should be refactored to reflect the fact this runs on the server now.
3738

3839
export async function areVersionsEqual(bpmn: string, otherBpmn: string) {
39-
const bpmnObj = await toBpmnObject(bpmn);
40-
const otherBpmnObj = await toBpmnObject(otherBpmn);
40+
// compare the bpmn of both versions excluding the differences that would come from one being
41+
// versioned and the other not being versioned
42+
const unversionedOther = await convertToEditableBpmn(otherBpmn);
4143

42-
const {
43-
versionId,
44-
name: versionName,
45-
description: versionDescription,
46-
versionBasedOn,
47-
versionCreatedOn,
48-
} = await getDefinitionsVersionInformation(otherBpmnObj);
49-
50-
if (versionId) {
51-
// check if the two bpmns were the same if they had the same version information
52-
await setDefinitionsVersionInformation(bpmnObj, {
53-
versionId,
54-
versionName,
55-
versionDescription,
56-
versionBasedOn,
57-
versionCreatedOn,
58-
});
44+
const bpmnObj = await toBpmnObject(bpmn);
45+
const unversionedOtherObj = await toBpmnObject(unversionedOther.bpmn);
46+
47+
// compare the two bpmns in a way that is not affected by the ordering of elements in the text
48+
// file
49+
const changes = diff(unversionedOtherObj, bpmnObj);
50+
const hasChanges =
51+
Object.keys(changes._changed).length ||
52+
Object.keys(changes._removed).length ||
53+
Object.keys(changes._added).length ||
54+
Object.keys(changes._layoutChanged).length;
55+
56+
if (hasChanges) return false;
57+
58+
const definitionsId = await getDefinitionsId(bpmnObj);
59+
60+
// compare the assets used in the bpmns
61+
// (we expect that the "same" asset names are referenced since the comparison above would find differences otherwise)
62+
63+
// compare start forms
64+
for (const [versioned, unversioned] of Object.entries(
65+
unversionedOther.changedStartFormTaskFileNames,
66+
)) {
67+
const versionedHtmlForm = await getHtmlForm(definitionsId, versioned);
68+
const unversionedHtmlForm = await getHtmlForm(definitionsId, unversioned);
69+
if (versionedHtmlForm !== unversionedHtmlForm) return false;
70+
}
5971

60-
// compare the two bpmns
61-
const changes = diff(otherBpmnObj, bpmnObj);
62-
const hasChanges =
63-
Object.keys(changes._changed).length ||
64-
Object.keys(changes._removed).length ||
65-
Object.keys(changes._added).length ||
66-
Object.keys(changes._layoutChanged).length;
72+
// compare user tasks
73+
for (const [versioned, unversioned] of Object.entries(
74+
unversionedOther.changedUserTaskFileNames,
75+
)) {
76+
const versionedHtmlForm = await getHtmlForm(definitionsId, versioned);
77+
const unversionedHtmlForm = await getHtmlForm(definitionsId, unversioned);
78+
if (versionedHtmlForm !== unversionedHtmlForm) return false;
79+
}
6780

68-
return !hasChanges;
81+
// compare script tasks
82+
for (const [versioned, unversioned] of Object.entries(
83+
unversionedOther.changedScriptTaskFileNames,
84+
)) {
85+
const versionedScript = await getProcessScriptTaskScript(definitionsId, versioned + '.js');
86+
const unversionedScript = await getProcessScriptTaskScript(definitionsId, unversioned + '.js');
87+
if (versionedScript !== unversionedScript) return false;
6988
}
7089

71-
return false;
90+
return true;
7291
}
7392

7493
export async function convertToEditableBpmn(bpmn: string) {

0 commit comments

Comments
 (0)