Skip to content

Commit 547812a

Browse files
authored
Merge pull request #494 from vishnoianil/native-edit
Add support to edit the skill and knowledge contribution for native mode
2 parents ab24e96 + 3e6da41 commit 547812a

File tree

26 files changed

+940
-183
lines changed

26 files changed

+940
-183
lines changed

package-lock.json

Lines changed: 108 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/api/native/git/branches/route.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ const LOCAL_TAXONOMY_ROOT_DIR = process.env.NEXT_PUBLIC_LOCAL_TAXONOMY_ROOT_DIR
99
const REMOTE_TAXONOMY_ROOT_DIR = process.env.NEXT_PUBLIC_TAXONOMY_ROOT_DIR || '';
1010
const REMOTE_TAXONOMY_REPO_CONTAINER_MOUNT_DIR = '/tmp/.instructlab-ui';
1111

12+
interface CommitDetails {
13+
message: string;
14+
email: string;
15+
name: string;
16+
}
17+
1218
interface Diffs {
1319
file: string;
1420
status: string;
@@ -131,6 +137,29 @@ async function handleDiff(branchName: string, localTaxonomyDir: string) {
131137
return NextResponse.json({ error: 'Invalid branch name for comparison' }, { status: 400 });
132138
}
133139

140+
// Resolve the reference to the branch's HEAD
141+
const commitOid = await git.resolveRef({
142+
fs,
143+
dir: localTaxonomyDir,
144+
ref: `refs/heads/${branchName}` // Resolve the branch reference
145+
});
146+
147+
// Read the commit object using its OID
148+
const commit = await git.readCommit({
149+
fs,
150+
dir: localTaxonomyDir,
151+
oid: commitOid
152+
});
153+
154+
const signoffMatch = commit.commit.message.split('Signed-off-by:');
155+
const message = signoffMatch ? signoffMatch[0].trim() : '';
156+
157+
const commitDetails: CommitDetails = {
158+
message: message,
159+
email: commit.commit.author.email,
160+
name: commit.commit.author.name
161+
};
162+
134163
const changes = await findDiff(branchName, localTaxonomyDir);
135164
const enrichedChanges: Diffs[] = [];
136165
for (const change of changes) {
@@ -142,7 +171,7 @@ async function handleDiff(branchName: string, localTaxonomyDir: string) {
142171
}
143172
}
144173

145-
return NextResponse.json({ changes: enrichedChanges }, { status: 200 });
174+
return NextResponse.json({ changes: enrichedChanges, commitDetails: commitDetails }, { status: 200 });
146175
} catch (error) {
147176
console.error(`Failed to show contribution changes ${branchName}:`, error);
148177
return NextResponse.json(

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/app/api/native/pr/skill/route.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ export async function POST(req: NextRequest) {
1717
const REPO_DIR = path.join(LOCAL_TAXONOMY_ROOT_DIR, '/taxonomy');
1818
try {
1919
// Extract the QnA data from the request body TODO: what is documentOutline?
20-
const { content, attribution, name, email, submissionSummary, documentOutline, filePath } = await req.json(); // eslint-disable-line @typescript-eslint/no-unused-vars
20+
const { action, branchName, content, attribution, name, email, submissionSummary, documentOutline, filePath, oldFilesPath } = await req.json(); // eslint-disable-line @typescript-eslint/no-unused-vars
2121

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

2729
const skillData = yaml.load(content) as SkillYamlData;
2830
const attributionData = attribution as AttributionData;
@@ -34,14 +36,23 @@ License of the work: ${attributionData.license_of_the_work}
3436
Creator names: ${attributionData.creator_names}
3537
`;
3638

39+
// Set the flag if commit needs to be amended
40+
let amendCommit = false;
41+
3742
// Initialize the repository if it doesn’t exist
3843
await git.init({ fs, dir: REPO_DIR });
3944

40-
// Create a new branch
41-
await git.branch({ fs, dir: REPO_DIR, ref: branchName });
45+
// Create a new branch if the skill is pushed for first time
46+
if (action != 'update') {
47+
await git.branch({ fs, dir: REPO_DIR, ref: skillBranchName });
48+
}
4249

4350
// Checkout the new branch
44-
await git.checkout({ fs, dir: REPO_DIR, ref: branchName });
51+
await git.checkout({ fs, dir: REPO_DIR, ref: skillBranchName });
52+
53+
// Define file paths
54+
const newYamlFilePath = path.join(SKILLS_DIR, filePath, 'qna.yaml');
55+
const newAttributionFilePath = path.join(SKILLS_DIR, filePath, 'attribution.txt');
4556

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

70+
if (action == 'update') {
71+
// Define file paths
72+
const oldYamlFilePath = path.join(SKILLS_DIR, oldFilesPath, 'qna.yaml');
73+
const oldAttributionFilePath = path.join(SKILLS_DIR, oldFilesPath, 'attribution.txt');
74+
75+
if (oldYamlFilePath != newYamlFilePath) {
76+
console.log('File path for the skill contribution is updated, removing the old files.');
77+
// Write the QnA YAML file
78+
const yamlFilePath = path.join(REPO_DIR, oldYamlFilePath);
79+
fs.unlinkSync(yamlFilePath);
80+
81+
// Write the attribution text file
82+
const attributionFilePath = path.join(REPO_DIR, oldAttributionFilePath);
83+
fs.unlinkSync(attributionFilePath);
84+
85+
await git.remove({ fs, dir: REPO_DIR, filepath: oldYamlFilePath });
86+
await git.remove({ fs, dir: REPO_DIR, filepath: oldAttributionFilePath });
87+
88+
amendCommit = true;
89+
}
90+
}
91+
5992
// Commit files
6093
await git.commit({
6194
fs,
@@ -64,12 +97,13 @@ Creator names: ${attributionData.creator_names}
6497
author: {
6598
name: name,
6699
email: email
67-
}
100+
},
101+
amend: amendCommit
68102
});
69103

70104
// Respond with success
71-
console.log('Skill contribution submitted successfully. Submission name is ', branchName);
72-
return NextResponse.json({ message: 'Skill contribution submitted successfully.', branch: branchName }, { status: 201 });
105+
console.log('Skill contribution submitted successfully. Submission name is ', skillBranchName);
106+
return NextResponse.json({ message: 'Skill contribution submitted successfully.', branch: skillBranchName }, { status: 201 });
73107
} catch (error) {
74108
console.error('Failed to create local branch and commit:', error);
75109
return NextResponse.json({ error: 'Failed to submit skill contribution.' }, { status: 500 });

src/app/edit-submission/knowledge/[id]/page.tsx renamed to src/app/edit-submission/knowledge/github/[id]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// src/app/edit-submission/knowledge/[id]/page.tsx
22
import * as React from 'react';
33
import { AppLayout } from '@/components/AppLayout';
4-
import EditKnowledge from '@/components/Contribute/EditKnowledge/EditKnowledge';
4+
import EditKnowledge from '@/components/Contribute/EditKnowledge/github/EditKnowledge';
55

66
type PageProps = {
77
params: Promise<{ id: string }>;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// src/app/edit-submission/knowledge/[id]/page.tsx
2+
import * as React from 'react';
3+
import { AppLayout } from '@/components/AppLayout';
4+
import EditKnowledgeNative from '@/components/Contribute/EditKnowledge/native/EditKnowledge';
5+
6+
type PageProps = {
7+
params: Promise<{ id: string }>;
8+
};
9+
10+
const EditKnowledgePage = async ({ params }: PageProps) => {
11+
const branchName = await params;
12+
13+
return (
14+
<AppLayout>
15+
<EditKnowledgeNative branchName={branchName.id} />
16+
</AppLayout>
17+
);
18+
};
19+
20+
export default EditKnowledgePage;

0 commit comments

Comments
 (0)