Skip to content

Commit

Permalink
fix(ui): prevent crash on invalid yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo authored and Skraye committed Jan 12, 2024
1 parent c4097f5 commit 70a3e46
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions ui/src/utils/yamlUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@ export default class YamlUtils {
static updateMetadata(source, metadata) {
// TODO: check how to keep comments
const yamlDoc = yaml.parseDocument(source);

if (!yamlDoc.contents.items) {
return source;
}

for (const property in metadata) {
if (yamlDoc.contents.items.find(item => item.key.value === property)) {
yamlDoc.contents.items.find(item => item.key.value === property).value = metadata[property];
Expand All @@ -508,7 +513,12 @@ export default class YamlUtils {
static cleanMetadata(source) {
// Reorder and remove empty metadata
const yamlDoc = yaml.parseDocument(source);
const order = ["id", "namespace", "description", "labels", "inputs", "variables", "tasks", "triggers", "errors", "taskDefaults"];

if (!yamlDoc.contents.items) {
return source;
}

const order = ["id", "namespace", "description", "labels", "inputs", "variables", "tasks", "triggers", "errors", "taskDefaults", "concurrency"];
const updatedItems = [];
for (const prop of order) {
const item = yamlDoc.contents.items.find(e => e.key.value === prop);
Expand All @@ -532,12 +542,23 @@ export default class YamlUtils {
}

static flowHaveTasks(source) {
const tasks = yaml.parseDocument(source).contents.items.find(item => item.key.value === "tasks");
return tasks && tasks.value.items && tasks.value.items.length >= 1;
const yamlDoc = yaml.parseDocument(source);

if (!yamlDoc.contents.items) {
return false;
}

const tasks = yamlDoc.contents.items.find(item => item.key.value === "tasks");
return tasks?.value?.items?.length >= 1;
}

static deleteMetadata(source, metadata) {
const yamlDoc = yaml.parseDocument(source);

if (!yamlDoc.contents.items) {
return source;
}

const item = yamlDoc.contents.items.find(e => e.key.value === metadata);
if (item) {
yamlDoc.contents.items.splice(yamlDoc.contents.items.indexOf(item), 1);
Expand Down

0 comments on commit 70a3e46

Please sign in to comment.