@@ -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 }
3438Link to work: ${ attribution . link_to_work }
3539Revision: ${ attribution . revision }
3640License of the work: ${ attribution . license_of_the_work }
3741Creator 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 } ) ;
0 commit comments