Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #51 from abhinandanwadwa/main
Browse files Browse the repository at this point in the history
Error handling for github urls (#50)
  • Loading branch information
abhinandanwadwa authored Jan 7, 2023
2 parents c0c1d9a + f50e632 commit 976a004
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions frontend/src/components/NewProj/NewProj.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ const NewProj = () => {
}
}, []);


const notIsCorrectURL = async () => {
// Creating an instance of URL interface
let url;
try {
url = new URL(repoLink);
} catch (error) {
console.log(error);
return true;
}

if (!url) {
return false;
}

// Spliting the url by '/'
const splitURL = url.toString().split("/");

// if the link to the website is not github, return false;
if (url.hostname !== 'github.com') {
return true;
}

// Generating GitHub API URI for fetch request.
const githubRequestURI = `https://api.github.com/repos/${splitURL[3]}/${splitURL[4]}`;

const response = await fetch(githubRequestURI);
const json = await response.json();

// if the repo is not found, return false;
if (response.status !== 200) {
return true;
}
else {
return false;
}
}

const formSubmit = async (e) => {
e.preventDefault();

Expand Down Expand Up @@ -89,6 +127,9 @@ const NewProj = () => {
else if (image === null) {
toast.error("Please upload a valid image");
}
else if (await notIsCorrectURL()) {
toast.error("Please enter a correct GitHub url");
}
else {
const authtoken = localStorage.getItem('auth-token');
const response = await fetch('http://localhost:8181/api/auth/uploadproject', {
Expand Down

0 comments on commit 976a004

Please sign in to comment.