Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const useDetection = () => {
- const [createConnectionError, setCreateConnectionError] = useState<any>();
- const [isDetecting, setIsDetecting] = useState<boolean>(false);
- const [autoDetectId, setAutoDetectId] = useState<string | undefined>();
- const [repoType, setRepoType] = useState<string>();
- const { setDetectedService } = useOnboarding();
- const detect = async (repo: any) => {
- if (isDetecting) return;
- setCreateConnectionError(null);
- setIsDetecting(true);
- try {
- const result = await apiAutodetectAutodetectCreate({
- repository: repo.repo,
- repository_type: repo.type,
- });
- setRepoType(repo.type);
- setAutoDetectId(result?.autodetect_job_id);
- } catch {
- setCreateConnectionError(["An unexpected error happened"]);
- }
- };
- useEffect(() => {
- const getAutodetectingJobStatus = async () => {
- if (autoDetectId) {
- const result = await apiAutodetectAutodetectjobsRead(autoDetectId);
- switch (result.status) {
- case AutodetectionJobStatus.FAILED: {
- setIsDetecting(false);
- setCreateConnectionError([
- "An error occurred trying to fetch your repository. Try to refresh the page and try again or contact the Digger team on slack.",
- ]);
- return;
- }
- case AutodetectionJobStatus.COMPLETED: {
- setIsDetecting(false);
- const service_type = !!result?.service_type && result?.service_type;
- const supportedArray = [
- AutodetectionJobServiceType.container,
- AutodetectionJobServiceType.webapp,
- AutodetectionJobServiceType.containerisable,
- ];
- // @ts-ignore
- if (!supportedArray.includes(service_type)) {
- setCreateConnectionError([
- "The repository doesn't have a Dockerfile which is required for deployment.",
- ]);
- return;
- }
- return setDetectedService({
- type: repoType,
- details: result?.service_details,
- repository: result?.repository,
- branch: result?.repository_default_branch,
- service_name: result?.service_name,
- service_type,
- });
- }
- default: {
- setTimeout(getAutodetectingJobStatus, 3000);
- }
- }
- }
- };
- if (autoDetectId) {
- getAutodetectingJobStatus();
- }
- }, [autoDetectId, setDetectedService]);
- return [detect, isDetecting, createConnectionError] as const;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement