Skip to content

Commit

Permalink
Add support to edit the skill contribution for native mode
Browse files Browse the repository at this point in the history
Signed-off-by: Anil Vishnoi <[email protected]>
  • Loading branch information
vishnoianil committed Jan 25, 2025
1 parent 901b1e2 commit 13edf7a
Show file tree
Hide file tree
Showing 15 changed files with 663 additions and 70 deletions.
105 changes: 105 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 42 additions & 10 deletions src/app/api/native/pr/skill/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export async function POST(req: NextRequest) {
const REPO_DIR = path.join(LOCAL_TAXONOMY_ROOT_DIR, '/taxonomy');
try {
// Extract the QnA data from the request body TODO: what is documentOutline?
const { content, attribution, name, email, submissionSummary, documentOutline, filePath } = await req.json(); // eslint-disable-line @typescript-eslint/no-unused-vars
const { action, branchName, content, attribution, name, email, submissionSummary, documentOutline, filePath, oldFilePath } = await req.json(); // eslint-disable-line @typescript-eslint/no-unused-vars

// Define file paths
const branchName = `skill-contribution-${Date.now()}`;
const newYamlFilePath = path.join(SKILLS_DIR, filePath, 'qna.yaml');
const newAttributionFilePath = path.join(SKILLS_DIR, filePath, 'attribution.txt');
let skillBranchName;
if (action == 'update' && branchName != '') {
skillBranchName = branchName;
} else {
skillBranchName = `skill-contribution-${Date.now()}`;
}

const skillData = yaml.load(content) as SkillYamlData;
const attributionData = attribution as AttributionData;
Expand All @@ -34,14 +36,21 @@ License of the work: ${attributionData.license_of_the_work}
Creator names: ${attributionData.creator_names}
`;

// Set the flag if commit needs to be amended
let amendCommit = false;

// Initialize the repository if it doesn’t exist
await git.init({ fs, dir: REPO_DIR });

// Create a new branch
await git.branch({ fs, dir: REPO_DIR, ref: branchName });
await git.branch({ fs, dir: REPO_DIR, ref: skillBranchName });

// Checkout the new branch
await git.checkout({ fs, dir: REPO_DIR, ref: branchName });
await git.checkout({ fs, dir: REPO_DIR, ref: skillBranchName });

// Define file paths
const newYamlFilePath = path.join(SKILLS_DIR, filePath, 'qna.yaml');
const newAttributionFilePath = path.join(SKILLS_DIR, filePath, 'attribution.txt');

// Write the QnA YAML file
const yamlFilePath = path.join(REPO_DIR, newYamlFilePath);
Expand All @@ -56,6 +65,28 @@ Creator names: ${attributionData.creator_names}
await git.add({ fs, dir: REPO_DIR, filepath: newYamlFilePath });
await git.add({ fs, dir: REPO_DIR, filepath: newAttributionFilePath });

if (action == 'update') {
// Define file paths
const oldYamlFilePath = path.join(SKILLS_DIR, oldFilePath, 'qna.yaml');
const oldAttributionFilePath = path.join(SKILLS_DIR, oldFilePath, 'attribution.txt');

if (oldYamlFilePath != newYamlFilePath) {
console.log('File path for the skill contribution is updated, removing the old files.');
// Write the QnA YAML file
const yamlFilePath = path.join(REPO_DIR, oldYamlFilePath);
fs.unlinkSync(yamlFilePath);

// Write the attribution text file
const attributionFilePath = path.join(REPO_DIR, oldAttributionFilePath);
fs.unlinkSync(attributionFilePath);

await git.remove({ fs, dir: REPO_DIR, filepath: oldYamlFilePath });
await git.remove({ fs, dir: REPO_DIR, filepath: oldAttributionFilePath });

amendCommit = true;
}
}

// Commit files
await git.commit({
fs,
Expand All @@ -64,12 +95,13 @@ Creator names: ${attributionData.creator_names}
author: {
name: name,
email: email
}
},
amend: amendCommit
});

// Respond with success
console.log('Skill contribution submitted successfully. Submission name is ', branchName);
return NextResponse.json({ message: 'Skill contribution submitted successfully.', branch: branchName }, { status: 201 });
console.log('Skill contribution submitted successfully. Submission name is ', skillBranchName);
return NextResponse.json({ message: 'Skill contribution submitted successfully.', branch: skillBranchName }, { status: 201 });
} catch (error) {
console.error('Failed to create local branch and commit:', error);
return NextResponse.json({ error: 'Failed to submit skill contribution.' }, { status: 500 });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// src/app/edit-submission/knowledge/[id]/page.tsx
import * as React from 'react';
import { AppLayout } from '@/components/AppLayout';
import EditKnowledge from '@/components/Contribute/EditKnowledge/EditKnowledge';
import EditKnowledge from '@/components/Contribute/EditKnowledge/github/EditKnowledge';

type PageProps = {
params: Promise<{ id: string }>;
Expand Down
20 changes: 20 additions & 0 deletions src/app/edit-submission/knowledge/native/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// src/app/edit-submission/knowledge/[id]/page.tsx
import * as React from 'react';
import { AppLayout } from '@/components/AppLayout';
import EditKnowledgeNative from '@/components/Contribute/EditKnowledge/native/EditKnowledge';

type PageProps = {
params: Promise<{ id: string }>;
};

const EditKnowledgePage = async ({ params }: PageProps) => {
const branchName = await params;

return (
<AppLayout>
<EditKnowledgeNative branchName={branchName.id} />
</AppLayout>
);
};

export default EditKnowledgePage;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// src/app/edit-submission/skill/[id]/page.tsx
import * as React from 'react';
import { AppLayout } from '@/components/AppLayout';
import EditSkill from '@/components/Contribute/EditSkill/EditSkill';
import EditSkill from '@/components/Contribute/EditSkill/github/EditSkill';

type PageProps = {
params: Promise<{ id: string }>;
Expand Down
20 changes: 20 additions & 0 deletions src/app/edit-submission/skill/native/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// src/app/edit-submission/skill/[id]/page.tsx
import * as React from 'react';
import { AppLayout } from '@/components/AppLayout';
import EditSkillNative from '@/components/Contribute/EditSkill/native/EditSkill';

type PageProps = {
params: Promise<{ id: string }>;
};

const EditSkillPage = async ({ params }: PageProps) => {
const branchName = await params;

return (
<AppLayout>
<EditSkillNative branchName={branchName.id} />
</AppLayout>
);
};

export default EditSkillPage;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import axios from 'axios';
import { KnowledgeEditFormData, KnowledgeFormData, QuestionAndAnswerPair, KnowledgeSeedExample } from '@/types';
import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import KnowledgeFormGithub from '../Knowledge/Github';
import KnowledgeFormGithub from '../../Knowledge/Github';
import { ValidatedOptions, Modal, ModalVariant } from '@patternfly/react-core';

interface EditKnowledgeClientComponentProps {
Expand Down
Loading

0 comments on commit 13edf7a

Please sign in to comment.