Advertisement
kopyl

Untitled

Sep 7th, 2022
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const useDetection = () => {
  2.   const [createConnectionError, setCreateConnectionError] = useState<any>();
  3.   const [isDetecting, setIsDetecting] = useState<boolean>(false);
  4.   const [autoDetectId, setAutoDetectId] = useState<string | undefined>();
  5.   const [repoType, setRepoType] = useState<string>();
  6.   const { setDetectedService } = useOnboarding();
  7.  
  8.   const detect = async (repo: any) => {
  9.     if (isDetecting) return;
  10.  
  11.     setCreateConnectionError(null);
  12.     setIsDetecting(true);
  13.  
  14.     try {
  15.       const result = await apiAutodetectAutodetectCreate({
  16.         repository: repo.repo,
  17.         repository_type: repo.type,
  18.       });
  19.       setRepoType(repo.type);
  20.       setAutoDetectId(result?.autodetect_job_id);
  21.     } catch {
  22.       setCreateConnectionError(["An unexpected error happened"]);
  23.     }
  24.   };
  25.  
  26.   useEffect(() => {
  27.     const getAutodetectingJobStatus = async () => {
  28.       if (autoDetectId) {
  29.         const result = await apiAutodetectAutodetectjobsRead(autoDetectId);
  30.         switch (result.status) {
  31.           case AutodetectionJobStatus.FAILED: {
  32.             setIsDetecting(false);
  33.             setCreateConnectionError([
  34.               "An error occurred trying to fetch your repository. Try to refresh the page and try again or contact the Digger team on slack.",
  35.             ]);
  36.             return;
  37.           }
  38.           case AutodetectionJobStatus.COMPLETED: {
  39.             setIsDetecting(false);
  40.             const service_type = !!result?.service_type && result?.service_type;
  41.             const supportedArray = [
  42.               AutodetectionJobServiceType.container,
  43.               AutodetectionJobServiceType.webapp,
  44.               AutodetectionJobServiceType.containerisable,
  45.             ];
  46.  
  47.             // @ts-ignore
  48.             if (!supportedArray.includes(service_type)) {
  49.               setCreateConnectionError([
  50.                 "The repository doesn't have a Dockerfile which is required for deployment.",
  51.               ]);
  52.               return;
  53.             }
  54.  
  55.             return setDetectedService({
  56.               type: repoType,
  57.               details: result?.service_details,
  58.               repository: result?.repository,
  59.               branch: result?.repository_default_branch,
  60.               service_name: result?.service_name,
  61.               service_type,
  62.             });
  63.           }
  64.           default: {
  65.             setTimeout(getAutodetectingJobStatus, 3000);
  66.           }
  67.         }
  68.       }
  69.     };
  70.     if (autoDetectId) {
  71.       getAutodetectingJobStatus();
  72.     }
  73.   }, [autoDetectId, setDetectedService]);
  74.  
  75.   return [detect, isDetecting, createConnectionError] as const;
  76. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement