Skip to content

Commit

Permalink
Merge pull request #1506 from Esri/bug/workflow-item-ids
Browse files Browse the repository at this point in the history
Item IDs referenced in Workflow project are not being replaced with variables during creation
  • Loading branch information
MikeTschudi authored Sep 25, 2024
2 parents 67632eb + 110b16a commit 5704573
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/common/src/generalHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ export function getSpecifiedWordRegEx(word: string): RegExp {
return new RegExp(`\\b${word}\\b`, "g");
}

/**
* Extracts templated ids from a block of text.
*
* @param text Text to scan for the pattern "{{[0-9A-F]{32}.itemId}}"
* @returns List of ids found in the text; braces and ".itemId" are removed; returns empty list if no matches
*/
export function getTemplatedIds(text: string): string[] {
const idTest: RegExp = /{{[0-9A-F]{32}.itemId}}/gi;

let ids: string[] = [];
const matches = text.match(idTest);
if (matches) {
ids = matches.map((id) => id.replace("{{", "").replace(".itemId}}", ""));
}
return ids;
}

/**
* Converts JSON to a Blob.
*
Expand Down
42 changes: 42 additions & 0 deletions packages/common/test/generalHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,48 @@ describe("Module `generalHelpers`: common utility functions shared across packag
});
});

describe("getTemplatedIds", () => {
it("should return an array of ids", () => {
const o = {
one: {
two: {
three: {
color: "{{7619da54364e42998db8f33617bd2d61.itemId}}",
},
threeB: {
color: "{{389cff847eaa47d29be69ec126cd2c2a.itemId}}",
},
},
other: "value",
},
};

const vals = generalHelpers.getTemplatedIds(JSON.stringify(o));
expect(vals.length).withContext("should return two values").toEqual(2);
expect(vals[0]).withContext("first id").toEqual("7619da54364e42998db8f33617bd2d61");
expect(vals[1]).withContext("second id").toEqual("389cff847eaa47d29be69ec126cd2c2a");
});

it("should handle lack of ids", () => {
const o = {
one: {
two: {
three: {
color: "7619da54364e42998db8f33617bd2d61",
},
threeB: {
color: "389cff847eaa47d29be69ec126cd2c2a",
},
},
other: "value",
},
};

const vals = generalHelpers.getTemplatedIds(JSON.stringify(o));
expect(vals.length).withContext("should return empty list").toEqual(0);
});
});

describe("getUniqueTitle", () => {
it("can return base title if it doesn't exist at path", () => {
const title: string = "The Title";
Expand Down
3 changes: 3 additions & 0 deletions packages/creator/src/createItemTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export function createItemTemplate(
type: itemInfo.type,
url: itemInfo.url,
};
if (!templateDictionary[itemId]) {
templateDictionary[itemId] = itemId;
}

// Save the URL as a symbol
if (itemInfo.url) {
Expand Down
2 changes: 2 additions & 0 deletions packages/creator/src/helpers/add-content-to-solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { createItemTemplate, postProcessFieldReferences } from "../createItemTem
import * as form from "@esri/solution-form";
import { getDataFilesFromTemplates, removeDataFilesFromTemplates } from "./template";
import { notebookProcessor } from "@esri/solution-simple-types";
import * as workflow from "@esri/solution-workflow";

// ------------------------------------------------------------------------------------------------------------------ //

Expand Down Expand Up @@ -179,6 +180,7 @@ export function addContentToSolution(

// test for and update group dependencies and other post-processing
solutionTemplates = await form.postProcessFormItems(solutionTemplates, templateDictionary);
solutionTemplates = workflow.postProcessFormItems(solutionTemplates);
solutionTemplates = _postProcessGroupDependencies(solutionTemplates);
solutionTemplates = postProcessWorkforceTemplates(solutionTemplates);

Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function convertItemToTemplate(
}
});

// Add the templatized configuration to the template
// Add the configuration to the template
itemTemplate.properties.configuration = await common.extractWorkflowFromZipFile(configZip);

return Promise.resolve(itemTemplate);
Expand Down
16 changes: 16 additions & 0 deletions packages/workflow/src/workflowHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ export async function fetchAuxiliaryItems(
return Promise.resolve(auxiliaryItemsResults.results.map((item) => item.id));
}

/**
* Updates the dependencies of the workflow item from its configuration.
*
* @param templates A collection of AGO item templates
* @returns Updated templates list
*/
export function postProcessFormItems(templates: common.IItemTemplate[]): common.IItemTemplate[] {
for (const template of templates) {
if (template.type === "Workflow") {
const ids = common.getTemplatedIds(JSON.stringify(template.properties.configuration));
template.dependencies = common.dedupe([...template.dependencies, ...ids]);
}
}
return templates;
}

/**
* Fetch the data from the new Workflow item and update the templateDictionary for variable replacement
*
Expand Down
51 changes: 51 additions & 0 deletions packages/workflow/test/workflowHelpers.test.ts

Large diffs are not rendered by default.

0 comments on commit 5704573

Please sign in to comment.