Skip to content

Commit

Permalink
Merge pull request instructlab#48 from nerdalert/yaml-line-breaks
Browse files Browse the repository at this point in the history
Bug fixes: amend DCO sign-off and pass data structures to the submission routes
  • Loading branch information
vishnoianil authored Jul 10, 2024
2 parents 2148ba4 + 02ebea2 commit 391fd33
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 203 deletions.
72 changes: 14 additions & 58 deletions src/app/api/pr/knowledge/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
import { NextRequest } from 'next/server';
import yaml from 'js-yaml';
import { SchemaVersion } from '@/types';
import { KnowledgeYamlData, AttributionData, YamlLineLength } from '@/types';

const GITHUB_API_URL = 'https://api.github.com';
const UPSTREAM_REPO_OWNER = process.env.NEXT_PUBLIC_TAXONOMY_REPO_OWNER!;
Expand All @@ -29,23 +29,10 @@ export async function POST(req: NextRequest) {

try {
const body = await req.json();
const {
name,
email,
task_description,
submission_summary,
domain,
repo,
commit,
patterns,
questions,
answers,
title_work,
link_work,
revision,
license_work,
creators
} = body;
const { content, name, email, submission_summary, attribution, filePath } = body;

const knowledgeData: KnowledgeYamlData = yaml.load(content) as KnowledgeYamlData;
const attributionData: AttributionData = attribution;

// Fetch GitHub username
const githubUsername = await getGitHubUsername(headers);
Expand All @@ -56,51 +43,20 @@ export async function POST(req: NextRequest) {
if (!forkExists) {
await createFork(headers);
// Add a delay to ensure the fork operation completes to avoid a race condition when retrieving the base SHA
// This only occurs if this is the first time submitting and the fork isn't present.
// TODO change to a retry
console.log('Pause 5s for the forking operation to complete');
await new Promise((resolve) => setTimeout(resolve, 5000));
}

const branchName = `knowledge-contribution-${Date.now()}`;
const newYamlFilePath = `knowledge/${name.replace(/ /g, '_')}-${Date.now()}.yaml`;
const newAttributionFilePath = `knowledge/${name.replace(/ /g, '_')}-attribution.txt`;

interface SeedExample {
question: string;
answer: string;
}

interface Document {
repo: string;
commit: string;
patterns: string[];
}

const yamlData = {
created_by: githubUsername,
version: SchemaVersion,
domain: domain,
task_description: task_description,
seed_examples: questions.map((question: string, index: number) => {
return {
question,
answer: answers[index]
} as SeedExample;
}),
document: {
repo: repo,
commit: commit,
patterns: patterns.split(',').map((pattern: string) => pattern.trim())
} as Document
};

const yamlString = yaml.dump(yamlData, { lineWidth: -1 });
const attributionContent = `Title of work: ${title_work}
Link to work: ${link_work}
Revision: ${revision}
License of the work: ${license_work}
Creator names: ${creators}
const newYamlFilePath = `${filePath}qna.yaml`;
const newAttributionFilePath = `${filePath}attribution.txt`;

const yamlString = yaml.dump(knowledgeData, { lineWidth: YamlLineLength });
const attributionContent = `Title of work: ${attributionData.title_of_work}
Link to work: ${attributionData.link_to_work}
Revision: ${attributionData.revision}
License of the work: ${attributionData.license_of_the_work}
Creator names: ${attributionData.creator_names}
`;

// Get the base branch SHA
Expand Down
47 changes: 14 additions & 33 deletions src/app/api/pr/skill/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
import { NextRequest } from 'next/server';
import yaml from 'js-yaml';
import { SchemaVersion } from '@/types';
import { YamlLineLength, SkillYamlData, AttributionData } from '@/types';

const GITHUB_API_URL = 'https://api.github.com';
const UPSTREAM_REPO_OWNER = process.env.NEXT_PUBLIC_TAXONOMY_REPO_OWNER!;
Expand All @@ -29,9 +29,8 @@ export async function POST(req: NextRequest) {

try {
const body = await req.json();
const { name, email, task_description, submission_summary, title_work, link_work, license_work, creators, questions, contexts, answers } = body;
const { content, attribution, name, email, submission_summary, filePath } = body;

// Fetch GitHub username
const githubUsername = await getGitHubUsername(headers);
console.log('GitHub Username:', githubUsername);

Expand All @@ -42,37 +41,19 @@ export async function POST(req: NextRequest) {
}

const branchName = `skill-contribution-${Date.now()}`;
const newYamlFilePath = `skills/${name.replace(/ /g, '_')}-qna.yaml`;
const newAttributionFilePath = `skills/${name.replace(/ /g, '_')}-attribution.txt`;
const newYamlFilePath = `${filePath}qna.yaml`;
const newAttributionFilePath = `${filePath}attribution.txt`;

interface SeedExample {
question: string;
answer: string;
context?: string;
}
const skillData = yaml.load(content) as SkillYamlData;
const attributionData = attribution as AttributionData;

const yamlString = yaml.dump(skillData, { lineWidth: YamlLineLength });

const yamlData = {
created_by: githubUsername,
version: SchemaVersion,
task_description: task_description,
seed_examples: questions.map((question: string, index: number) => {
const example: SeedExample = {
question,
answer: answers[index]
};
if (contexts[index].trim() !== '') {
example.context = contexts[index];
}
return example;
})
};

const yamlString = yaml.dump(yamlData, { lineWidth: -1 });
const attributionContent = `Title of work: ${title_work}
Link to work: ${link_work}
Revision: -
License of the work: ${license_work}
Creator names: ${creators}
const attributionString = `Title of work: ${attributionData.title_of_work}
Link to work: ${attributionData.link_to_work}
Revision: ${attributionData.revision}
License of the work: ${attributionData.license_of_the_work}
Creator names: ${attributionData.creator_names}
`;

// Get the base branch SHA
Expand All @@ -88,7 +69,7 @@ Creator names: ${creators}
githubUsername,
[
{ path: newYamlFilePath, content: yamlString },
{ path: newAttributionFilePath, content: attributionContent }
{ path: newAttributionFilePath, content: attributionString }
],
branchName,
`${submission_summary}\n\nSigned-off-by: ${name} <${email}>`
Expand Down
107 changes: 80 additions & 27 deletions src/app/edit-submission/knowledge/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Button } from '@patternfly/react-core/dist/dynamic/components/Button';
import { Text } from '@patternfly/react-core/dist/dynamic/components/Text';
import { AppLayout } from '../../../../components/AppLayout';
import { UploadFile } from '../../../../components/Contribute/Knowledge/UploadFile';
import { AttributionData, PullRequestFile, KnowledgeYamlData, SchemaVersion } from '@/types';
import { AttributionData, PullRequestFile, KnowledgeYamlData, SchemaVersion, YamlLineLength } from '@/types';
import {
fetchPullRequest,
fetchFileContent,
Expand All @@ -30,7 +30,9 @@ import {
import yaml from 'js-yaml';
import axios from 'axios';

const EditPullRequestPage: React.FunctionComponent<{ params: { id: string } }> = ({ params }) => {
const UPSTREAM_REPO_NAME = process.env.NEXT_PUBLIC_TAXONOMY_REPO!;

const EditKnowledgePage: React.FunctionComponent<{ params: { id: string } }> = ({ params }) => {
const { data: session } = useSession();
const [title, setTitle] = React.useState('');
const [body, setBody] = React.useState('');
Expand All @@ -54,6 +56,8 @@ const EditPullRequestPage: React.FunctionComponent<{ params: { id: string } }> =
const [branchName, setBranchName] = React.useState<string | null>(null);
const [useFileUpload, setUseFileUpload] = React.useState(false);
const [uploadedFiles, setUploadedFiles] = React.useState<File[]>([]);
const [filePath, setFilePath] = React.useState<string>('');
const [originalFilePath, setOriginalFilePath] = React.useState<string>(''); // Store original file path
const router = useRouter();
const number = parseInt(params.id, 10);

Expand Down Expand Up @@ -94,7 +98,6 @@ const EditPullRequestPage: React.FunctionComponent<{ params: { id: string } }> =
console.log('Parsed YAML data:', yamlData);

// Populate the form fields with YAML data
setEmail(yamlData.created_by);
setTaskDescription(yamlData.task_description);
setDomain(yamlData.domain);
setRepo(yamlData.document.repo);
Expand All @@ -103,6 +106,11 @@ const EditPullRequestPage: React.FunctionComponent<{ params: { id: string } }> =
setQuestions(yamlData.seed_examples.map((example) => example.question));
setAnswers(yamlData.seed_examples.map((example) => example.answer));

// Set the file path from the current YAML file
const currentFilePath = foundYamlFile.filename.split('/').slice(0, -1).join('/');
setFilePath(currentFilePath);
setOriginalFilePath(currentFilePath); // Store the original file path

// Fetch and parse attribution file if it exists
const foundAttributionFile = prFiles.find((file: PullRequestFile) => file.filename.includes('attribution'));
if (foundAttributionFile) {
Expand Down Expand Up @@ -135,7 +143,7 @@ const EditPullRequestPage: React.FunctionComponent<{ params: { id: string } }> =
}, [session, number, params]);

const handleSave = async () => {
if (session?.accessToken && yamlFile && attributionFile && branchName) {
if (session?.accessToken && yamlFile && attributionFile && branchName && email && name) {
try {
console.log(`Updating PR with number: ${number}`);
await updatePullRequest(session.accessToken, number, { title, body });
Expand All @@ -144,7 +152,7 @@ const EditPullRequestPage: React.FunctionComponent<{ params: { id: string } }> =
console.log(`GitHub username: ${githubUsername}`);

const updatedYamlData: KnowledgeYamlData = {
created_by: email,
created_by: githubUsername,
version: SchemaVersion,
domain,
task_description,
Expand All @@ -159,32 +167,55 @@ const EditPullRequestPage: React.FunctionComponent<{ params: { id: string } }> =
}))
};
const updatedYamlContent = yaml.dump(updatedYamlData, {
lineWidth: -1,
lineWidth: YamlLineLength,
noCompatMode: true,
quotingType: '"'
});

console.log('Updated YAML content:', updatedYamlContent);

const updatedAttributionContent = `Title of work: ${title_work}
Link to work: ${link_work}
Revision: ${revision}
License of the work: ${license_work}
Creator names: ${creators}
const updatedAttributionData: AttributionData = {
title_of_work: title_work,
link_to_work: link_work,
revision,
license_of_the_work: license_work,
creator_names: creators
};

const updatedAttributionContent = `Title of work: ${updatedAttributionData.title_of_work}
Link to work: ${updatedAttributionData.link_to_work}
Revision: ${updatedAttributionData.revision}
License of the work: ${updatedAttributionData.license_of_the_work}
Creator names: ${updatedAttributionData.creator_names}
`;

console.log('Updated Attribution content:', updatedAttributionContent);

// Update the commit by amending it with the new content
console.log(`Amending commit with updated content`);
const commitMessage = `Amend commit with updated content\n\nSigned-off-by: ${name} <${email}>`;

// Ensure proper file paths for the edit
const finalYamlPath = filePath.replace(/^\//, '').replace(/\/?$/, '/') + yamlFile.filename.split('/').pop();
const finalAttributionPath = filePath.replace(/^\//, '').replace(/\/?$/, '/') + attributionFile.filename.split('/').pop();

const oldFilePath = {
yaml: originalFilePath.replace(/^\//, '').replace(/\/?$/, '/') + yamlFile.filename.split('/').pop(),
attribution: originalFilePath.replace(/^\//, '').replace(/\/?$/, '/') + attributionFile.filename.split('/').pop()
};

const newFilePath = {
yaml: finalYamlPath,
attribution: finalAttributionPath
};

const amendedCommitResponse = await amendCommit(
session.accessToken,
githubUsername,
'taxonomy-sub-testing',
{ yaml: yamlFile.filename, attribution: attributionFile.filename },
UPSTREAM_REPO_NAME,
oldFilePath,
newFilePath,
updatedYamlContent,
updatedAttributionContent,
branchName
branchName,
commitMessage
);
console.log('Amended commit response:', amendedCommitResponse);

Expand All @@ -208,7 +239,7 @@ Creator names: ${creators}
}
} else {
setFailureAlertTitle('Error');
setFailureAlertMessage('YAML file, Attribution file, or branch name is missing.');
setFailureAlertMessage('YAML file, Attribution file, branch name, email, or name is missing.');
setIsFailureAlertVisible(true);
}
};
Expand Down Expand Up @@ -337,14 +368,6 @@ Creator names: ${creators}
}
>
<FormGroup isRequired key={'author-info-details-id'}>
<TextInput
isRequired
type="email"
aria-label="email"
placeholder="Enter your github ID"
value={email}
onChange={(_event, value) => setEmail(value)}
/>
<TextInput
isRequired
type="text"
Expand All @@ -353,6 +376,14 @@ Creator names: ${creators}
value={name}
onChange={(_event, value) => setName(value)}
/>
<TextInput
isRequired
type="email"
aria-label="email"
placeholder="Enter your email address"
value={email}
onChange={(_event, value) => setEmail(value)}
/>
</FormGroup>
</FormFieldGroupExpandable>

Expand Down Expand Up @@ -386,6 +417,28 @@ Creator names: ${creators}
</FormGroup>
</FormFieldGroupExpandable>

<FormFieldGroupExpandable
isExpanded
toggleAriaLabel="Details"
header={
<FormFieldGroupHeader
titleText={{ text: 'File Path Info', id: 'file-path-info-id' }}
titleDescription="Specify the file path for the QnA and Attribution files."
/>
}
>
<FormGroup isRequired key={'file-path-details-id'}>
<TextInput
isRequired
type="text"
aria-label="filePath"
placeholder="Enter the file path for both files"
value={filePath}
onChange={(_event, value) => setFilePath(value)}
/>
</FormGroup>
</FormFieldGroupExpandable>

<FormFieldGroupExpandable
toggleAriaLabel="Details"
header={
Expand Down Expand Up @@ -580,4 +633,4 @@ Creator names: ${creators}
);
};

export default EditPullRequestPage;
export default EditKnowledgePage;
Loading

0 comments on commit 391fd33

Please sign in to comment.