Skip to content

Commit

Permalink
adds devfile provider for catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
invincibleJai committed Apr 28, 2021
1 parent 2c1e85f commit 304c8ab
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 7 deletions.
23 changes: 23 additions & 0 deletions frontend/packages/dev-console/console-extensions.json
Expand Up @@ -186,6 +186,29 @@
"required": ["OPENSHIFT"]
}
},
{
"type": "console.catalog/item-type",
"properties": {
"type": "Devfile",
"title": "%devconsole~Devfiles%",
"catalogDescription": "%devconsole~Browse for devfiles that support a particular language or framework. Cluster administrators can customize the content made available in the catalog.%",
"typeDescription": "%devconsole~**Devfiles** are sets of objects for creating services, build configurations, and anything you have permission to create within a Project.%"
},
"flags": {
"required": ["OPENSHIFT"]
}
},
{
"type": "console.catalog/item-provider",
"properties": {
"catalogId": "dev-catalog",
"type": "Devfile",
"provider": { "$codeRef": "catalog.devfileProvider" }
},
"flags": {
"required": ["OPENSHIFT"]
}
},
{
"type": "console.catalog/item-type",
"properties": {
Expand Down
3 changes: 3 additions & 0 deletions frontend/packages/dev-console/locales/en/devconsole.json
Expand Up @@ -25,6 +25,9 @@
"Templates": "Templates",
"Browse for templates that can deploy services, create builds, or create any resources the template enables. Cluster administrators can customize the content made available in the catalog.": "Browse for templates that can deploy services, create builds, or create any resources the template enables. Cluster administrators can customize the content made available in the catalog.",
"**Templates** are sets of objects for creating services, build configurations, and anything you have permission to create within a Project.": "**Templates** are sets of objects for creating services, build configurations, and anything you have permission to create within a Project.",
"Devfiles": "Devfiles",
"Browse for devfiles that support a particular language or framework. Cluster administrators can customize the content made available in the catalog.": "Browse for devfiles that support a particular language or framework. Cluster administrators can customize the content made available in the catalog.",
"**Devfiles** are sets of objects for creating services, build configurations, and anything you have permission to create within a Project.": "**Devfiles** are sets of objects for creating services, build configurations, and anything you have permission to create within a Project.",
"Helm Charts": "Helm Charts",
"Add": "Add",
"Select a Project to start adding to it or <2>create a Project</2>.": "Select a Project to start adding to it or <2>create a Project</2>.",
Expand Down
Expand Up @@ -5,3 +5,5 @@ export { default as templateProvider } from './useTemplates';
export { default as builderImageSamplesProvider } from './useBuilderImageSamples';

export { default as devfileSamplesProvider } from './useDevfileSamples';

export { default as devfileProvider } from './useDevfile';
@@ -0,0 +1,88 @@
import * as React from 'react';
import { TFunction } from 'i18next';
import { useTranslation } from 'react-i18next';
import { APIError } from '@console/shared';
import { ExtensionHook, CatalogItem } from '@console/dynamic-plugin-sdk';
import { coFetchJSON } from '@console/internal/co-fetch';
import { ExternalLink } from '@console/internal/components/utils';
import { DevfileSample } from '../../import/devfile/devfile-types';

const normalizeDevfile = (devfileSamples: DevfileSample[], t: TFunction): CatalogItem[] => {
const normalizedDevfileSamples = devfileSamples?.map((sample) => {
const { name: uid, displayName, description, tags, git, icon } = sample;
const gitRepoUrl = Object.values(git.remotes)[0];
const iconUrl = icon ? `data:image/png;base64,${icon}` : '';
const href = `/import?importType=devfile&devfileName=${uid}&gitRepo=${gitRepoUrl}`;
const createLabel = t('devconsole~Create Application');
const type = 'Devfile';

const detailsProperties = [
{
label: t('devconsole~Sample repository'),
value: (
<ExternalLink href={gitRepoUrl} additionalClassName="co-break-all" text={gitRepoUrl} />
),
},
];

const detailsDescriptions = [
{
value: <p>{description}</p>,
},
];

const item: CatalogItem = {
uid: `${type}-${uid}`,
type,
name: displayName,
description,
tags,
cta: {
label: createLabel,
href,
},
icon: { url: iconUrl },
details: {
properties: detailsProperties,
descriptions: detailsDescriptions,
},
};

return item;
});

return normalizedDevfileSamples;
};

const useDevfile: ExtensionHook<CatalogItem[]> = (): [CatalogItem[], boolean, any] => {
const [devfileSamples, setDevfileSamples] = React.useState<DevfileSample[]>([]);
const [loadedError, setLoadedError] = React.useState<APIError>();
const { t } = useTranslation();

React.useEffect(() => {
let mounted = true;
const payload = {
registry: 'pkg/devfile/sample-placeholder.json',
};
coFetchJSON
.put('/api/devfile/samples', payload)
.then((resp) => {
if (mounted) setDevfileSamples(resp);
})
.catch((err) => {
if (mounted) setLoadedError(err);
});
return () => (mounted = false);
}, []);

const normalizedDevfileSamples = React.useMemo(() => normalizeDevfile(devfileSamples, t), [
devfileSamples,
t,
]);

const loaded = !!devfileSamples;

return [normalizedDevfileSamples, loaded, loadedError];
};

export default useDevfile;
@@ -1,10 +1,7 @@
import * as React from 'react';
import * as _ from 'lodash';
<<<<<<< HEAD
import { useTranslation } from 'react-i18next';
import { TFunction } from 'i18next';
=======
>>>>>>> create item provider for devfile samples
import { APIError } from '@console/shared';
import { ExtensionHook, CatalogItem } from '@console/dynamic-plugin-sdk';
import { coFetchJSON } from '@console/internal/co-fetch';
Expand Down
Expand Up @@ -30,13 +30,19 @@ const useHelmCharts: ExtensionHook<CatalogItem[]> = ({
);

React.useEffect(() => {
let mounted = true;
coFetch('/api/helm/charts/index.yaml')
.then(async (res) => {
const yaml = await res.text();
const json = safeLoad(yaml);
setHelmCharts(json.entries);
if (mounted) {
const yaml = await res.text();
const json = safeLoad(yaml);
setHelmCharts(json.entries);
}
})
.catch(setLoadedError);
.catch((err) => {
if (mounted) setLoadedError(err);
});
return () => (mounted = false);
}, []);

const normalizedHelmCharts: CatalogItem[] = React.useMemo(
Expand Down

0 comments on commit 304c8ab

Please sign in to comment.