Check if repo with the same name already exists

This commit is contained in:
IshaVenikar 2024-10-28 15:48:38 +05:30
parent 63969ae25a
commit 3b01ee03e7

View File

@ -41,6 +41,17 @@ const CreateRepo = () => {
const [gitAccounts, setGitAccounts] = useState<string[]>([]);
const [isLoading, setIsLoading] = useState(false);
const checkRepoExists = async (account: string, repoName: string) => {
try {
await octokit.rest.repos.get({ owner: account, repo: repoName });
return true;
} catch (error) {
// Error handled by octokit error hook interceptor in Octokit context
console.error(error);
return;
}
};
const submitRepoHandler: SubmitHandler<SubmitRepoValues> = useCallback(
async (data) => {
assert(data.account);
@ -50,6 +61,18 @@ const CreateRepo = () => {
assert(template.repoFullName, 'Template URL not provided');
const [owner, repo] = template.repoFullName.split('/');
const repoExists = await checkRepoExists(data.account, data.repoName);
if (repoExists) {
toast({
id: 'repo-exist-error',
title: 'Repository already exists with this name',
variant: 'warning',
onDismiss: dismiss,
});
setIsLoading(false);
return;
}
setIsLoading(true);
navigate(
@ -81,7 +104,7 @@ const CreateRepo = () => {
});
}
},
[octokit],
[octokit, toast],
);
useEffect(() => {