Skip to content

Commit

Permalink
Merge pull request #339 from thefrontside/tm/allow-path
Browse files Browse the repository at this point in the history
Allow passing a path to YAML actions
  • Loading branch information
taras authored Aug 31, 2023
2 parents c3cdd2e + 6f6756d commit b4e907e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .changeset/swift-toys-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'backend': patch
'@frontside/scaffolder-yaml-actions': patch
---

Allowed passing a path instead of url in YAML actions
4 changes: 2 additions & 2 deletions plugins/scaffolder-yaml-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Set the contents of a YAML document.

**Input:**

* `url` [*string*] - URL of the YAML document [example: https://github.com/backstage/backstage/tree/master/catalog-info.yaml]
* `url` [*string*] - URL of the YAML file or file system path [example: https://github.com/backstage/backstage/tree/master/catalog-info.yaml]
* `path` [*string*] - the path of the property to set [example: metadata.name]
* `value` [*string* | *number* | *null] - value to set
* entityRef [string, optional] - entity ref of entity to update in case YAML file contains multiple documents
Expand All @@ -81,7 +81,7 @@ Append a value to a collection in a YAML document.

**Input:**

* `url` [*string*] - URL of the YAML document [example: https://github.com/backstage/backstage/tree/master/catalog-info.yaml]
* `url` [*string*] - URL of the YAML file or file system path (relative or absolute) [example: https://github.com/backstage/backstage/tree/master/catalog-info.yaml]
* `path` [*string*] - the path of the property to update [example: metadata.tags]
* `value` [*string* | *number* | *null* | *Record<string, any>*] - value to append
* entityRef [string, optional] - entity ref of entity to update in case YAML file contains multiple documents
Expand Down
1 change: 1 addition & 0 deletions plugins/scaffolder-yaml-actions/src/actions/_helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isUrl = (maybeUrl: string) => /^(?:[a-z+]+:)?\/\//.test(maybeUrl)
40 changes: 25 additions & 15 deletions plugins/scaffolder-yaml-actions/src/actions/append.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import _path from 'path';
import { Logger } from 'winston';
import { z } from 'zod';
import { append } from '../operations/append';
import { isUrl } from './_helpers';

interface CreateYamAppendActionOptions {
logger: Logger;
Expand All @@ -18,7 +19,7 @@ interface CreateYamAppendActionOptions {
}

const InputSchema = z.object({
url: z.string().describe('URL of the YAML file to update'),
url: z.string().describe('URL of the YAML file to set or file system path (relative or absolute)'),
path: z.string().describe('The path of the collection to append to'),
value: z
.union([z.string(), z.number(), z.null(), z.record(z.any())])
Expand Down Expand Up @@ -52,18 +53,28 @@ export function createYamlAppendAction({
async handler(ctx) {
const { url, entityRef, path, value } = ctx.input;

const { filepath, resource, owner, name } = parseGitUrl(url);

const sourceFilepath = resolveSafeChildPath(ctx.workspacePath, filepath);

// This should be removed in favour of using fetch:plain:file
// FIX: blocked by https://github.com/backstage/backstage/issues/17072
await fetchAction.handler({
...ctx,
input: {
url: _path.dirname(ctx.input.url),
},
});
let sourceFilepath;
if (isUrl(url)) {
const { filepath, resource, owner, name } = parseGitUrl(url);

// This should be removed in favour of using fetch:plain:file
// FIX: blocked by https://github.com/backstage/backstage/issues/17072
await fetchAction.handler({
...ctx,
input: {
url: _path.dirname(ctx.input.url),
},
});

sourceFilepath = resolveSafeChildPath(ctx.workspacePath, filepath);

ctx.output('repoUrl', `${resource}?repo=${name}&owner=${owner}`);
ctx.output('filePath', filepath);
} else if (_path.isAbsolute(url)) {
sourceFilepath = url;
} else {
sourceFilepath = resolveSafeChildPath(ctx.workspacePath, url);
}

let content;
try {
Expand All @@ -82,9 +93,8 @@ export function createYamlAppendAction({

await fs.writeFile(sourceFilepath, updated);

ctx.output('repoUrl', `${resource}?repo=${name}&owner=${owner}`);
ctx.output('filePath', filepath);
ctx.output('path', _path.dirname(sourceFilepath));
ctx.output('sourceFilepath', sourceFilepath);
},
});
}
Expand Down
40 changes: 25 additions & 15 deletions plugins/scaffolder-yaml-actions/src/actions/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import _path from 'path';
import { Logger } from 'winston';
import { z } from 'zod';
import { set } from '../operations/set';
import { isUrl } from './_helpers';

interface CreateYamlSetActionOptions {
logger: Logger;
Expand All @@ -18,7 +19,7 @@ interface CreateYamlSetActionOptions {
}

const InputSchema = z.object({
url: z.string().describe('URL of the YAML file to set'),
url: z.string().describe('URL of the YAML file to set or file system path (relative or absolute)'),
path: z.string().describe('The path of the property to set'),
value: z
.union([z.string(), z.number(), z.null()])
Expand Down Expand Up @@ -52,18 +53,28 @@ export function createYamlSetAction({
async handler(ctx) {
const { url, entityRef, path, value } = ctx.input;

const { filepath, resource, owner, name } = parseGitUrl(url);

const sourceFilepath = resolveSafeChildPath(ctx.workspacePath, filepath);

// This should be removed in favour of using fetch:plain:file
// FIX: blocked by https://github.com/backstage/backstage/issues/17072
await fetchAction.handler({
...ctx,
input: {
url: _path.dirname(ctx.input.url),
},
});
let sourceFilepath;
if (isUrl(url)) {
const { filepath, resource, owner, name } = parseGitUrl(url);

// This should be removed in favour of using fetch:plain:file
// FIX: blocked by https://github.com/backstage/backstage/issues/17072
await fetchAction.handler({
...ctx,
input: {
url: _path.dirname(ctx.input.url),
},
});

sourceFilepath = resolveSafeChildPath(ctx.workspacePath, filepath);

ctx.output('repoUrl', `${resource}?repo=${name}&owner=${owner}`);
ctx.output('filePath', filepath);
} else if (_path.isAbsolute(url)) {
sourceFilepath = url;
} else {
sourceFilepath = resolveSafeChildPath(ctx.workspacePath, url);
}

let content;
try {
Expand All @@ -82,9 +93,8 @@ export function createYamlSetAction({

await fs.writeFile(sourceFilepath, updated);

ctx.output('repoUrl', `${resource}?repo=${name}&owner=${owner}`);
ctx.output('filePath', filepath);
ctx.output('path', _path.dirname(sourceFilepath));
ctx.output('sourceFilepath', sourceFilepath);
},
});
}
Expand Down

0 comments on commit b4e907e

Please sign in to comment.