From fe599f97a3e72f73dadaee82ee16b7cca67ef1ec Mon Sep 17 00:00:00 2001 From: neeraj Date: Fri, 1 Mar 2024 12:11:25 +0530 Subject: [PATCH 1/3] Handle error if repo already exists --- .../projects/create/template/index.tsx | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx index 9ff50c84..ef7c519f 100644 --- a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx +++ b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx @@ -1,7 +1,8 @@ import React, { useCallback, useEffect, useState } from 'react'; import { useForm, Controller, SubmitHandler } from 'react-hook-form'; import { useNavigate, useOutletContext, useParams } from 'react-router-dom'; -import toast from 'react-hot-toast'; +import { toast as reactHotToast } from 'react-hot-toast'; +import { RequestError } from 'octokit'; import assert from 'assert'; import { Button, Option, Typography } from '@material-tailwind/react'; @@ -10,6 +11,9 @@ import { useOctokit } from '../../../../../context/OctokitContext'; import { useGQLClient } from '../../../../../context/GQLClientContext'; import AsyncSelect from '../../../../../components/shared/AsyncSelect'; import { Template } from '../../../../../types'; +import { useToast } from 'components/shared/Toast'; + +const REPO_EXIST_ERROR = 'Could not clone: Name already exists on this account'; type SubmitRepoValues = { framework: string; @@ -22,6 +26,7 @@ const CreateRepo = () => { const { octokit } = useOctokit(); const { template } = useOutletContext<{ template: Template }>(); const client = useGQLClient(); + const { toast, dismiss } = useToast(); const { orgSlug } = useParams(); @@ -40,16 +45,34 @@ const CreateRepo = () => { const [owner, repo] = template.repoFullName.split('/'); // TODO: Handle this functionality in backend - const gitRepo = await octokit?.rest.repos.createUsingTemplate({ - template_owner: owner, - template_repo: repo, - owner: data.account, - name: data.repoName, - include_all_branches: false, - private: data.isPrivate, - }); + let gitRepo; + try { + gitRepo = await octokit?.rest.repos.createUsingTemplate({ + template_owner: owner, + template_repo: repo, + owner: data.account, + name: data.repoName, + include_all_branches: false, + private: data.isPrivate, + }); + } catch (error) { + if ( + !( + error instanceof RequestError && + error.message.includes(REPO_EXIST_ERROR) + ) + ) { + throw error; + } - if (!gitRepo) { + toast({ + id: 'repo-exist-error', + title: REPO_EXIST_ERROR, + variant: 'error', + onDismiss: dismiss, + }); + + setIsLoading(false); return; } @@ -72,7 +95,7 @@ const CreateRepo = () => { } catch (err) { console.error(err); setIsLoading(false); - toast.error('Error deploying project'); + reactHotToast.error('Error deploying project'); } }, [octokit], From 691edf35085f696919c522d65ac3a334020162a5 Mon Sep 17 00:00:00 2001 From: neeraj Date: Fri, 1 Mar 2024 13:26:38 +0530 Subject: [PATCH 2/3] Update toast message --- .../src/pages/org-slug/projects/create/template/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx index ef7c519f..c888178b 100644 --- a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx +++ b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx @@ -67,7 +67,7 @@ const CreateRepo = () => { toast({ id: 'repo-exist-error', - title: REPO_EXIST_ERROR, + title: 'Could not create: repository already exists', variant: 'error', onDismiss: dismiss, }); From dc6f43640956a37c888258e4c4b287c1f6a59395 Mon Sep 17 00:00:00 2001 From: neeraj Date: Fri, 1 Mar 2024 14:04:43 +0530 Subject: [PATCH 3/3] Handle review changes --- .../projects/create/template/index.tsx | 70 +++++++++---------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx index c888178b..7894d105 100644 --- a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx +++ b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx @@ -1,7 +1,6 @@ import React, { useCallback, useEffect, useState } from 'react'; import { useForm, Controller, SubmitHandler } from 'react-hook-form'; import { useNavigate, useOutletContext, useParams } from 'react-router-dom'; -import { toast as reactHotToast } from 'react-hot-toast'; import { RequestError } from 'octokit'; import assert from 'assert'; @@ -45,34 +44,16 @@ const CreateRepo = () => { const [owner, repo] = template.repoFullName.split('/'); // TODO: Handle this functionality in backend - let gitRepo; - try { - gitRepo = await octokit?.rest.repos.createUsingTemplate({ - template_owner: owner, - template_repo: repo, - owner: data.account, - name: data.repoName, - include_all_branches: false, - private: data.isPrivate, - }); - } catch (error) { - if ( - !( - error instanceof RequestError && - error.message.includes(REPO_EXIST_ERROR) - ) - ) { - throw error; - } + const gitRepo = await octokit?.rest.repos.createUsingTemplate({ + template_owner: owner, + template_repo: repo, + owner: data.account, + name: data.repoName, + include_all_branches: false, + private: data.isPrivate, + }); - toast({ - id: 'repo-exist-error', - title: 'Could not create: repository already exists', - variant: 'error', - onDismiss: dismiss, - }); - - setIsLoading(false); + if (!gitRepo) { return; } @@ -84,18 +65,31 @@ const CreateRepo = () => { template: 'webapp', }); - if (Boolean(addProject)) { - setIsLoading(true); - navigate( - `deploy?projectId=${addProject.id}&templateId=${template.id}`, - ); - } else { - setIsLoading(false); - } + navigate(`deploy?projectId=${addProject.id}&templateId=${template.id}`); } catch (err) { - console.error(err); setIsLoading(false); - reactHotToast.error('Error deploying project'); + + if ( + err instanceof RequestError && + err.message.includes(REPO_EXIST_ERROR) + ) { + toast({ + id: 'repo-exist-error', + title: 'Could not create: repository already exists', + variant: 'error', + onDismiss: dismiss, + }); + + return; + } + + console.error(err); + toast({ + id: 'error-deploying-project', + title: 'Error deploying project', + variant: 'error', + onDismiss: dismiss, + }); } }, [octokit],