Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions workspaces/extensions/.changeset/loud-pugs-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@red-hat-developer-hub/backstage-plugin-extensions': patch
---

Fix extension plugin Apply button to apply minimal YAML when the YAML section is empty
31 changes: 31 additions & 0 deletions workspaces/extensions/plugins/extensions/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,37 @@ plugins:
`,
);
});

it('should create minimal YAML when editor is empty', () => {
const content = applyContent(
'',
'backstage-community-plugin-quay',
packages,
mockNewContent,
);
expect(content).toContain('plugins:');
expect(content).toContain(
'package: ./dynamic-plugins/dist/backstage-community-plugin-quay',
);
expect(content).toContain('disabled: false');
expect(content).toContain('pluginConfig:');
expect(content).toContain('catalog:');
});

it('should create minimal YAML when editor has only whitespace', () => {
const content = applyContent(
' \n \t ',
'backstage-community-plugin-quay',
packages,
mockNewContent,
);
expect(content).toContain('plugins:');
expect(content).toContain(
'package: ./dynamic-plugins/dist/backstage-community-plugin-quay',
);
expect(content).toContain('disabled: false');
expect(content).toContain('pluginConfig:');
});
});

describe('getExampleAsMarkdown', () => {
Expand Down
53 changes: 50 additions & 3 deletions workspaces/extensions/plugins/extensions/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Pair, parseDocument, Scalar, YAMLSeq, stringify } from 'yaml';
import { Pair, parse, parseDocument, Scalar, YAMLSeq, stringify } from 'yaml';
import { JsonObject } from '@backstage/types';
import { ExtensionsPluginInstallStatus } from '@red-hat-developer-hub/backstage-plugin-extensions-common';
import { TranslationFunction } from '@backstage/core-plugin-api/alpha';
Expand Down Expand Up @@ -80,13 +80,29 @@ export const applyContent = (
otherPackageNames: { [key: string]: string },
newContent: string | JsonObject,
) => {
if (!editorContent) {
return null;
if (!editorContent || !editorContent.trim()) {
const packagePath = otherPackageNames[packageName];
if (!packagePath) {
return null;
}

const minimalYaml = {
plugins: [
{
package: packagePath,
disabled: false,
pluginConfig:
typeof newContent === 'string' ? parse(newContent) : newContent,
},
],
};
return stringify(minimalYaml);
}
const content = parseDocument(editorContent);
const plugins = content.get('plugins');

if (plugins instanceof YAMLSeq && Array.isArray(plugins?.items)) {
let foundPackage = false;
(plugins?.items || []).forEach((plugin: any) => {
if (plugin instanceof Object) {
const pluginPackage = plugin.items?.find((i: Pair<Scalar, Scalar>) => {
Expand All @@ -96,6 +112,7 @@ export const applyContent = (
);
});
if (pluginPackage) {
foundPackage = true;
if (typeof newContent === 'string') {
plugin.set('pluginConfig', parseDocument(newContent));
} else {
Expand All @@ -104,6 +121,36 @@ export const applyContent = (
}
}
});

if (!foundPackage) {
const packagePath = otherPackageNames[packageName];
if (packagePath) {
const newPlugin: any = {
package: packagePath,
disabled: false,
};
if (typeof newContent === 'string') {
newPlugin.pluginConfig = parseDocument(newContent);
} else {
newPlugin.pluginConfig = newContent;
}
plugins.add(newPlugin);
}
}
} else {
const packagePath = otherPackageNames[packageName];
if (packagePath) {
const newPlugin: any = {
package: packagePath,
disabled: false,
};
if (typeof newContent === 'string') {
newPlugin.pluginConfig = parseDocument(newContent);
} else {
newPlugin.pluginConfig = newContent;
}
content.set('plugins', [newPlugin]);
}
}
return content.toString();
};
Expand Down