Parcourir la source

Fix parse v2 to use provided app name if not found in porter.yaml (#3550)

Feroze Mohideen il y a 2 ans
Parent
commit
9b763e7aab

+ 11 - 13
dashboard/src/lib/hooks/usePorterYaml.ts

@@ -9,17 +9,17 @@ import { z } from "zod";
 
 type PorterYamlStatus =
   | {
-      loading: true;
-      detectedName: null;
-      detectedServices: null;
-      porterYamlFound: boolean;
-    }
+    loading: true;
+    detectedName: null;
+    detectedServices: null;
+    porterYamlFound: false;
+  }
   | {
-      detectedServices: DetectedServices | null;
-      detectedName: string | null;
-      loading: false;
-      porterYamlFound: boolean;
-    };
+    detectedServices: DetectedServices | null;
+    detectedName: string | null;
+    loading: false;
+    porterYamlFound: boolean;
+  };
 
 /*
  *
@@ -52,8 +52,6 @@ export const usePorterYaml = ({
       source?.porter_yaml_path,
     ],
     async () => {
-      setPorterYamlFound(false);
-
       if (!currentProject || !source) {
         return;
       }
@@ -173,7 +171,7 @@ export const usePorterYaml = ({
       loading: true,
       detectedName: null,
       detectedServices: null,
-      porterYamlFound: true,
+      porterYamlFound: false,
     };
   }
 

+ 4 - 5
dashboard/src/main/home/app-dashboard/create-app/CreateApp.tsx

@@ -135,7 +135,7 @@ const CreateApp: React.FC<CreateAppProps> = ({ history }) => {
   const build = watch("app.build");
   const image = watch("source.image");
   const services = watch("app.services");
-  const { detectedServices: servicesFromYaml, porterYamlFound, detectedName } = usePorterYaml({ source: source?.type === "github" ? source : null });
+  const { detectedServices: servicesFromYaml, porterYamlFound, detectedName, loading: isLoadingPorterYaml } = usePorterYaml({ source: source?.type === "github" ? source : null });
   const deploymentTarget = useDefaultDeploymentTarget();
   const { updateAppStep } = useAppAnalytics(name.value);
   const { validateApp } = useAppValidation({
@@ -440,7 +440,7 @@ const CreateApp: React.FC<CreateAppProps> = ({ history }) => {
                               source={source}
                               projectId={currentProject.id}
                             />
-                            {!userHasSeenNoPorterYamlFoundModal && !porterYamlFound &&
+                            {!userHasSeenNoPorterYamlFoundModal && !porterYamlFound && !isLoadingPorterYaml &&
                               <Controller
                                 name="source.porter_yaml_path"
                                 control={control}
@@ -482,9 +482,8 @@ const CreateApp: React.FC<CreateAppProps> = ({ history }) => {
                             }
                           >
                             {detectedServices.count > 0
-                              ? `Detected ${detectedServices.count} service${
-                                  detectedServices.count > 1 ? "s" : ""
-                                } from porter.yaml.`
+                              ? `Detected ${detectedServices.count} service${detectedServices.count > 1 ? "s" : ""
+                              } from porter.yaml.`
                               : `Could not detect any services from porter.yaml. Make sure it exists in the root of your repo.`}
                           </Text>
                         </AppearingDiv>

+ 1 - 1
internal/porter_app/parse.go

@@ -41,7 +41,7 @@ func ParseYAML(ctx context.Context, porterYaml []byte, appName string) (*porterv
 
 	switch version.Version {
 	case PorterYamlVersion_V2:
-		appProto, err = v2.AppProtoFromYaml(ctx, porterYaml)
+		appProto, err = v2.AppProtoFromYaml(ctx, porterYaml, appName)
 		if err != nil {
 			return nil, telemetry.Error(ctx, span, err, "error converting v2 yaml to proto")
 		}

+ 6 - 1
internal/porter_app/v2/yaml.go

@@ -12,7 +12,7 @@ import (
 )
 
 // AppProtoFromYaml converts a Porter YAML file into a PorterApp proto object
-func AppProtoFromYaml(ctx context.Context, porterYamlBytes []byte) (*porterv1.PorterApp, error) {
+func AppProtoFromYaml(ctx context.Context, porterYamlBytes []byte, appName string) (*porterv1.PorterApp, error) {
 	ctx, span := telemetry.NewSpan(ctx, "v2-app-proto-from-yaml")
 	defer span.End()
 
@@ -26,6 +26,11 @@ func AppProtoFromYaml(ctx context.Context, porterYamlBytes []byte) (*porterv1.Po
 		return nil, telemetry.Error(ctx, span, err, "error unmarshaling porter yaml")
 	}
 
+	// if the porter yaml is missing a name field, use the app name that is provided in the request
+	if porterYaml.Name == "" {
+		porterYaml.Name = appName
+	}
+
 	appProto := &porterv1.PorterApp{
 		Name: porterYaml.Name,
 		Env:  porterYaml.Env,