Skip to content

Commit f18c0f7

Browse files
committed
Component create from Git fails if a devfile exists in git repo #3386
Fixes: #3386 Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
1 parent c0eacb9 commit f18c0f7

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

src/webview/common/devfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { StarterProject } from '../../odo/componentTypeDescription';
88
export type Devfile = {
99
name: string;
1010
id: string;
11-
port: number;
11+
port?: number;
1212
registryName: string;
1313
description: string;
1414
logoUrl: string;

src/webview/common/devfileListItem.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Check, Close } from '@mui/icons-material';
66
import { Box, Chip, Stack, Tooltip, Typography } from '@mui/material';
77
import * as React from 'react';
88
import { Devfile } from '../common/devfile';
9+
import DevfileLogo from '../../../images/context/devfile.png';
910

1011
export type DevfileListItemProps = {
1112
devfile: Devfile;
@@ -56,7 +57,7 @@ function DevfileListContent(props: DevfileListItemProps) {
5657
borderRadius: '4px',
5758
}}
5859
>
59-
<img src={props.devfile.logoUrl} style={{ maxWidth: '6em', maxHeight: '6em' }} />
60+
<img src={props.devfile.logoUrl ? props.devfile.logoUrl : DevfileLogo} style={{ maxWidth: '6em', maxHeight: '6em' }} />
6061
</Box>
6162
<Stack
6263
direction="column"
@@ -76,7 +77,11 @@ function DevfileListContent(props: DevfileListItemProps) {
7677
{props.devfile.name}
7778
</Typography>
7879
<Typography variant="body2" fontStyle="italic">
79-
from {props.devfile.registryName}
80+
from {
81+
(props.devfile.registryName ?
82+
props.devfile.registryName
83+
: 'Custom Devfile')
84+
}
8085
</Typography>
8186
</Stack>
8287
<Typography
@@ -98,17 +103,17 @@ function DevfileListContent(props: DevfileListItemProps) {
98103
icon={props.devfile.supportsDeploy ? <Check /> : <Close />}
99104
color={props.devfile.supportsDeploy ? 'success' : 'error'}
100105
/>
101-
{props.devfile.tags.map((tag, i) => {
106+
{(props.devfile.tags && props.devfile.tags.map((tag, i) => {
102107
if (i >= 4) {
103108
return;
104109
}
105110
return <Chip size="small" label={tag} key={tag} />;
106-
})}
107-
{props.devfile.tags.length > 4 && (
111+
}))}
112+
{(props.devfile.tags && props.devfile.tags.length > 4 && (
108113
<Tooltip title={props.devfile.tags.slice(4).join(', ')}>
109114
<Chip size="small" label="• • •" />
110115
</Tooltip>
111-
)}
116+
))}
112117
</Stack>
113118
</Stack>
114119
</Stack>

src/webview/create-component/createComponentLoader.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from '../common-ext/createComponentHelpers';
3030
import { loadWebviewHtml, validateGitURL } from '../common-ext/utils';
3131
import { Devfile, DevfileRegistry, TemplateProjectIdentifier } from '../common/devfile';
32+
import { DevfileV1 } from '../../util/devfileV1Type';
3233

3334
interface CloneProcess {
3435
status: boolean;
@@ -281,11 +282,19 @@ export default class CreateComponentLoader {
281282
action: 'cloneFailed',
282283
});
283284
} else {
285+
const isGirDevfileExists = await isDevfileExists(tmpFolder);
284286
void CreateComponentLoader.panel.webview.postMessage({
285287
action: 'devfileExists',
286-
data: await isDevfileExists(tmpFolder),
288+
data: isGirDevfileExists,
287289
});
288-
void CreateComponentLoader.getRecommendedDevfile(tmpFolder);
290+
291+
if (isGirDevfileExists) {
292+
// Use the Devfile existing in Gir-repo
293+
void CreateComponentLoader.getExistingDevfile(tmpFolder);
294+
} else {
295+
// Use recommended Devfile
296+
void CreateComponentLoader.getRecommendedDevfile(tmpFolder);
297+
}
289298
}
290299
break;
291300
}
@@ -424,6 +433,55 @@ export default class CreateComponentLoader {
424433
}
425434
}
426435

436+
static async getExistingDevfile(uri: Uri): Promise<void> {
437+
const devFileYamlPath = path.join(tmpFolder.fsPath, 'devfile.yaml');
438+
let rawDevfile: any;
439+
let supportsDebug = false; // Initial value
440+
let supportsDeploy = false; // Initial value
441+
try {
442+
void CreateComponentLoader.panel.webview.postMessage({
443+
action: 'getRecommendedDevfileStart'
444+
});
445+
const componentDescription = await Odo.Instance.describeComponent(uri.fsPath);
446+
if (componentDescription) {
447+
rawDevfile = componentDescription.devfileData.devfile;
448+
supportsDebug = componentDescription.devfileData.supportedOdoFeatures.debug;
449+
supportsDeploy = componentDescription.devfileData.supportedOdoFeatures.deploy;
450+
}
451+
} catch (Error) {
452+
// Will try reading the raw devfile
453+
} finally {
454+
if (!rawDevfile) {
455+
//Try reading the raw devfile
456+
const file = await fs.readFile(devFileYamlPath, 'utf8');
457+
rawDevfile = JSYAML.load(file.toString());
458+
}
459+
460+
void CreateComponentLoader.panel.webview.postMessage({
461+
action: 'getRecommendedDevfile'
462+
});
463+
464+
const devfile: Devfile = {
465+
description: rawDevfile.metadata.description,
466+
registryName: devFileYamlPath,
467+
name: rawDevfile.metadata.displayName ? rawDevfile.metadata.displayName : rawDevfile.metadata.name,
468+
id: rawDevfile.metadata.name,
469+
starterProjects: rawDevfile.starterProjects,
470+
tags: [],
471+
yaml: JSYAML.dump(rawDevfile),
472+
supportsDebug,
473+
supportsDeploy,
474+
} as Devfile;
475+
476+
void CreateComponentLoader.panel.webview.postMessage({
477+
action: 'recommendedDevfile',
478+
data: {
479+
devfile,
480+
},
481+
});
482+
}
483+
}
484+
427485
static async getRecommendedDevfile(uri: Uri): Promise<void> {
428486
let analyzeRes: AnalyzeResponse[] = [];
429487
let compDescriptions: ComponentTypeDescription[] = [];
@@ -444,7 +502,7 @@ export default class CreateComponentLoader {
444502
try {
445503
const devFileV1Path = path.join(uri.fsPath, 'devfile.yaml');
446504
const file = await fs.readFile(devFileV1Path, 'utf8');
447-
const devfileV1 = JSYAML.load(file.toString());
505+
const devfileV1 = JSYAML.load(file.toString()) as DevfileV1;
448506
await fs.unlink(devFileV1Path);
449507
analyzeRes = await Odo.Instance.analyze(uri.fsPath);
450508
compDescriptions = getCompDescription(analyzeRes);
@@ -475,7 +533,7 @@ export default class CreateComponentLoader {
475533
});
476534
const devfileRegistry: DevfileRegistry[] = getDevfileRegistries();
477535
const allDevfiles: Devfile[] = devfileRegistry.flatMap((registry) => registry.devfiles);
478-
const devfile: Devfile =
536+
const devfile: Devfile | undefined =
479537
compDescriptions.length !== 0
480538
? allDevfiles.find(
481539
(devfile) => devfile.name === compDescriptions[0].displayName,
@@ -515,7 +573,8 @@ function getDevfileType(devfileDisplayName: string): string {
515573
const devfileDescription: ComponentTypeDescription = Array.from(compDescriptions).find(
516574
(description) => description.displayName === devfileDisplayName,
517575
);
518-
return devfileDescription.name;
576+
// TODO: What value to return in case of custom devfile that comes, f.i., from a Git-repo?
577+
return devfileDescription ? devfileDescription.name : devfileDisplayName;
519578
}
520579

521580
function getEndPoints(compDescription: ComponentTypeDescription): Endpoint[] {

src/webview/create-component/pages/fromExistingGitRepo.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,15 @@ export function FromExistingGitRepo({ setCurrentView }) {
454454
setCurrentPage('fromGitRepo');
455455
}}
456456
createComponent={createComponentFromGitRepo}
457-
devfile={recommendedDevfile.isDevfileExistsInRepo ? undefined : selectedDevfile ? selectedDevfile : recommendedDevfile.devfile}
457+
devfile={
458+
recommendedDevfile.isDevfileExistsInRepo ?
459+
recommendedDevfile.devfile // An existing Git-Repo devfile
460+
: selectedDevfile ?
461+
selectedDevfile // A selected Devfile
462+
: recommendedDevfile.devfile // A recommended Devfile
463+
}
458464
initialComponentName={buildSanitizedComponentName(gitURL.url)}
459-
/>
465+
/>
460466
);
461467
case 'selectDifferentDevfile':
462468
return (

0 commit comments

Comments
 (0)