Skip to content

Commit c56f799

Browse files
committed
Add support to edit the knowledge contribution for native mode
Signed-off-by: Anil Vishnoi <[email protected]>
1 parent 0163834 commit c56f799

File tree

11 files changed

+323
-196
lines changed

11 files changed

+323
-196
lines changed

src/app/api/native/pr/knowledge/route.ts

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ export async function POST(req: NextRequest) {
1818
const REPO_DIR = path.join(LOCAL_TAXONOMY_ROOT_DIR, '/taxonomy');
1919
try {
2020
// Extract the data from the request body
21-
const { content, attribution, name, email, submissionSummary, filePath } = await req.json();
21+
const { action, branchName, content, attribution, name, email, submissionSummary, filePath, oldFilesPath } = await req.json();
22+
23+
let knowledgeBranchName;
24+
if (action == 'update' && branchName != '') {
25+
knowledgeBranchName = branchName;
26+
} else {
27+
knowledgeBranchName = `knowledge-contribution-${Date.now()}`;
28+
}
2229

2330
// Parse the YAML string into an object
2431
const knowledgeData = yaml.load(content) as KnowledgeYamlData;
@@ -27,24 +34,29 @@ export async function POST(req: NextRequest) {
2734
const yamlString = dumpYaml(knowledgeData);
2835

2936
// Define branch name and file paths
30-
const branchName = `knowledge-contribution-${Date.now()}`;
31-
const newYamlFilePath = path.join(KNOWLEDGE_DIR, filePath, 'qna.yaml');
32-
const newAttributionFilePath = path.join(KNOWLEDGE_DIR, filePath, 'attribution.txt');
3337
const attributionContent = `Title of work: ${attribution.title_of_work}
3438
Link to work: ${attribution.link_to_work}
3539
Revision: ${attribution.revision}
3640
License of the work: ${attribution.license_of_the_work}
3741
Creator names: ${attribution.creator_names}
3842
`;
3943

44+
// Set the flag if commit needs to be amended
45+
let amendCommit = false;
46+
4047
// Initialize the repository if it doesn’t exist
4148
await git.init({ fs, dir: REPO_DIR });
4249

43-
// Create a new branch
44-
await git.branch({ fs, dir: REPO_DIR, ref: branchName });
50+
// Create a new branch if the knowledge is pushed for first time
51+
if (action != 'update') {
52+
await git.branch({ fs, dir: REPO_DIR, ref: knowledgeBranchName });
53+
}
4554

4655
// Checkout the new branch
47-
await git.checkout({ fs, dir: REPO_DIR, ref: branchName });
56+
await git.checkout({ fs, dir: REPO_DIR, ref: knowledgeBranchName });
57+
58+
const newYamlFilePath = path.join(KNOWLEDGE_DIR, filePath, 'qna.yaml');
59+
const newAttributionFilePath = path.join(KNOWLEDGE_DIR, filePath, 'attribution.txt');
4860

4961
// Write YAML file to the knowledge directory
5062
const yamlFilePath = path.join(REPO_DIR, newYamlFilePath);
@@ -59,6 +71,28 @@ Creator names: ${attribution.creator_names}
5971
await git.add({ fs, dir: REPO_DIR, filepath: newYamlFilePath });
6072
await git.add({ fs, dir: REPO_DIR, filepath: newAttributionFilePath });
6173

74+
if (action == 'update') {
75+
// Define file paths
76+
const oldYamlFilePath = path.join(KNOWLEDGE_DIR, oldFilesPath, 'qna.yaml');
77+
const oldAttributionFilePath = path.join(KNOWLEDGE_DIR, oldFilesPath, 'attribution.txt');
78+
79+
if (oldYamlFilePath != newYamlFilePath) {
80+
console.log('File path for the knowledge contribution is updated, removing the old files.');
81+
// Write the QnA YAML file
82+
const yamlFilePath = path.join(REPO_DIR, oldYamlFilePath);
83+
fs.unlinkSync(yamlFilePath);
84+
85+
// Write the attribution text file
86+
const attributionFilePath = path.join(REPO_DIR, oldAttributionFilePath);
87+
fs.unlinkSync(attributionFilePath);
88+
89+
await git.remove({ fs, dir: REPO_DIR, filepath: oldYamlFilePath });
90+
await git.remove({ fs, dir: REPO_DIR, filepath: oldAttributionFilePath });
91+
92+
amendCommit = true;
93+
}
94+
}
95+
6296
// Commit the changes
6397
await git.commit({
6498
fs,
@@ -67,12 +101,13 @@ Creator names: ${attribution.creator_names}
67101
author: {
68102
name: name,
69103
email: email
70-
}
104+
},
105+
amend: amendCommit
71106
});
72107

73108
// Respond with success message and branch name
74-
console.log(`Knowledge contribution submitted successfully to local taxonomy repo. Submission Name is ${branchName}.`);
75-
return NextResponse.json({ message: 'Knowledge contribution submitted successfully.', branch: branchName }, { status: 201 });
109+
console.log(`Knowledge contribution submitted successfully to local taxonomy repo. Submission Name is ${knowledgeBranchName}.`);
110+
return NextResponse.json({ message: 'Knowledge contribution submitted successfully.', branch: knowledgeBranchName }, { status: 201 });
76111
} catch (error) {
77112
console.error(`Failed to submit knowledge contribution to local taxonomy repo:`, error);
78113
return NextResponse.json({ error: 'Failed to submit knowledge contribution.' }, { status: 500 });

src/components/Contribute/EditKnowledge/github/EditKnowledge.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { KnowledgeEditFormData, KnowledgeFormData, QuestionAndAnswerPair, Knowle
1212
import { useEffect, useState } from 'react';
1313
import { useRouter } from 'next/navigation';
1414
import KnowledgeFormGithub from '../../Knowledge/Github';
15-
import { ValidatedOptions, Modal, ModalVariant } from '@patternfly/react-core';
15+
import { ValidatedOptions, Modal, ModalVariant, ModalBody } from '@patternfly/react-core';
1616

1717
interface EditKnowledgeClientComponentProps {
1818
prNumber: number;
@@ -57,8 +57,7 @@ const EditKnowledge: React.FC<EditKnowledgeClientComponentProps> = ({ prNumber }
5757
branchName: '',
5858
knowledgeFormData: knowledgeExistingFormData,
5959
pullRequestNumber: prNumber,
60-
yamlFile: { filename: '' },
61-
attributionFile: { filename: '' }
60+
oldFilesPath: ''
6261
};
6362

6463
knowledgeExistingFormData.submissionSummary = prData.title;
@@ -70,11 +69,14 @@ const EditKnowledge: React.FC<EditKnowledgeClientComponentProps> = ({ prNumber }
7069
if (!foundYamlFile) {
7170
throw new Error('No YAML file found in the pull request.');
7271
}
73-
knowledgeEditFormData.yamlFile = foundYamlFile;
72+
const existingFilesPath = foundYamlFile.filename.split('/').slice(1, -1).join('/');
73+
74+
// Set the current Yaml file path as a old files path
75+
knowledgeEditFormData.oldFilesPath = existingFilesPath + '/';
7476

7577
const yamlContent = await fetchFileContent(session.accessToken, foundYamlFile.filename, prData.head.sha);
7678
const yamlData: KnowledgeYamlData = yaml.load(yamlContent) as KnowledgeYamlData;
77-
console.log('Parsed YAML data:', yamlData);
79+
console.log('Parsed Knowledge YAML data:', yamlData);
7880

7981
// Populate the form fields with YAML data
8082
knowledgeExistingFormData.documentOutline = yamlData.document_outline;
@@ -118,9 +120,8 @@ const EditKnowledge: React.FC<EditKnowledgeClientComponentProps> = ({ prNumber }
118120
if (foundAttributionFile) {
119121
const attributionContent = await fetchFileContent(session.accessToken, foundAttributionFile.filename, prData.head.sha);
120122
const attributionData = parseAttributionContent(attributionContent);
121-
console.log('Parsed attribution data:', attributionData);
123+
console.log('Parsed knowledge attribution data:', attributionData);
122124

123-
knowledgeEditFormData.attributionFile = foundAttributionFile;
124125
// Populate the form fields with attribution data
125126
knowledgeExistingFormData.titleWork = attributionData.title_of_work;
126127
knowledgeExistingFormData.linkWork = attributionData.link_to_work ? attributionData.link_to_work : '';
@@ -165,19 +166,15 @@ const EditKnowledge: React.FC<EditKnowledgeClientComponentProps> = ({ prNumber }
165166

166167
if (isLoading) {
167168
return (
168-
// <AppLayout>
169169
<Modal variant={ModalVariant.small} title="Loading Knowledge Data" isOpen={isLoading} onClose={() => handleOnClose()}>
170-
<div>{loadingMsg}</div>
170+
<ModalBody>
171+
<div>{loadingMsg}</div>
172+
</ModalBody>
171173
</Modal>
172-
// </AppLayout>
173174
);
174175
}
175176

176-
return (
177-
// <AppLayout>
178-
<KnowledgeFormGithub knowledgeEditFormData={knowledgeEditFormData} />
179-
// </AppLayout>
180-
);
177+
return <KnowledgeFormGithub knowledgeEditFormData={knowledgeEditFormData} />;
181178
};
182179

183180
export default EditKnowledge;

0 commit comments

Comments
 (0)