From 4afad5b23a41833eb14e7da64d714736414e1f86 Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Mon, 4 Nov 2024 22:41:53 +0000
Subject: [PATCH 01/20] adding the file upload component that can be used for
Knowledge and Skills
Signed-off-by: Ash Evans
---
src/components/Contribute/YamlFileUpload.tsx | 108 +++++++++++++++++++
1 file changed, 108 insertions(+)
create mode 100644 src/components/Contribute/YamlFileUpload.tsx
diff --git a/src/components/Contribute/YamlFileUpload.tsx b/src/components/Contribute/YamlFileUpload.tsx
new file mode 100644
index 00000000..b8f5551d
--- /dev/null
+++ b/src/components/Contribute/YamlFileUpload.tsx
@@ -0,0 +1,108 @@
+import React from 'react';
+import { FileUpload } from '@patternfly/react-core/dist/dynamic/components/FileUpload';
+import { Form } from '@patternfly/react-core/dist/dynamic/components/Form';
+import { FormGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
+import { FormHelperText } from '@patternfly/react-core/dist/dynamic/components/Form';
+import { HelperText } from '@patternfly/react-core/dist/dynamic/components/HelperText';
+import { HelperTextItem } from '@patternfly/react-core/dist/dynamic/components/HelperText';
+import yaml from 'js-yaml';
+import { KnowledgeFormData } from './Knowledge';
+import { SkillFormData } from './Skill';
+
+interface YamlFileUploadProps {
+ isKnowledgeForm: boolean;
+ onYamlUploadFillForm: (data: KnowledgeFormData | SkillFormData) => void;
+}
+
+const YamlFileUpload: React.FC = ({ isKnowledgeForm, onYamlUploadFillForm }) => {
+ const [value, setValue] = React.useState('');
+ const [filename, setFilename] = React.useState('');
+ const [isLoading, setIsLoading] = React.useState(false);
+ const [isRejected, setIsRejected] = React.useState(false);
+
+ const handleFileInputChange = (_: unknown, file: File) => {
+ setFilename(file.name);
+ if (file) {
+ readFileContent(file);
+ }
+ };
+
+ const readFileContent = (file: File) => {
+ setIsLoading(true);
+ const reader = new FileReader();
+
+ reader.onload = (event) => {
+ const fileContent = event.target?.result as string;
+ setValue(fileContent);
+ setIsLoading(false);
+
+ try {
+ const parsedData = yaml.load(fileContent);
+
+ if (isKnowledgeForm && isKnowledgeFormData(parsedData)) {
+ onYamlUploadFillForm(parsedData);
+ } else if (!isKnowledgeForm) {
+ const skillData = parsedData as SkillFormData;
+ onYamlUploadFillForm(skillData);
+ } else {
+ setIsRejected(true); // Set rejected if it doesn't match expected type
+ }
+ } catch (error) {
+ console.error('Error parsing YAML file:', error);
+ setIsRejected(true);
+ }
+ };
+
+ reader.onerror = () => {
+ console.error('Error reading file');
+ setIsLoading(false);
+ setIsRejected(true);
+ };
+
+ reader.readAsText(file);
+ };
+
+ const isKnowledgeFormData = (data: object): data is KnowledgeFormData => {
+ return typeof data === 'object' && 'knowledgeDocumentRepositoryUrl' in data && 'knowledgeDocumentCommit' in data;
+ };
+
+ const handleClear = () => {
+ setFilename('');
+ setValue('');
+ setIsRejected(false);
+ };
+
+ return (
+ <>
+
+ >
+ );
+};
+
+export default YamlFileUpload;
From e0ef48a1cd5c104e9f529cef777e9d80a580616e Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Wed, 6 Nov 2024 11:09:15 +0000
Subject: [PATCH 02/20] adding the YamlFileUpload component to the knowledge
form and creating a function to populate the knowledge form fields
Signed-off-by: Ash Evans
---
src/components/Contribute/Knowledge/index.tsx | 21 +++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/components/Contribute/Knowledge/index.tsx b/src/components/Contribute/Knowledge/index.tsx
index 73651889..2e701d93 100644
--- a/src/components/Contribute/Knowledge/index.tsx
+++ b/src/components/Contribute/Knowledge/index.tsx
@@ -28,12 +28,15 @@ import { ValidatedOptions } from '@patternfly/react-core/dist/esm/helpers/consta
import { DownloadDropdown } from './DownloadDropdown/DownloadDropdown';
import { ViewDropdown } from './ViewDropdown/ViewDropdown';
import Update from './Update/Update';
-import { PullRequestFile } from '@/types';
+import { KnowledgeYamlData, PullRequestFile } from '@/types';
import { Button } from '@patternfly/react-core/dist/esm/components/Button/Button';
import { useRouter } from 'next/navigation';
import { autoFillKnowledgeFields } from './AutoFill';
import { Spinner } from '@patternfly/react-core/dist/esm/components/Spinner';
+import UploadIcon from '@patternfly/react-icons/dist/esm/icons/upload-icon';
+import YamlFileUpload from '../YamlFileUpload';
+
export interface QuestionAndAnswerPair {
immutable: boolean;
question: string;
@@ -432,6 +435,18 @@ export const KnowledgeForm: React.FunctionComponent = ({ kno
setSeedExamples(autoFillKnowledgeFields.seedExamples);
};
+ const onYamlUploadKnowledgeFillForm = (data: KnowledgeYamlData): void => {
+ setName(data.created_by);
+ setDocumentOutline(data.document_outline);
+ setSubmissionSummary(data.document_outline);
+ setDomain(data.domain);
+ setKnowledgeDocumentRepositoryUrl(data.document.repo);
+ setKnowledgeDocumentCommit(data.document.commit);
+ // Looks like the form accepts
+ setDocumentName(data.document.patterns.join(', '));
+ setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
+ };
+
const knowledgeFormData: KnowledgeFormData = {
email: email,
name: name,
@@ -479,7 +494,7 @@ export const KnowledgeForm: React.FunctionComponent = ({ kno
Auto-Fill
)}
-
+
+
+
);
From a00e08635af51100268ec5c014fd66c0c922da2c Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Wed, 6 Nov 2024 11:10:05 +0000
Subject: [PATCH 03/20] adding a function to convert the yaml qnas to
SeedExamples[]
Signed-off-by: Ash Evans
---
src/components/Contribute/Knowledge/index.tsx | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/src/components/Contribute/Knowledge/index.tsx b/src/components/Contribute/Knowledge/index.tsx
index 2e701d93..4ceabb17 100644
--- a/src/components/Contribute/Knowledge/index.tsx
+++ b/src/components/Contribute/Knowledge/index.tsx
@@ -435,6 +435,21 @@ export const KnowledgeForm: React.FunctionComponent = ({ kno
setSeedExamples(autoFillKnowledgeFields.seedExamples);
};
+ const yamlSeedExampleToFormSeedExample = (
+ yamlSeedExamples: { context: string; questions_and_answers: { question: string; answer: string }[] }[]
+ ) => {
+ return yamlSeedExamples.map((yamlSeedExample) => ({
+ immutable: true,
+ isExpanded: false,
+ context: yamlSeedExample.context,
+ isContextValid: ValidatedOptions.default,
+ questionAndAnswers: yamlSeedExample.questions_and_answers.map((questionAndAnswer) => ({
+ question: questionAndAnswer.question,
+ answer: questionAndAnswer.answer
+ }))
+ })) as SeedExample[];
+ };
+
const onYamlUploadKnowledgeFillForm = (data: KnowledgeYamlData): void => {
setName(data.created_by);
setDocumentOutline(data.document_outline);
From 702a6021dacc3306d182f5139f38c9404d10307f Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Wed, 6 Nov 2024 11:15:52 +0000
Subject: [PATCH 04/20] adding new logic in type checking knowledge or skills
yaml upload
Signed-off-by: Ash Evans
---
src/components/Contribute/YamlFileUpload.tsx | 28 +++++++++++---------
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/src/components/Contribute/YamlFileUpload.tsx b/src/components/Contribute/YamlFileUpload.tsx
index b8f5551d..434b3d39 100644
--- a/src/components/Contribute/YamlFileUpload.tsx
+++ b/src/components/Contribute/YamlFileUpload.tsx
@@ -6,15 +6,15 @@ import { FormHelperText } from '@patternfly/react-core/dist/dynamic/components/F
import { HelperText } from '@patternfly/react-core/dist/dynamic/components/HelperText';
import { HelperTextItem } from '@patternfly/react-core/dist/dynamic/components/HelperText';
import yaml from 'js-yaml';
-import { KnowledgeFormData } from './Knowledge';
-import { SkillFormData } from './Skill';
+import { KnowledgeYamlData, SkillYamlData } from '@/types';
interface YamlFileUploadProps {
isKnowledgeForm: boolean;
- onYamlUploadFillForm: (data: KnowledgeFormData | SkillFormData) => void;
+ onYamlUploadKnowledgeFillForm?: (data: KnowledgeYamlData) => void;
+ onYamlUploadSkillsFillForm?: (data: SkillYamlData) => void;
}
-const YamlFileUpload: React.FC = ({ isKnowledgeForm, onYamlUploadFillForm }) => {
+const YamlFileUpload: React.FC = ({ isKnowledgeForm, onYamlUploadKnowledgeFillForm, onYamlUploadSkillsFillForm }) => {
const [value, setValue] = React.useState('');
const [filename, setFilename] = React.useState('');
const [isLoading, setIsLoading] = React.useState(false);
@@ -38,14 +38,12 @@ const YamlFileUpload: React.FC = ({ isKnowledgeForm, onYaml
try {
const parsedData = yaml.load(fileContent);
-
if (isKnowledgeForm && isKnowledgeFormData(parsedData)) {
- onYamlUploadFillForm(parsedData);
- } else if (!isKnowledgeForm) {
- const skillData = parsedData as SkillFormData;
- onYamlUploadFillForm(skillData);
+ onYamlUploadKnowledgeFillForm?.(parsedData);
+ } else if (!isKnowledgeForm && isSkillFormData(parsedData)) {
+ onYamlUploadSkillsFillForm?.(parsedData);
} else {
- setIsRejected(true); // Set rejected if it doesn't match expected type
+ setIsRejected(true);
}
} catch (error) {
console.error('Error parsing YAML file:', error);
@@ -62,8 +60,14 @@ const YamlFileUpload: React.FC = ({ isKnowledgeForm, onYaml
reader.readAsText(file);
};
- const isKnowledgeFormData = (data: object): data is KnowledgeFormData => {
- return typeof data === 'object' && 'knowledgeDocumentRepositoryUrl' in data && 'knowledgeDocumentCommit' in data;
+ // Type guard for KnowledgeFormData
+ const isKnowledgeFormData = (data: any): data is KnowledgeYamlData => {
+ return data && typeof data === 'object' && 'document' in data && 'document_outline' in data;
+ };
+
+ // Type guard for SkillFormData
+ const isSkillFormData = (data: any): data is SkillYamlData => {
+ return data && typeof data === 'object' && 'task_description' in data;
};
const handleClear = () => {
From ec86d6f041ee0773b52481ec4fda56e1aaba6881 Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Wed, 6 Nov 2024 11:28:59 +0000
Subject: [PATCH 05/20] adding a ternary to account for partially complete yaml
Signed-off-by: Ash Evans
---
src/components/Contribute/Knowledge/index.tsx | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/src/components/Contribute/Knowledge/index.tsx b/src/components/Contribute/Knowledge/index.tsx
index 4ceabb17..16377901 100644
--- a/src/components/Contribute/Knowledge/index.tsx
+++ b/src/components/Contribute/Knowledge/index.tsx
@@ -451,14 +451,13 @@ export const KnowledgeForm: React.FunctionComponent = ({ kno
};
const onYamlUploadKnowledgeFillForm = (data: KnowledgeYamlData): void => {
- setName(data.created_by);
- setDocumentOutline(data.document_outline);
- setSubmissionSummary(data.document_outline);
- setDomain(data.domain);
- setKnowledgeDocumentRepositoryUrl(data.document.repo);
- setKnowledgeDocumentCommit(data.document.commit);
- // Looks like the form accepts
- setDocumentName(data.document.patterns.join(', '));
+ setName(data.created_by ?? '');
+ setDocumentOutline(data.document_outline ?? '');
+ setSubmissionSummary(data.document_outline ?? '');
+ setDomain(data.domain ?? '');
+ setKnowledgeDocumentRepositoryUrl(data.document.repo ?? '');
+ setKnowledgeDocumentCommit(data.document.commit ?? '');
+ setDocumentName(data.document.patterns.join(', ') ?? '');
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
};
From 3a3d69a38f3ab36d3f5f7b43bbd75a8d4d3cb615 Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Wed, 6 Nov 2024 11:44:10 +0000
Subject: [PATCH 06/20] accounting for partial qna yaml file
Signed-off-by: Ash Evans
---
src/components/Contribute/Knowledge/index.tsx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/components/Contribute/Knowledge/index.tsx b/src/components/Contribute/Knowledge/index.tsx
index 16377901..9b9c965c 100644
--- a/src/components/Contribute/Knowledge/index.tsx
+++ b/src/components/Contribute/Knowledge/index.tsx
@@ -441,11 +441,11 @@ export const KnowledgeForm: React.FunctionComponent = ({ kno
return yamlSeedExamples.map((yamlSeedExample) => ({
immutable: true,
isExpanded: false,
- context: yamlSeedExample.context,
+ context: yamlSeedExample.context ?? '',
isContextValid: ValidatedOptions.default,
questionAndAnswers: yamlSeedExample.questions_and_answers.map((questionAndAnswer) => ({
- question: questionAndAnswer.question,
- answer: questionAndAnswer.answer
+ question: questionAndAnswer.question ?? '',
+ answer: questionAndAnswer.answer ?? ''
}))
})) as SeedExample[];
};
From 1b5f86411daf0f0f7237649b90296a3f08e8ccdd Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Wed, 6 Nov 2024 12:11:25 +0000
Subject: [PATCH 07/20] add YamlFileUpload to the skill form
Signed-off-by: Ash Evans
---
src/components/Contribute/Skill/index.tsx | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/components/Contribute/Skill/index.tsx b/src/components/Contribute/Skill/index.tsx
index 737f92fb..c333aeb9 100644
--- a/src/components/Contribute/Skill/index.tsx
+++ b/src/components/Contribute/Skill/index.tsx
@@ -32,6 +32,7 @@ import SkillsInformation from './SkillsInformation/SkillsInformation';
import SkillsDescriptionContent from './SkillsDescription/SkillsDescriptionContent';
import { autoFillSkillsFields } from './AutoFill';
import { Spinner } from '@patternfly/react-core/dist/dynamic/components/Spinner';
+import YamlFileUpload from '../YamlFileUpload';
export interface SeedExample {
immutable: boolean;
@@ -371,6 +372,8 @@ export const SkillForm: React.FunctionComponent = ({ skillEditFo
Auto-Fill
)}
+
+
,
+ id: 'author-info-id'
+ }}
+ titleDescription="If you have partially completed yaml file you can upload it!"
+ />
+ }
+ >
+
+ setIsRejected(true)
+ }}
+ validated={isRejected ? 'error' : 'default'}
+ browseButtonText="Upload"
+ />
+
+
+
+ {isRejected ? 'Invalid YAML file for this form type' : 'Upload a YAML file'}
+
+
+
+
+
);
};
From 537b298aa20e429180334bbcfc843463883cf5fb Mon Sep 17 00:00:00 2001
From: Ash Evans
Date: Wed, 6 Nov 2024 14:35:03 +0000
Subject: [PATCH 12/20] finishing notht the skill and knowledge sections and
cleaning up imports
Signed-off-by: Ash Evans
---
src/components/Contribute/Knowledge/index.tsx | 5 ++---
src/components/Contribute/Skill/index.tsx | 2 +-
src/components/Contribute/YamlFileUpload.tsx | 2 +-
3 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/src/components/Contribute/Knowledge/index.tsx b/src/components/Contribute/Knowledge/index.tsx
index 9b9c965c..0c9f3f7f 100644
--- a/src/components/Contribute/Knowledge/index.tsx
+++ b/src/components/Contribute/Knowledge/index.tsx
@@ -33,8 +33,6 @@ import { Button } from '@patternfly/react-core/dist/esm/components/Button/Button
import { useRouter } from 'next/navigation';
import { autoFillKnowledgeFields } from './AutoFill';
import { Spinner } from '@patternfly/react-core/dist/esm/components/Spinner';
-
-import UploadIcon from '@patternfly/react-icons/dist/esm/icons/upload-icon';
import YamlFileUpload from '../YamlFileUpload';
export interface QuestionAndAnswerPair {
@@ -508,8 +506,9 @@ export const KnowledgeForm: React.FunctionComponent = ({ kno
Auto-Fill
)}
-