Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update labels in Markdown editor when workflow labels change. #17863

Merged
merged 9 commits into from
Apr 2, 2024
36 changes: 36 additions & 0 deletions client/src/components/Markdown/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ describe("parse.ts", () => {
const args = getArgs("job_metrics(job_id=THISFAKEID)");
expect(args.name).toBe("job_metrics");
});

it("parses labels spaces at the end with single quotes", () => {
const args = getArgs("job_metrics(step=' fakestepname ')");
expect(args.name).toBe("job_metrics");
expect(args.args.step).toBe(" fakestepname ");
});

it("parses labels spaces at the end with double quotes", () => {
const args = getArgs('job_metrics(step=" fakestepname ")');
expect(args.name).toBe("job_metrics");
expect(args.args.step).toBe(" fakestepname ");
});
});

describe("splitMarkdown", () => {
Expand All @@ -19,6 +31,12 @@ describe("parse.ts", () => {
expect(sections.length).toBe(2);
expect(sections[0].content).toBe("\n");
});

it("should parse labels with leading spaces", () => {
const { sections } = splitMarkdown("\n```galaxy\njob_metrics(step='THISFAKEID ')\n```", true);
expect(sections.length).toBe(2);
expect(sections[0].content).toBe("\n");
});
});

describe("replaceLabel", () => {
Expand Down Expand Up @@ -103,6 +121,15 @@ describe("parse.ts", () => {
expect(result).toBe(output);
});

it("should work with single quotes for labels and spaces in the quotes including end", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output='from this ', title=dog)\n```\n";
const output =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output='to thatx ', title=dog)\n```\n";
const result = replaceLabel(input, "output", "from this ", "to thatx ");
expect(result).toBe(output);
});

it("should add quotes when refactoring to labels with spaces", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output=fromthis, title=dog)\n```\n";
Expand All @@ -112,6 +139,15 @@ describe("parse.ts", () => {
expect(result).toBe(output);
});

it("should add quotes when refactoring to labels with spaces including end space", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output=fromthis, title=dog)\n```\n";
const output =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output='to that ', title=dog)\n```\n";
const result = replaceLabel(input, "output", "fromthis", "to that ");
expect(result).toBe(output);
});

it("should leave non-arguments alone", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(title='cow from farm', output=from)\n```\n";
Expand Down
17 changes: 14 additions & 3 deletions client/src/components/Markdown/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const FUNCTION_ARGUMENT_VALUE_REGEX = `\\s*(?:[\\w_\\-]+|\\"[^\\"]+\\"|\\'[^\\']+\\')\\s*`;
const FUNCTION_ARGUMENT_VALUE_TO_VALUE_REGEX = `\\s*(?:\\"(?<unquoted>[^\\"]+)\\"|\\'(?<squoted>[^\\']+)\\'|(?<dquoted>[\\w_\\-]+))\\s*`;
const FUNCTION_ARGUMENT_REGEX = `\\s*[\\w\\|]+\\s*=` + FUNCTION_ARGUMENT_VALUE_REGEX;
const FUNCTION_CALL_LINE = `\\s*(\\w+)\\s*\\(\\s*(?:(${FUNCTION_ARGUMENT_REGEX})(,${FUNCTION_ARGUMENT_REGEX})*)?\\s*\\)\\s*`;
const FUNCTION_CALL_LINE_TEMPLATE = new RegExp(FUNCTION_CALL_LINE, "m");
Expand Down Expand Up @@ -87,6 +88,7 @@ export function replaceLabel(
const match = incomingContent.match(argRexExp);
if (match) {
const firstMatch = match[0];
// TODO: handle whitespace more broadly here...
if (escapedToLabel.indexOf(" ") >= 0) {
const quoteChar = getQuoteChar(firstMatch);
escapedToLabel = `${quoteChar}${escapedToLabel}${quoteChar}`;
Expand Down Expand Up @@ -135,11 +137,20 @@ export function getArgs(content: string): GalaxyDirectiveSection {
}
const arguments_str = function_arguments[i]?.toString().replace(/,/g, "").trim();
if (arguments_str) {
const [key, val] = arguments_str.split("=");
if (key == undefined || val == undefined) {
const [keyStr, valStr] = arguments_str.split("=");
if (keyStr == undefined || keyStr == undefined) {
jmchilton marked this conversation as resolved.
Show resolved Hide resolved
throw Error("Failed to parse galaxy directive");
}
args[key.trim()] = val.replace(/['"]+/g, "").trim();
const key = keyStr.trim();
let val: string = valStr?.trim() ?? "";
if (val) {
const strippedValueMatch = val.match(FUNCTION_ARGUMENT_VALUE_TO_VALUE_REGEX);
const groups = strippedValueMatch?.groups;
if (groups) {
val = groups.unquoted ?? groups.squoted ?? groups.dquoted ?? val;
}
}
args[key] = val;
}
}
return {
Expand Down
Loading