Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixes following skill contribution related issues #136

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/app/api/pr/skill/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function POST(req: NextRequest) {

try {
const body = await req.json();
const { content, attribution, name, email, submission_summary, filePath } = body;
const { content, attribution, name, email, submission_summary, task_description, filePath } = body;

const githubUsername = await getGitHubUsername(headers);
console.log('GitHub Username:', githubUsername);
Expand Down Expand Up @@ -78,7 +78,7 @@ Creator names: ${attributionData.creator_names}
);

// Create a pull request from the user's fork to the upstream repository
const pr = await createPullRequest(headers, githubUsername, branchName, submission_summary);
const pr = await createPullRequest(headers, githubUsername, branchName, submission_summary, task_description);

return NextResponse.json(pr, { status: 201 });
} catch (error) {
Expand Down Expand Up @@ -253,13 +253,14 @@ async function getCommitSha(headers: HeadersInit, username: string, branchName:
return data.object.sha;
}

async function createPullRequest(headers: HeadersInit, username: string, branchName: string, skillSummary: string) {
async function createPullRequest(headers: HeadersInit, username: string, branchName: string, skillSummary: string, skillDescription: string) {
const response = await fetch(`${GITHUB_API_URL}/repos/${UPSTREAM_REPO_OWNER}/${UPSTREAM_REPO_NAME}/pulls`, {
method: 'POST',
headers,
body: JSON.stringify({
title: `Skill: ${skillSummary}`,
head: `${username}:${branchName}`,
body: skillDescription,
base: BASE_BRANCH
})
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/Contribute/Knowledge/Submit/Submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const Submit: React.FC<Props> = ({ knowledgeFormData, setActionGroupAlertContent
const result = await response.json();
const actionGroupAlertContent: ActionGroupAlertContent = {
title: 'Knowledge contribution submitted successfully!',
message: `A new pull request has been created for your knowledge submission.`,
message: `Thank you for your contribution!`,
url: `${result.html_url}`,
success: true
};
Expand Down
34 changes: 20 additions & 14 deletions src/components/Contribute/Knowledge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,30 +355,36 @@ export const KnowledgeForm: React.FunctionComponent = () => {
setCreators={setCreators}
/>

<ActionGroup>
<Submit
knowledgeFormData={knowledgeFormData}
setActionGroupAlertContent={setActionGroupAlertContent}
githubUsername={githubUsername}
resetForm={resetForm}
/>
<DownloadYaml knowledgeFormData={knowledgeFormData} setActionGroupAlertContent={setActionGroupAlertContent} githubUsername={githubUsername} />
<DownloadAttribution knowledgeFormData={knowledgeFormData} setActionGroupAlertContent={setActionGroupAlertContent} />
</ActionGroup>
{actionGroupAlertContent && (
<Alert
variant={actionGroupAlertContent.success ? 'success' : 'danger'}
title={actionGroupAlertContent.title}
timeout={10000}
onTimeout={onCloseActionGroupAlert}
actionClose={<AlertActionCloseButton onClose={onCloseActionGroupAlert} />}
>
<p>
{actionGroupAlertContent.message}{' '}
<a href={actionGroupAlertContent.url} target="_blank" rel="noreferrer">
{actionGroupAlertContent.url}
</a>
{actionGroupAlertContent.message}
<br />
{actionGroupAlertContent.success && actionGroupAlertContent.url && actionGroupAlertContent.url.trim().length > 0 && (
<a href={actionGroupAlertContent.url} target="_blank" rel="noreferrer">
View your pull request
</a>
)}
</p>
</Alert>
)}

<ActionGroup>
<Submit
knowledgeFormData={knowledgeFormData}
setActionGroupAlertContent={setActionGroupAlertContent}
githubUsername={githubUsername}
resetForm={resetForm}
/>
<DownloadYaml knowledgeFormData={knowledgeFormData} setActionGroupAlertContent={setActionGroupAlertContent} githubUsername={githubUsername} />
<DownloadAttribution knowledgeFormData={knowledgeFormData} setActionGroupAlertContent={setActionGroupAlertContent} />
</ActionGroup>
</Form>
);
};
Expand Down
42 changes: 29 additions & 13 deletions src/components/Contribute/Skill/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,39 +140,39 @@ export const SkillForm: React.FunctionComponent = () => {

let validation = validateFields(infoFields);
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateFields(attributionFields);
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateEmail(email);
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateUniqueItems(questions, 'questions');
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateUniqueItems(answers, 'answers');
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
Expand Down Expand Up @@ -205,7 +205,15 @@ export const SkillForm: React.FunctionComponent = () => {
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ content: yamlString, attribution: attributionData, name, email, submission_summary, filePath: sanitizedFilePath })
body: JSON.stringify({
content: yamlString,
attribution: attributionData,
name,
email,
submission_summary,
task_description,
filePath: sanitizedFilePath
})
});

if (!response.ok) {
Expand Down Expand Up @@ -250,39 +258,39 @@ export const SkillForm: React.FunctionComponent = () => {

let validation = validateFields(infoFields);
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateFields(attributionFields);
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateEmail(email);
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateUniqueItems(questions, 'questions');
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
}

validation = validateUniqueItems(answers, 'answers');
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
Expand Down Expand Up @@ -326,7 +334,7 @@ export const SkillForm: React.FunctionComponent = () => {

const validation = validateFields(attributionFields);
if (!validation.valid) {
setFailureAlertTitle('Something went wrong!');
setFailureAlertTitle(validation.title);
setFailureAlertMessage(validation.message);
setIsFailureAlertVisible(true);
return;
Expand Down Expand Up @@ -526,6 +534,8 @@ export const SkillForm: React.FunctionComponent = () => {
<Alert
variant="success"
title={success_alert_title}
timeout={10000}
onTimeout={onCloseSuccessAlert}
actionClose={<AlertActionCloseButton onClose={onCloseSuccessAlert} />}
actionLinks={
<AlertActionLink component="a" href={success_alert_message} target="_blank" rel="noopener noreferrer">
Expand All @@ -537,7 +547,13 @@ export const SkillForm: React.FunctionComponent = () => {
</Alert>
)}
{isFailureAlertVisible && (
<Alert variant="danger" title={failure_alert_title} actionClose={<AlertActionCloseButton onClose={onCloseFailureAlert} />}>
<Alert
variant="danger"
title={failure_alert_title}
timeout={10000}
onTimeout={onCloseFailureAlert}
actionClose={<AlertActionCloseButton onClose={onCloseFailureAlert} />}
>
{failure_alert_message}
</Alert>
)}
Expand Down
19 changes: 11 additions & 8 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
// src/utils/validation.ts
export const validateFields = (fields: Record<string, string>): { valid: boolean; message: string } => {
export const validateFields = (fields: Record<string, string>): { valid: boolean; title: string; message: string } => {
for (const [key, value] of Object.entries(fields)) {
if (value.trim() === '') {
return {
valid: false,
message: `The ${key} field is empty. Please make sure all the fields are filled!`
title: `Please make sure you complete the ${key} field`,
message: `Some fields are not filled out`
};
}
}
return { valid: true, message: '' };
return { valid: true, title: '', message: '' };
};

export const validateEmail = (email: string): { valid: boolean; message: string } => {
export const validateEmail = (email: string): { valid: boolean; title: string; message: string } => {
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!emailRegex.test(email)) {
return {
valid: false,
message: 'Please enter a valid email address!'
title: `Email address issue!`,
message: `Please enter a valid email address.`
};
}
return { valid: true, message: '' };
return { valid: true, title: '', message: '' };
};

export const validateUniqueItems = (items: string[], itemType: string): { valid: boolean; message: string } => {
export const validateUniqueItems = (items: string[], itemType: string): { valid: boolean; title: string; message: string } => {
const uniqueItems = new Set(items);
if (uniqueItems.size !== items.length) {
return {
valid: false,
title: `Seed example issue!`,
message: `Please make sure all the ${itemType} are unique!`
};
}
return { valid: true, message: '' };
return { valid: true, title: '', message: '' };
};